-
Notifications
You must be signed in to change notification settings - Fork 0
/
milk2_v1.cpp
74 lines (63 loc) · 1.35 KB
/
milk2_v1.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
/*
ID: wxx54331
PROG: milk2
LANG: C++
*/
/*
由于时间的范围是1e6次方,因此开了一个bool数组,来记录某个时间点是否有人喂奶。
然后遍历每一个区间,把这个区间内的时间点都置为有人喂奶。
最后只要统计这个bool数组,就能得到结果
*/
#include<iostream>
#include<fstream>
using namespace std;
int main()
{
ifstream fin("milk2.in");
ofstream fout("milk2.out");
const int MAX = 1000001;
bool isMilk[MAX] = {}; /*记录某个时间点是否有人喂奶*/
int N;
fin >> N;
int a,b;
int begin = MAX, end = 0; /*begin和end用来记录所有区间的头和尾*/
int time;
for(int i = 0; i < N; ++i) /*遍历所有喂奶的区间*/
{
fin >> a >> b;
if(a < begin)
begin = a;
if(b >end)
end = b;
for(time = a+1; time <= b; ++time)
isMilk[time] = true;
}
int longestMilkTime = 0, longestNoMilkTime = 0;
int tmp = 0;
for(time = begin; time <= end+1; ++time)
{
if(isMilk[time] == true)
tmp++;
else
{
if(tmp > longestMilkTime)
longestMilkTime = tmp;
tmp = 0;
}
}
for(time = begin+1, tmp=0; time <= end; ++time)
{
if(isMilk[time] == false)
tmp++;
else
{
if(tmp > longestNoMilkTime)
longestNoMilkTime = tmp;
tmp = 0;
}
}
fout << longestMilkTime << " " << longestNoMilkTime << endl;
fin.close();
fout.close();
return 0;
}