AcWing 125. 耍杂技的牛

//按照wi+si从小到大的顺序排,结果一定最优,最大的危险系数一定是最小的
//类比于国王游戏 
#include <iostream>
#include <algorithm>
using namespace std;
typedef pair<int, int> PII;
const int N = 50010;
int n;
PII cow[N];
int main() {
    scanf("%d", &n);
    for (int i = 0; i < n; i ++ ) {
        int s, w;
        scanf("%d%d", &w, &s);
        cow[i] = {w + s, w};
    }
    sort(cow, cow + n);
    int res = -2e9, sum = 0;
    for (int i = 0; i < n; i ++ ) {
        int s = cow[i].first - cow[i].second, w = cow[i].second;
        res = max(res, sum - s);
        sum += w;
    }
    printf("%d\n", res);
    return 0;
}

 

 

上一篇:[Usaco2004Feb]Cow Marathon 树的直径


下一篇:DP五十题