forked from Diusrex/UVA-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
10191 Longest Nap.cpp
75 lines (54 loc) · 1.96 KB
/
10191 Longest Nap.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#include <vector>
#include <iostream>
#include <cstdio>
using namespace std;
const int TOTAL_MINUTES = 8 * 60;
int main()
{
std::string junk;
int N;
int T = 1;
vector<bool> timeAvailable(TOTAL_MINUTES + 2);
timeAvailable[TOTAL_MINUTES + 1] = false;
while (cin >> N)
{
for (int i = 0; i <= TOTAL_MINUTES; ++i)
timeAvailable[i] = true;
while (N--)
{
int startHr, startMin, endHr, endMin;
int startTime, endTime;
char startSeparator, endSeparator;
cin >> startHr >> startSeparator >> startMin >> endHr >> endSeparator >> endMin;
getline(cin, junk);
startTime = (startHr - 10) * 60 + startMin;
endTime = (endHr - 10) * 60 + endMin;
for (int i = startTime; i < endTime; ++i)
timeAvailable[i] = false;
}
int bestStartTime, bestLength = 0;
for (int i = 0; i <= TOTAL_MINUTES; ++i)
{
if (timeAvailable[i])
{
int j = i + 1;
while (timeAvailable[j])
++j;
if (j == TOTAL_MINUTES + 1)
--j;
if (j - i > bestLength)
{
bestStartTime = i;
bestLength = j - i;
}
i = j;
}
}
if (bestLength >= 60)
printf("Day #%d: the longest nap starts at %d:%02d and will last for %d hours and %d minutes.\n",
T++, bestStartTime / 60 + 10, bestStartTime % 60, bestLength / 60, bestLength % 60);
else
printf("Day #%d: the longest nap starts at %d:%02d and will last for %d minutes.\n",
T++, bestStartTime / 60 + 10, bestStartTime % 60, bestLength);
}
}