题解 POJ3045 【Cow Acrobats】

题目链接:Link

Problem

Solution

首先,我们可以发现:交换相邻的两头奶牛,对其它奶牛的危险值没有影响。设这两头奶牛分别为\((s_1,w_1),(s_2,w_2)\),之前所有奶牛的体重和为\(sw\),则1放在2前面更优意味着:

\[max(sw-s_1,sw+w_1-s_2) < max(sw-s_2,sw+w_2-s1) \]

继续化简:

\[max(-s_1,w_1-s_2) < max(-s_2,w_2-s1) \]

两边同加 $ s_1 + s_2 $:

\[max(s_2,w_1+s_1) < max(s_1,w_2+s_2) \]

由于$ s_1,s_2,w_1,w_2 $都为正整数,所以 $ s_2 < max(s_1,w_2+s_2) , s_1 < max(w_1+s_1) $,因此 $ s_2,s_1 $对不等式是否成立没有影响,所以:

\[w_1+s_1 < w_2+s_2 \]

然后就可以套用临项交换的模板了。

Code

#include<cstdio>
#include<algorithm>
using namespace std;
typedef long long LL;
const int maxn=50005;
struct COW { int w,s; };
int n; COW co[maxn];
inline bool operator<(const COW &a,const COW &b) { return a.w+a.s<b.w+b.s; }
int main()
{
#ifdef local
	freopen("pro.in","r",stdin);
#endif
	scanf("%d",&n);
	for(int i=0;i<n;i++) scanf("%d%d",&co[i].w,&co[i].s);
	sort(co,co+n);
	LL sum=0,res=-1e9;
	for(int i=0;i<n;i++)
	{
		if(sum-co[i].s>res) res=sum-co[i].s;
		sum+=co[i].w;
	}
	printf("%lld\n",res);
	return 0;
}
posted @ 2019-08-19 22:11  happyZYM  阅读(171)  评论(0编辑  收藏  举报