zoukankan      html  css  js  c++  java
  • BZOJ1727 [Usaco2006 Open]The Milk Queue 挤奶队列

    贪心。。。我怎么不会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
    ", 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
    By Xs酱~ 转载请说明 博客地址:http://www.cnblogs.com/rausen
  • 相关阅读:
    用Margin还是用Padding?
    更优雅的清除浮动float方法
    清除浮动float (:after方法)
    px,em,rem
    load()方法
    PHP函数详解:call_user_func()使用方法
    移动端touch事件影响click事件以及在touchmove添加preventDefault导致页面无法滚动的解决方法
    Mysql开启远程连接方法
    mysql的字符串连接符
    php使用curl访问https返回无结果的问题
  • 原文地址:https://www.cnblogs.com/rausen/p/4461589.html
Copyright © 2011-2022 走看看