ACWing算法基础课-耍杂技的牛

阅读 37

2022-02-16

1. 耍杂技的牛

题目链接: https://www.acwing.com/problem/content/description/127/


参考文献

C++ 代码

#include <iostream>
#include <algorithm>

using namespace std;


typedef pair<int, int> PII;

const int N = 50010;

PII cow[N];

int n;

int main()
{
    scanf("%d", &n);
    for (int i=0; i<n; i++) 
    {
        int w, s;
        scanf("%d%d", &w, &s);
        
        cow[i] = {w+s, w};
    }
    
    
    // 按照 w+s 从小到大排序
    sort(cow, cow+n);
    
    // res 记录风险值, sum记录重量
    int res = -2e9, sum=0;
    for (int i=0; i<n; i++)
    {
        int w = cow[i].second, s = cow[i].first - w;
        
        res = max(res, sum-s);
        
        sum += w;
    }
    
    printf("%d\n", res);
    
    return 0;
    
    
}

精彩评论(0)

0 0 举报