奶牛玩杂技

传送门

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

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

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

我们假设前面的奶牛总重为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;
}

 

转载于:https://www.cnblogs.com/captain1/p/9853256.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值