poj 3045 Cow Acrobats 【二分】

来源:互联网 发布:爱思助手mac版 编辑:程序博客网 时间:2024/05/20 03:37

Cow Acrobats
Time Limit: 1000MS Memory Limit: 65536KTotal Submissions: 3678 Accepted: 1423

Description

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.

Input

* 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. 

Output

* Line 1: A single integer, giving the largest risk of all the cows in any optimal ordering that minimizes the risk.

Sample Input

310 32 53 3

Sample Output

2

Hint

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.


题意:有N头牛,已经给出每头牛的体重和力气。现在N头牛要按顺序排列起来,其中第i头牛的承担风险为前i-1头牛的体重和减去它的力气。要求你任意选择一种序列,使得这N头牛承担的最大风险最小化。


思路:二分。对任意的两头牛i和i+1考虑二者的状态,设前i-1头牛的总体重为sum。那么排序方式为

sum + num[i].w - num[i+1].s < sum + num[i+1].w - num[i].s;

化简后为 num[i].w - num[i+1].s < num[i+1].w - num[i].s。

sort下,二分答案就可以了。


AC代码:


#include <cstdio>#include <cstring>#include <cmath>#include <cstdlib>#include <algorithm>#include <queue>#include <stack>#include <map>#include <set>#include <vector>#define INF 0x3f3f3f#define eps 1e-8#define MAXN (50000+1)#define MAXM (100000)#define Ri(a) scanf("%d", &a)#define Rl(a) scanf("%lld", &a)#define Rf(a) scanf("%lf", &a)#define Rs(a) scanf("%s", a)#define Pi(a) printf("%d\n", (a))#define Pf(a) printf("%.2lf\n", (a))#define Pl(a) printf("%lld\n", (a))#define Ps(a) printf("%s\n", (a))#define W(a) while(a--)#define CLR(a, b) memset(a, (b), sizeof(a))#define MOD 1000000007#define LL long long#define lson o<<1, l, mid#define rson o<<1|1, mid+1, r#define ll o<<1#define rr o<<1|1using namespace std;struct Node{    int w, s;};Node num[MAXN];bool cmp(Node a, Node b){    return a.w - b.s < b.w - a.s;}int sum[MAXN];bool judge(int o, int n){    for(int i = 1; i <= n; i++)        if(sum[i-1] - num[i].s > o)            return false;    return true;}int main(){    int n;    while(Ri(n) != EOF)    {        for(int i = 1; i <= n; i++)            Ri(num[i].w), Ri(num[i].s);        sort(num+1, num+n+1, cmp);        sum[0] = 0;        int l = INF, r = 0;        for(int i = 1; i <= n; i++)        {            l = min(sum[i-1] - num[i].s, l);            r = max(sum[i-1] - num[i].s, r);            sum[i] = sum[i-1] + num[i].w;        }        int ans;        while(l <= r)        {            int mid = (l + r) >> 1;            if(judge(mid, n))            {                ans = mid;                r = mid-1;            }            else                l = mid+1;        }        Pi(ans);    }    return 0;}




0 0
原创粉丝点击