zoukankan      html  css  js  c++  java
  • 奶牛玩杂技

    传送门

    这道题是很有趣的贪心题……

    一开始可能我们会无从下手……因为直接看是看不出来啥的,只按照重量或者承受能力也是不行的。

    我们考虑一个新套路,选择两头奶牛,把他们进行交换,看怎么样贡献会比较大。

    我们假设前面的奶牛总重为w,第一头奶牛重为w1,承受能力为s1,第二头同理为w2,s2,那么,第一头的压扁程度是w-s1,第二头的是w+w1-s2,而交换之后,第一头的是w-s2,第二头的是w+w2-s1.

    我们发现,w+w2-s1必然比w-s1大,而w+w1-s2必然比w-s2大,所以我们只要比较w+w2-s1和w+w1-s2即可,移项之后发现,w+s更大的那只奶牛放在下面更优,所以我们按这个指标排序计算即可。

    看一下代码。

    #include<cstdio>
    #include<algorithm>
    #include<cstring>
    #include<iostream>
    #include<cmath>
    #include<set>
    #include<queue>
    #define rep(i,a,n) for(int i = a;i <= n;i++)
    #define per(i,n,a) for(int i = n;i >= a;i--)
    #define enter putchar('
    ')
    
    using namespace std;
    typedef long long ll;
    const int M = 200005;
    const int N = 1005;
    const int INF = 2147483647;
    
    int read()
    {
        int ans = 0,op = 1;
        char ch = getchar();
        while(ch < '0' || ch > '9')
        {
        if(ch == '-') op = -1;
        ch = getchar();
        }
        while(ch >= '0' && ch <= '9')
        {
        ans *= 10;
        ans += ch - '0';
        ch = getchar();
        }
        return ans * op;
    }
    
    struct cow
    {
        int w,s;
        bool operator < (const cow &g) const
        {
            return w + s < g.w + g.s;
        }
    }c[M];
    
    int n,ans = -INF,sum;
    
    int main()
    {
        n = read();
        rep(i,1,n) c[i].w = read(),c[i].s = read();
        sort(c+1,c+1+n);
        rep(i,1,n) ans = max(ans,sum - c[i].s),sum += c[i].w;
        printf("%d
    ",ans);
        return 0;
    }
  • 相关阅读:
    deleted
    deleted
    HDU
    FZU 1901 Period II(KMP中的next)题解
    HDU 3374 String Problem(最大最小表示+KMP)题解
    HDU 4300 Clairewd’s message(扩展KMP)题解
    POJ 2923 Relocation(状压DP+01背包)题解
    HDU 4272 LianLianKan (状压DP+DFS)题解
    POJ 1185 炮兵阵地(状压DP)题解
    POJ
  • 原文地址:https://www.cnblogs.com/captain1/p/9853256.html
Copyright © 2011-2022 走看看