感觉为什么自己停课了,还是和大家都差不多水平呢emmmm,最后十多天希望能进步吧
对于一头牛(i),他的压扁指数为(left(sumlimits_{j=1}^{i-1} w_j ight)-power_i)
思考怎样放牛才能更优呢???
现在有两头牛(i),(j)
如果(i)在(j)上面((j=i+1)),那么(i)的压扁指数为(left(sumlimits_{a=1}^{i-1} w_a ight)-power_i),(b)的压扁指数为(left(sumlimits_{a=1}^{i-1} w_a ight)+w_i-power_{j}),那么答案就是(max(W-power_i,W+w_i-power_j))
如果(j)早(i)上面((i=j+1)),那么(j)的压扁指数为(left(sumlimits_{a=1}^{j-1} w_a ight)-power_j),(b)的压扁指数为(left(sumlimits_{a=1}^{j-1} w_a ight)+w_j-power_{i}),那么答案就是(max(W-power_j,W+w_j-power_i))
如果想要第一种情况更优那么就一定是(max(W-power_i,W+w_i-power_j))<(max(W-power_j,W+w_j-power_i))
显然(W-power_i>W+w_j-power_i)
(quadquad)(W-power_j<W-power_j+w_i)
那么问题就在于让(W+w_i-power_j<W+w_j-power_i)
变形一下:(w_i+power_i<w_j+power_j)时,(i)在上面,排序后统计答案就ok
代码:
#include <iostream>
#include <cstdio>
#include <algorithm>
using namespace std;
int read(){
int x=1,a=0;char ch=getchar();
while(ch<'0'||ch>'9'){if (ch=='-') x=-1;ch=getchar();}
while(ch>='0'&&ch<='9'){a=a*10+ch-'0';ch=getchar();}
return x*a;
}
const int maxn=5e4+10;
int n,sum[maxn];
struct node{
int a,b;
}num[maxn];
bool cmp(node x,node y){
return x.a+x.b<y.a+y.b;
}
int main(){
n=read();
int ans=-1e9;
for (int i = 1;i <= n;i++) num[i].a=read(),num[i].b=read();
sort(num+1,num+n+1,cmp);
for (int i = 1;i <= n;i++){
sum[i]=sum[i-1]+num[i].a;
ans=max(ans,sum[i-1]-num[i].b);
}
printf("%d
",ans);
return 0;
}