1 条题解

  • 0
    @ 2017-08-23 14:25:14

    ---------------------------------------------AC code---------------------------------------------

    #include<cstdio>
    #include<algorithm>
    
    #ifdef WIN32
    #   define outl "%I64d"
    #else
    #   define outl "%lld"
    #endif
    
    using namespace std;
    
    inline int read(){
        int x = 0;
        bool f = 1;
        char ch = getchar();
        while(ch < '0' || ch > '9'){
            if(ch == '-')   f = 0;
            ch = getchar();
        }
        while(ch >= '0' && ch <= '9'){
            x = x * 10 + ch - '0';
            ch = getchar();
        }
        return f ? x : -x;
    }
    
    typedef long long LL;
    const int MAXN = 100005;
    int CASES, n;
    LL ans, remain;
    struct P{
        int a, b, c;//a:die; b:involve; c:survive;
    }p[MAXN];
    
    bool cmp(P x, P y){
        return x.c > y.c;
    }
    
    int main(){
    //  freopen("army.in", "r", stdin);
    //  freopen("army.out", "w", stdout);
        CASES = read();
        while(CASES--){
            n = read();
            for(int i = 1; i <= n; i++) p[i].a = read(), p[i].b = read(), p[i].c = p[i].b - p[i].a;
            sort(p+1, p+n+1, cmp);
            ans = 0, remain = 0;
            for(int i = 1; i <= n; i++){
                if(remain < p[i].b){
                    ans += p[i].b - remain;
                    remain = (LL)p[i].c;
                }
                else{
                    remain -= (LL)p[i].b;
                    remain += (LL)p[i].c;
                }
            }
            printf(outl"\n", ans);
        }
        return 0;
    }
    
  • 1

信息

难度
(无)
分类
贪心 点击显示
标签
递交数
0
已通过
0
通过率
?
上传者