码迷,mamicode.com
首页 > 其他好文 > 详细

BZOJ1727 [Usaco2006 Open]The Milk Queue 挤奶队列

时间:2015-04-27 23:40:53      阅读:552      评论:0      收藏:0      [点我收藏+]

标签:

贪心。。。我怎么不会QAQ【捂脸熊】

对于1、2两头牛,如果1号牛要排在2号牛前面才能时间更少,则

$$max(A_1 + B_1 + B_2, \ A_1 + A_2 + B_2) \le max(A_2 + B_2 + B_1, \ A_2 + A_1 + B_1) \\ \Leftrightarrow min(A_1, \ B_2) < min(A_2, \ B_1)$$

于是排序以后模拟一下就好了

 

技术分享
 1 /**************************************************************
 2     Problem: 1727
 3     User: rausen
 4     Language: C++
 5     Result: Accepted
 6     Time:48 ms
 7     Memory:1156 kb
 8 ****************************************************************/
 9  
10 #include <cstdio>
11 #include <algorithm> 
12 using namespace std;
13 const int N = 3e4 + 5;
14 const int inf = 1e9;
15  
16 inline int read();
17  
18 struct data {
19     int a, b, k;
20      
21     inline void get() {
22         a = read(), b = read();
23         if (a < b) k = a;
24         else k = inf - b;
25     }
26      
27     inline bool operator < (const data &d) const {
28         return k < d.k;
29     }
30 } a[N];
31  
32 int n, ans;
33  
34 int main() {
35     int i, t1, t2;
36     n = read();
37     for (i = 1; i <= n; ++i) a[i].get();
38     sort(a + 1, a + n + 1);
39     for (i = 1, t2 = 0; i <= n; ++i) t2 += a[i].b;
40     for (i = 1, t1 = ans = 0; i <= n; ++i) {
41         t1 += a[i].a;
42         ans = max(ans, t1 + t2);
43         t2 -= a[i].b;
44     }
45     printf("%d\n", ans);
46     return 0;
47 }
48  
49 inline int read() {
50     static int x;
51     static char ch;
52     x = 0, ch = getchar();
53     while (ch < 0 || 9 < ch)
54         ch = getchar();
55     while (0 <= ch && ch <= 9) {
56         x = x * 10 + ch - 0;
57         ch = getchar();
58     }
59     return x;
60 }
View Code

 

BZOJ1727 [Usaco2006 Open]The Milk Queue 挤奶队列

标签:

原文地址:http://www.cnblogs.com/rausen/p/4461589.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!