推公式——耍杂技的牛

由图可知,只要存在一个逆序,把他们交换一下,最大风险值就会降低,答案更优,因此最优解是按照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;
}
相关推荐
早起CaiCai37 分钟前
【2023-01 OM】利用深度学习算法融合异质观测实现海洋场降尺度
人工智能·深度学习·算法
梦里寻码1 小时前
我用 NestJS + Vue3 写了一套完整的研发管理系统(58 表 / 229 API / 130 项断言,全开源
前端·后端·算法
猿人谷2 小时前
Surrogate Model:让进化算法学会“预测”,为什么它正在成为智能优化的重要技术?
后端·算法·架构
青少儿编程课堂3 小时前
蓝桥杯青少组图形化 STEMA 省考(报名倒计时·11/22开考)+ 全国青少年信息素养大赛 2026—2027赛季 + 电子学会图形化考级:校园华容道大闯关
c++·python·算法·bfs·信息学竞赛
在猴站学算法4 小时前
(《统计学习方法》(第2版) 李航)第2章 感知机算法
学习·算法·机器学习·统计学习
Rainmicro4 小时前
【无标题】
java·数据库·算法
NEFU_Jyj14 小时前
牛客周赛R168
算法
Navigator_Z1 天前
LeetCode //C - 1317. Convert Integer to the Sum of Two No-Zero Integers
c语言·算法·leetcode