POJ - 3045(贪心)

这道题真是给我弄yue了。

本来是在一个二分比赛中看到了这道题,然后一直在想二分,想了好久也没能想出来该咋做,看了大佬的题解才知道了贪心去做更加地好
首先就是要按照w+s从大到小进行一个排序
先验证一下正确性
设 牛一 w1 s1 牛二 w2 s2 ,假设牛一上面所有牛的体重为sum,最优状态下牛一在牛二上面,可以得到
牛一的危险性a=sum-s1,牛二的危险性b=sum+w1-s2.
交换两牛位置后
牛一的危险性a’=sum+w2-s1,牛二的危险性b’=sum-s2.
可以得到sum(a,b)<=sum(a’,b’).
化简一下可以得到 w1+s1<=w2+s2;

这道题有个最离谱的点就是,最上面那头牛也有自己的危险性(一定是一个负数).

#include<iostream>
#include<string.h>
#include<algorithm>
#include<stdio.h>
#include<cmath>
#include<queue>
#include<stack>
#include<map>
#include<set>
#include<vector>
#define x first
#define y second
#pragma comment(linker, "/STACK:102400000,102400000")
#define int long long
typedef long long ll;
const double PI=3.14159265359;
using namespace std;
const int N=5e4+10;
struct point {
	int w,s;
}a[N];
int n;
bool cmp(point a,point b){
	return a.w+a.s>b.s+b.w;
}
int sum=0;
int check(){
	int temp=sum;
	temp-=a[1].w;
	int  ans=temp-a[1].s;
	for(int i=2;i<=n;i++){
		temp-=a[i].w;
		int res=temp-a[i].s;
		ans=max(ans,res);
	}
	return ans;
}
signed main()
{
	scanf("%lld",&n);
	for(int i=1;i<=n;i++){
		scanf("%lld%lld",&a[i].w,&a[i].s);
		sum+=a[i].w;
	}
	sort(a+1,a+1+n,cmp);
    int l=check();
	printf("%lld",l);
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值