奶牛玩杂技

传送门

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

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

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

我们假设前面的奶牛总重为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('\n')

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\n",ans);
    return 0;
}

 

posted @ 2018-10-25 22:06  CaptainLi  阅读(278)  评论(0编辑  收藏  举报