标签:poj
传送门
大意:有N头牛叠罗汉,对于每头牛定义一个难受值D,D等于在它上面的所有奶牛的体重减去它的力量。
拿到这道题,感觉无从下手啊,贪力量,可是体重就不能保证上面的最好了。正解其实是要贪每头牛的力量加上体重,和大的放在下面。
证明:
-
-
-
-
显然我们可以得到
主要是开拓思维的题,代码不难写
#include<cstdio>
#include<algorithm>
using namespace std;
int n;
struct P
{
int w, l;
bool operator < (const P rhs) const
{
return w+l<rhs.w+rhs.l;
}
}a[50005];
int main()
{
scanf("%d", &n);
for(int i = 1; i <= n; i ++)
scanf("%d%d", &a[i].w, &a[i].l);
sort(a + 1, a + n + 1);
int sum = 0, ans = -0x3f3f3f3f;
for(int i = 1; i <= n; i ++)
{
ans = max(ans, sum - a[i].l);
sum += a[i].w;
}
printf("%d\n", ans);
return 0;
}
版权声明:请随意转载O(∩_∩)O
标签:poj
原文地址:http://blog.csdn.net/gengmingrui/article/details/47684101