贪心 :按重量值和强壮值从小到大排序即为最优解
牛 交换前 交换后
i −s(i) w(i+1)−s(i)
i+1 w(i)−s(i+1) −s(i+1)
比较即可
AC代码
#include <iostream>
#include <algorithm>
using namespace std;
#define N 50001
typedef pair < int , int > PII;
int n;
PII Q[N];
int main()
{
cin >> n;
for (int i = 1; i <= n; i++)
{
int w , s;
cin >> w >> s;
Q[i] = { w + s , w };
}
sort(Q + 1,Q + 1 + n);
int ans = -0x3f3f3f3f,sum = 0;
for (int i = 1; i <= n; i++)
{
int s = Q[i].first - Q[i].second;
ans = max(ans , sum - s);
sum += Q[i].second;
}
cout << ans;
return 0;
}