推公式——耍杂技的牛

由图可知,只要存在一个逆序,把他们交换一下,最大风险值就会降低,答案更优,因此最优解是按照wi+si从小到大升序排列,顺次计算每头牛的危险系数,最大值即是答案。

cpp 复制代码
#include <iostream>
#include <algorithm>

using namespace std;

typedef pair<int, int> PII;

const int N = 50010;

int n;
PII cow[N];

int main()
{
    cin>>n;
    for (int i = 0; i < n; i ++ )
    {
        int s, w;
        cin>>w>>s;
        cow[i] = {w + s, w};
        //pair是按照first为第一关键字排序,以second为第二关键字排序
    }

    sort(cow, cow + n);//升序

    int res = -2e9, sum = 0;//res代表风险值,sum当前这头牛上面所有牛的重量
    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;
    }

    cout<<res;

    return 0;
}
相关推荐
CV-Climber33 分钟前
检索技术的实际应用
人工智能·算法
hhzz1 小时前
Tiger AI Platform平台中增加人脸识别功能
图像处理·人工智能·算法·计算机视觉·大模型
从零开始的代码生活_2 小时前
C++ 继承详解:访问控制、对象模型、菱形继承与设计取舍
开发语言·c++·后端·学习·算法
TsingtaoAI2 小时前
3D高斯泼溅技术发展及其在具身智能领域的应用综述
人工智能·算法·ai·具身智能·高斯泼溅
atunet2 小时前
关于图算法中的连通分量检测与最小割问题7
算法
心运软件3 小时前
银行客户流失预测(Python 完整实战)
算法
邪神与厨二病3 小时前
牛客周赛 Round 153
python·算法
qizayaoshuap5 小时前
# ❌ 井字棋 — 鸿蒙ArkTS Minimax AI算法与博弈系统设计
人工智能·算法·华为·harmonyos
皓月斯语6 小时前
B3842 [GESP202306 三级] 春游 题解
数据结构·c++·算法·题解
atunet6 小时前
树状结构在查询优化中的作用与实现细节7
算法