# Week14 cat sleeping problem

Posted by rline101 on Sun, 14 Jun 2020 05:38:52 +0200

Title:
As we all know, TT has a magic meow at home. This meow is very sleepy. There is no day or night when you sleep. Meow meow can sleep many times a day!! Sleep as long as you want
Meow sleep time is continuous, that is, once meow starts to sleep, it can not be disturbed, otherwise meow will bite people
It can be assumed that meow must sleep for at least A continuous hour, that is, once meow starts to sleep, it cannot be disturbed for at least A continuous hour (A*60 minutes)!
Now you know that meow is very sleepy. He eats, drinks, pulls, scatters and sleeps all day long. In other words, he either sleeps or wakes up!
As we all know, this magic meow is lazy, just like TT. It can't move continuously for more than B hours in the daytime.
The cat master is very comfortable without working or writing code, so he just wants to sleep.
But now the cat owner is interested in a new look on BiliBili website.
The new broadcast time has been pasted on the bedside (the same schedule is used every day), during which time it must be awake!!
As a meow, he thinks it's very troublesome to arrange time. Now please help him to arrange sleeping time.
Ideas:
It's a simulation problem, but it's very complicated to deal with, and there are many holes. After consulting my classmates, I finally understood how to do it.

1. If the interval between the two intervals is less than A, the rest of the sleeping time can be A-hour (only one interval needs special judgment).
2. Judge whether the duration of the merged interval exceeds B hours. If it exceeds B hours, output "No". Otherwise, output the rest sleeping time.
3. Pay attention to the closed interval and the new day.
code:
```#include <iostream>
#include <algorithm>
using namespace std;

struct timeNode
{
timeNode(int _h = 0, int _min = 0) : h(_h), minute(_min) {}
timeNode operator-(const timeNode &t)
{
timeNode tempt(h, minute);
if (tempt < t)
{
tempt.h += 24;
}
if (tempt.minute < t.minute)
{
tempt.h -= 1;
tempt.minute += 60;
}
tempt.minute -= t.minute;
tempt.h -= t.h;
return tempt;
}
bool operator<(const timeNode &t)
{
if (h < t.h)
return true;
else if (h > t.h)
return false;
else
return minute < t.minute;
}
bool operator>(const timeNode &t)
{
if (h > t.h)
return true;
else if (h < t.h)
return false;
else
return minute > t.minute;
}
timeNode &operator=(const timeNode &t)
{
h = t.h;
minute = t.minute;
return *this;
}
int h, minute;
};
timeNode &minus1min(timeNode &t)
{
timeNode tempt = t;
if (tempt.minute == 0)
{
if (tempt.h == 0)
tempt.h += 24;
tempt.h--;
tempt.minute += 60;
}
tempt.minute--;
return tempt;
}
timeNode &plus1min(timeNode &t)
{
timeNode tempt = t;
tempt.minute += 1;
if (tempt.minute == 60)
{
tempt.minute = 0;
tempt.h++;
}
if (tempt.h == 24)
tempt.h = 0;
return tempt;
}
struct timeInterval
{
timeInterval() {}
timeInterval(struct timeNode _st, struct timeNode _et)
{
startTime = _st;
endTime = _et;
};
bool operator<(const timeInterval &t) { return startTime < t.startTime; }
timeInterval &operator=(const timeInterval &t)
{
startTime = t.startTime;
endTime = t.endTime;
return *this;
}
timeNode startTime, endTime;
};
timeInterval tiInt;
int a, b, n, cnt;
bool comp(timeInterval &_a, timeInterval &_b)
{
return _a < _b;
}
int main()
{
while (scanf("%d %d %d", &a, &b, &n) != EOF)
{
timeNode A(a, 1), B(b - 1, 59);
bool flag = true;
cnt = 0;
for (int i = 1; i <= n; i++)
{
int a1, a2, a3, a4;
scanf("%d:%d-%d:%d", &a1, &a2, &a3, &a4);
timeNode t1(a1, a2), t2(a3, a4);
tiInt[++cnt].startTime = t1;
tiInt[cnt].endTime = t2;
}
sort(tiInt + 1, tiInt + 1 + cnt, comp);
for (int i = 1; i < cnt; i++)
{
if (tiInt[i + 1].startTime - tiInt[i].endTime < A)
{
tiInt[i].endTime = tiInt[i + 1].endTime;
for (int j = i + 1; j < cnt; j++) //Move forward
tiInt[j] = tiInt[j + 1];
cnt--;
i--;
}
}
if (cnt != 1 && tiInt.startTime - tiInt[cnt].endTime < A)
{
tiInt[cnt].endTime = tiInt.endTime;
for (int i = 1; i < cnt; i++)
tiInt[i] = tiInt[i + 1];
cnt--;
}
for (int i = 1; i <= cnt; i++)
{
if (tiInt[i].endTime - tiInt[i].startTime > B)
{
flag = false;
break;
}
}
if (!flag)
{
printf("No\n");
continue;
}
if (cnt == 1 && tiInt.startTime - tiInt.endTime < A)
{
printf("No\n");
continue;
}
printf("Yes\n%d\n", cnt);
for (int i = 1; i <= cnt; i++)
{
timeNode t1 = plus1min(tiInt[i % cnt == 0 ? cnt : i % cnt].endTime);
timeNode t2 = minus1min(tiInt[(i + 1) % cnt == 0 ? cnt : (i + 1) % cnt].startTime);
printf("%02d:%02d-%02d:%02d\n", t1.h, t1.minute, t2.h, t2.minute);
}
}
return 0;
}

```

Topics: REST less