Farmer John's N (1 <= N <= 50,000) cows (numbered 1..N) are planning to run away and join the circus. Their hoofed feet prevent them from tightrope walking and swinging from the trapeze (and their last attempt at firing a cow out of a cannon met with a dismal failure). Thus, they have decided to practice performing acrobatic stunts.
The cows aren't terribly creative and have only come up with one acrobatic stunt: standing on top of each other to form a vertical stack of some height. The cows are trying to figure out the order in which they should arrange themselves ithin this stack.
Each of the N cows has an associated weight (1 <= W_i <= 10,000) and strength (1 <= S_i <= 1,000,000,000). The risk of a cow collapsing is equal to the combined weight of all cows on top of her (not including her own weight, of course) minus her strength (so that a stronger cow has a lower risk). Your task is to determine an ordering of the cows that minimizes the greatest risk of collapse for any of the cows.
The cows aren't terribly creative and have only come up with one acrobatic stunt: standing on top of each other to form a vertical stack of some height. The cows are trying to figure out the order in which they should arrange themselves ithin this stack.
Each of the N cows has an associated weight (1 <= W_i <= 10,000) and strength (1 <= S_i <= 1,000,000,000). The risk of a cow collapsing is equal to the combined weight of all cows on top of her (not including her own weight, of course) minus her strength (so that a stronger cow has a lower risk). Your task is to determine an ordering of the cows that minimizes the greatest risk of collapse for any of the cows.
* Line 1: A single line with the integer N.
* Lines 2..N+1: Line i+1 describes cow i with two space-separated integers, W_i and S_i.
* Lines 2..N+1: Line i+1 describes cow i with two space-separated integers, W_i and S_i.
* Line 1: A single integer, giving the largest risk of all the cows in any optimal ordering that minimizes the risk.
3 10 3 2 5 3 3
2
OUTPUT DETAILS:
Put the cow with weight 10 on the bottom. She will carry the other two cows, so the risk of her collapsing is 2+3-3=2. The other cows have lower risk of collapsing.
Put the cow with weight 10 on the bottom. She will carry the other two cows, so the risk of her collapsing is 2+3-3=2. The other cows have lower risk of collapsing.
题意:有n头牛,叠起来,每头牛都有自己的体重和力量,风险值就是自己上面的牛的总重减去自己的力量。求这n头牛叠起来风险值最小是多少。
思路:这个题当时看了好久都没懂是什么意思。到底是什么牛放在最下面。也不知道是怎么排序。题意理解的有点偏差。后来结束后看了看题解。才明白,这n头牛,对于没一头牛而言,将它上面的所有的牛看做一个整体,求出总体重来,然后到第i头牛的危险值是sum - w[i] - s[i],而要想这个值最小,则取w[i] + s[i] 最大,所以按照w+s的大小排序。
心得:唉还是题意理解上有问题。。。。。觉得心累。。。以后还应该多思考思考。。。
源代码:
/*
Cow Acrobats
*/
#include <iostream>
#include <cstring>
#include <algorithm>
#include <stdio.h>
#include <queue>
#include <iomanip>
#include <string>
using namespace std;
struct W
{
long long weight,s;
}w[100000];
long long sum[100000];
bool cmp (W a,W b)
{
return a.weight + a.s > b.weight + b.s; //按照weight + s的顺序排序
}
int main()
{
long long n;
scanf("%lld",&n);
for (long long i = 1; i <= n; i++)
{
scanf("%lld%lld",&w[i].weight,&w[i].s);
}
sort(w + 1,w + 1 + n,cmp);
memset(sum,0,sizeof(sum));
for (long long i = 1; i <= n; i++)
sum[i] = sum[i - 1] + w[i].weight;
long long ant = -99999999999; //刚开始这个地方wa,数据不够大。又多加了几个9;题解上可以用-0x3f3f3f来代替。
for (long long i = 1; i <= n; i++)
{
long long temp = sum[n] - sum[i];
temp -= w[i].s;
ant = max (temp,ant);
}
printf("%lld\n",ant);
return 0;
}