推公式
先给出结论:
按照W[i]+S[i]
从小到大的顺序排,最大的危险系数一定是最小的。
证明思路:
贪心得到的答案 \(\ge\) 最优解
贪心得到的答案 \(\le\) 最优解
#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 w, s;
cin >> w >> s;
cow[i] = {w + s, w};
}
sort(cow, cow + n);
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;
}
cout << res << endl;
return 0;
}
标签:PII,cow,55,res,sum,笔记,int,算法,include
From: https://www.cnblogs.com/I-am-Sino/p/17045181.html