usaco1.2.1 Milking Cows
2016-11-25 22:01
295 查看
一. 原题
Milking CowsThree 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, N |
Lines 2..N+1: | Two non-negative integers less than 1,000,000, respectively the starting and ending time in seconds after 0500 |
SAMPLE INPUT (file milk2.in)
3 300 1000 700 1200 1500 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: milk2 LANG: C++ Compiling... Compile: OK Executing... 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 automated
congratulations.
AC代码:
/* ID:maxkibb3 PROG:milk2 LANG: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 (milk2)
- USACO 1.2.1 Milking Cows
- USACO Section 1.2.1 Milking Cows
- USACO之Section1.2.1 Milking Cows [其他]
- USACO1.2.1 Milking Cows(挤牛奶)
- USACO 1.2.1 Milking Cows(milk2)
- USACO1.2.1 Milking Cows (milk2)
- USACO 1.2.1 Milking Cows
- USACO1.2.1--Milking Cows
- USACO 1.2.1 - MIlking Cows(模拟)
- USACO 1.2.1 Milking Cows
- [USACO 1.2.1] Milking Cows
- usaco 1.2.1 Milking Cows 题解
- USACO 1.2.1 Milking Cows
- USACO1.2.1 Milking Cows 给奶牛挤奶
- usaco 1.2.1 Milking Cows 挤牛奶
- usaco 1.2.1 Milking Cows
- USACO 1.2.1 Milking Cows 挤牛奶
- USACO 1.2.1 Milking Cows
- Usaco 1.2.1 挤牛奶(Milking Cows)