游客 Signup | Login
中文 | En

1918 - 挤牛奶

三个农民每天清晨5点起床,然后去牛棚给3头牛挤奶。第一个农民在300秒(从5点开始计时)给他的牛挤奶,一直到1000秒。第二个农民在700秒开始,在 1200秒结束。第三个农民在1500秒开始2100秒结束。期间最长的至少有一个农民在挤奶的连续时间为900秒(从300秒到1200秒),而最长的无人挤奶的连续时间(从挤奶开始一直到挤奶结束)为300秒(从1200秒到1500秒)。 
你的任务是编一个程序,读入一个有N个农民(1 <= N <= 5000)挤N头牛的工作时间列表,计算以下两点(均以秒为单位): 
• 最长至少有一人在挤奶的时间段。 
• 最长的无人挤奶的时间段。(从有人挤奶开始算起) 

Input

第一行: 
一个整数N。 
第2到N+1行: 
每行两个小于1000000的非负整数,表示一个农民的开始时刻与结束时刻。 

Output

一行,用一个空格隔开的两个整数,即题目所要求的两个答案。

Examples

Input

3
300 1000
700 1200
1500 2100

Output

900 300

Solution C++

#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;

struct Node{
	int a, b;
}p[5010], t;
bool cmp(Node x, Node y){
	return x.a < y.a;
}
int main(){
	//freopen("farmer.in","r",stdin);
	//freopen("farmer.out","w",stdout);
	int n, i, ans1, ans2;
	while(scanf("%d", &n) != EOF){
		for(i = 0; i < n; i++)
			scanf("%d%d", &p[i].a, &p[i].b);
		sort(p, p+n, cmp);
		t = p[0];
		ans1 = p[0].b-p[0].a;
		ans2 = 0;
		for(i = 1; i < n; i++){
			if(p[i].a >= t.a && p[i].a <= t.b){
				if(p[i].b > t.b){
					t.b = p[i].b;
				}
			}else{
				if(ans1 < t.b-t.a) ans1 = t.b-t.a;
				if(ans2 < p[i].a-t.b) ans2 = p[i].a-t.b;
				t = p[i];
			}
		}
		if(ans1 < t.b-t.a) ans1 = t.b-t.a;
		if(ans2 < p[i].a-t.b) ans2 = p[i].a-t.b;
		printf("%d %d\n", ans1, ans2);
    }
	return 0;
}
Time Limit 1 second
Memory Limit 128 MB
Discuss Stats
上一题 下一题