usaco1.2.1 Milking Cows
来源:互联网 发布:舒乐安定 淘宝 编辑:程序博客网 时间:2024/06/08 19: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 integer, NLines 2..N+1:Two non-negative integers less than 1,000,000, respectively 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
二. 分析
三. 代码
USER: Qi Shen [maxkibb3]TASK: milk2LANG: C++Compiling...Compile: OKExecuting... Test 1: TEST OK [0.000 secs, 4276 KB] Test 2: TEST OK [0.000 secs, 4276 KB] Test 3: TEST OK [0.000 secs, 4276 KB] Test 4: TEST OK [0.000 secs, 4276 KB] Test 5: TEST OK [0.000 secs, 4276 KB] Test 6: TEST OK [0.000 secs, 4276 KB] Test 7: TEST OK [0.000 secs, 4276 KB] Test 8: TEST OK [0.054 secs, 4276 KB]All tests OK.YOUR PROGRAM ('milk2') WORKED FIRST TIME! That's fantastic-- and a rare thing. Please accept these special automatedcongratulations.
/*ID:maxkibb3PROG:milk2LANG:C++*/#include<cstdio>#include<algorithm>using namespace std;struct seg { int s, e; bool operator < (const seg &obj) const { return s < obj.s; }};const int MAX = 5005;int n, cnt;long long ans1, ans2;seg a[MAX], ans[MAX];void float_up(int idx, int _s, int _e) { if(idx == -1) { seg new_ele; new_ele.s = _s, new_ele.e = _e; ans[cnt++] = new_ele; } else { if(_s < a[idx].s) { float_up(idx - 1, _s, min(_e, a[idx].s)); } if(_e > a[idx].e) { float_up(idx - 1, max(_s, a[idx].e), _e); } }}int main() { freopen("milk2.in", "r", stdin); freopen("milk2.out", "w", stdout); scanf("%d", &n); for(int i = 0; i < n; i++) { scanf("%d%d", &a[i].s, &a[i].e); float_up(i - 1, a[i].s, a[i].e); } sort(ans, ans + cnt); long long pre_len = 0; for(int i = 0; i < cnt; i++) { if(i == 0) { pre_len = ans[i].e - ans[i].s; if(pre_len > ans1) { ans1 = pre_len; } } else { if(ans[i].s == ans[i - 1].e) { pre_len += ans[i].e - ans[i].s; if(pre_len > ans1) { ans1 = pre_len; } } else { pre_len = ans[i].e - ans[i].s; if(pre_len > ans1) { ans1 = pre_len; } if(ans[i].s - ans[i - 1].e > ans2) { ans2 = ans[i].s - ans[i - 1].e; } } } } printf("%lld %lld\n", ans1, ans2); return 0;}
- USACO1.2.1 Milking Cows
- usaco1.2.1 Milking Cows
- USACO1.2.1 Milking Cows (milk2)
- USACO1.2.1 Milking Cows (milk2)
- USACO1.2.1 Milking Cows(挤牛奶)
- USACO1.2.1 Milking Cows 给奶牛挤奶
- USACO1.2 Milking Cows
- USACO1.2--Milking Cows
- USACO1.2 挤牛奶Milking Cows
- [USACO1.2]挤牛奶Milking Cows
- 【USACO1.2_1】★Milking Cows 挤牛奶
- 洛谷 P1204 [USACO1.2]挤牛奶Milking Cows
- 洛谷 P1204 [USACO1.2] 挤牛奶Milking Cows
- 【洛谷P1204】【USACO1.2】挤牛奶Milking Cows
- 【模拟】洛谷 P1204 [USACO1.2]挤牛奶Milking Cows
- 洛谷 1204 [USACO1.2]挤牛奶Milking Cows
- [USACO1.2]挤牛奶Milking Cows 差分
- Milking Cows
- FTP+Nginx图片服务器(整合)
- java的native方法
- linux环境安装配置VNC
- Matlab中读取txt文件的几种方法
- Android AlertDialog
- usaco1.2.1 Milking Cows
- hiho一下 第110周 floyd + 贪心 + 倍增
- C语言中,malloc和free深入理解
- 插入排序——C/C++实现
- 文章标题
- java集合(TreeSet自然排序)
- ActiveX控件(MFC篇)
- Linux下的C语言编程——sqlite3实现停车场管理系统
- NOIP2012 Day1T1Vigenère 密码 P1079