usaco Milking Cows

题目链接:http://cerberus.delos.com:791/usacoprob2?a=bv3dg9ejwKm&S=milk2

这题目不是线段树,直接模拟

 1 /*
 2 ID: zypz4571
 3 LANG: C++
 4 TASK: milk2
 5 */
 6 #include <algorithm>
 7 #include <cstdio>
 8 #include <cstdlib>
 9 using namespace std;
10 struct Node {
11     int s, t;
12 }a[5002];
13 bool cmp(const Node &a, const Node &b)
14 {
15     return a.s < b.s;
16 }
17 int main(void)
18 {
19     freopen("milk2.in", "r", stdin);
20     freopen("milk2.out", "w", stdout);
21     int n; scanf("%d",&n);
22     for (int i = 0; i < n; scanf("%d%d",&a[i].s, &a[i].t),++i);
23     sort(a,a+n,cmp);
24     int Maxmilk=a[0].t-a[0].s, Maxidle=0, now = a[0].t, milk=Maxmilk, idle=0;
25     for (int i = 1; i < n; ++i) {
26         if (now >= a[i].t) continue;
27         if (now >= a[i].s) {
28             milk += (a[i].t - now), now = a[i].t, Maxmilk = max(Maxmilk, milk);
29         }
30         if (now < a[i].s) {
31             idle = a[i].s - now, Maxidle = max(Maxidle, idle), milk = a[i].t - a[i].s,
32             Maxmilk = max(milk, Maxmilk), now = a[i].t;
33         }
34     }
35     printf("%d %d\n", Maxmilk, Maxidle);
36 
37     return 0;
38 }

=_=开始想太多……

posted on 2013-07-14 18:03  aries__liu  阅读(209)  评论(0编辑  收藏  举报