Milking Cows
来源:互联网 发布:数据库建模工具哪个好 编辑:程序博客网 时间:2024/04/28 20:40
Three farmers rise at 5 am each morning and head for the barn to milk three cows. The first farmer begins milking his cow at time 300 (measured in seconds after 5 am) and ends at time 1000. The second farmer begins at time 700 and ends at time 1200. The third farmer begins at time 1500 and ends at time 2100. The longest continuous time during which at least one farmer was milking a cow was 900 seconds (from 300 to 1200). The longest time no milking was done, between the beginning and the ending of all milking, was 300 seconds (1500 minus 1200).
Your job is to write a program that will examine a list of beginning and ending times for N (1 <= N <= 5000) farmers milking N cows and compute (in seconds):
- The longest time interval at least one cow was milked.
- The longest time interval (after milking starts) during which no cows were being milked.
PROGRAM NAME: milk2
INPUT FORMAT
Line 1:The single integerLines 2..N+1:Two non-negative integers less than 1000000, the starting and ending time in seconds after 0500SAMPLE INPUT (file milk2.in)
3300 1000700 12001500 2100
OUTPUT FORMAT
A single line with two integers that represent the longest continuous time of milking and the longest idle time.SAMPLE OUTPUT (file milk2.out)
900 300
/*ID: z6909331PROG: milk2LANG: C++*/#include <iostream>#include <cstdio>#include <fstream>#include <algorithm>#define maxn 5005using namespace std;struct node{ int start,end;}a[maxn];bool cmp(node x,node y){ if(x.start!=y.start) return x.start<y.start; else return x.end<y.end;}ofstream fout ("milk2.out");ifstream fin ("milk2.in");int main(){ int n; fin>>n; for(int i=0;i<n;i++) fin>>a[i].start>>a[i].end; sort(a,a+n,cmp); int max1=0,max2=0; int time=a[0].end-a[0].start; int maxend=a[0].end; for(int i=1;i<n;i++) { if(maxend>=a[i].start)//两区间相交,合并 { if(a[i].end>maxend) { time+=a[i].end-maxend; maxend=a[i].end; } } else//两区间不相交,重新计算 { time=a[i].end-a[i].start; maxend=a[i].end; } if(max1<time) max1=time; } if(time>max1) max1=time; time=0; maxend=a[0].end; for(int i=1;i<=n-1;i++) { if(maxend<a[i].start) { time=a[i].start-maxend; if(time>max2) max2=time; } if(a[i].end>maxend) maxend=a[i].end; } fout<<max1<<' '<<max2<<endl; return 0;}
- Milking Cows
- Milking Cows
- Milking Cows
- Milking Cows
- Milking Cows
- milking cows
- milking cows
- Milking Cows
- Milking Cows
- Milking Cows
- Milking Cows
- Milking cows
- Milking Cows
- Milking Cows
- Milking Cows
- Milking Cows
- Chap1 Milking Cows
- Milking Cows【转帖】
- 系统默认列表模版信息
- Meta programming中的define method
- linux socket网络编程(二)
- The Janki Method Shortens the Time It’ll Take You to Learn to Code
- [CFString release]: message sent to deallocated instance问题
- Milking Cows
- Java可变参数类型与重写,重载的讨论
- BES 5上如何remove wipe远程擦除BlackBerry手机
- 位置敏感哈希Locality-Sensitive Hashing
- shell实例
- ADOPENKEYSET ADOPENDYNAMIC
- 7z使用中,关于解压时的覆盖提示和子文件夹解压失败处理方法
- webrtc的scoped_ptr
- android_时间格式