• POJ:3045-Cow Acrobats


    Cow Acrobats

    Time Limit: 1000MS Memory Limit: 65536K
    Total Submissions: 6253 Accepted: 2345

    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

    3
    10 3
    2 5
    3 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.


    解题心得:

    1. 有n头牛,每一只牛有重量和力量,现在n只牛叠罗汉,每只牛的风险等于他头上牛的重量总和减去这只牛的力量。问怎么排列,使最大风险牛的风险最小。有两种想法。
    2. 挑战上面说这是一个二分题,其实就是一个贪心啊,先假设每一只牛都放在最下面,算出风险,然后将风险最小的那个放在下面,这样风险大的就可以减去已经安放了风险小的牛的重量,这样得到的风险就会逐步减小,最后风险最大的就是最小的。
    3. 也可以这样想,假设有两只牛a和b,a的重量为w1,力量为p1,b的重量为w2,力量为p2,如果将a放在下面,a的风险就是p1-w2,如果将b放在下面,风险就是p2-w1。如果a的风险比b的风险更小就会得到一个关系式:p1-w2
    #include <stdio.h>
    #include <algorithm>
    #include <cstring>
    using namespace std;
    const int maxn =5e4+100;
    struct COW{
        int va,w;
        long long press;
    }cow[maxn];
    int n;
    
    bool cmp(COW a,COW b) {
        return a.press < b.press;
    }
    
    void init() {
        scanf("%d",&n);
        long long sum = 0;
        for(int i=0;i<n;i++) {
            scanf("%d%d",&cow[i].w,&cow[i].va);
            sum += cow[i].w;
        }
        for(int i=0;i<n;i++) {
            cow[i].press = sum - cow[i].w - cow[i].va;
        }
        sort(cow,cow+n,cmp);
    }
    
    long long get_oder() {
        long long cut = 0;
        long long ans = -10*(long long)0x7f7f7f7f;
        for(int i=0;i<n;i++) {
            cow[i].press = cow[i].press - cut;
            ans = max(ans,cow[i].press);
            cut += cow[i].w;
        }
        return ans;
    }
    
    int main() {
        init();
        long long ans = get_oder();
        printf("%lld
    ",ans);
        return 0;
    }

    第二种想法的代码:

    #include <stdio.h>
    #include <algorithm>
    #include <cstring>
    using namespace std;
    const int maxn =5e4+100;
    struct COW{
        int va,w;
    }cow[maxn];
    int n;
    
    bool cmp(COW a,COW b) {
        return a.va + a.w > b.va + b.w;
    }
    
    void init() {
        scanf("%d",&n);
        for(int i=0;i<n;i++) {
            scanf("%d%d",&cow[i].w,&cow[i].va);
        }
        sort(cow,cow+n,cmp);
    }
    
    long long get_oder() {
        long long sum = 0;
        long long Max = -10*(long long)0x7f7f7f7f7f;
        for(int i=n-1;i>=0;i--) {
            long long temp = sum - cow[i].va;
            sum += cow[i].w;
            Max = max(Max,temp);
        }
        return  Max;
    }
    
    int main() {
        init();
        long long ans = get_oder();
        printf("%lld
    ",ans);
        return 0;
    }
  • 相关阅读:
    poj 3026 Borg Maze
    poj2828 Buy Tickets
    poj3264 Balanced Lineup
    高精度运算
    poj1035 Spell checker
    poj2318 TOYS 点积叉积理解
    求两直线交点的一般做法
    C语言I博客作业05
    C语言I博客作业04
    C语言I博客作业07
  • 原文地址:https://www.cnblogs.com/GoldenFingers/p/9107123.html
Copyright © 2020-2023  润新知