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
  • 相关阅读:
    浅谈线段树
    并查集最简单讲解
    mysql索引原理深度解析
    算法、数据结构可视化
    php算法题---对称的二叉树
    php算法题---连续子数组的最大和
    Jsoup一个简短的引论——采用Java抓取网页数据
    宏定义详细信息
    解决java.sql.SQLException: ORA-01789: query block has incorrect number of result columns
    BZOJ 2435 NOI2011 道路建设 BFS/DFS
  • 原文地址:https://www.cnblogs.com/rausen/p/4461589.html
Copyright © 2011-2022 走看看