题意:
一群牛在叠罗汉;
每头牛都有一个重量W和力量值X;
在叠的时候每头牛都有一个风险值R;
要你求总的风险值中最大的那个风险值R’;
理解:
其实一直都没能理解到意思;
然后手贱看了大神的思想,发现果然如此;
可以如此证明:
因为两头牛切换位置只对他们自己受影响;
因此,如果A在上,B在下,则有:
A: Ra = S + Wb - Xa;
B: Rb = S - Xb;
反之如下:
A: Ra = S - Xa;
B: Rb = S + Wa - Xb;
如果我们定义一方案好于而方案则有:
S + Wb - Xa < S + Wa - Xb;
则: Wa + Xa < Wb + Xb;
因此我们用上诉公式来排个序,之后计算下就完了。
代码如下:
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <vector>
#include <string>
#include <map>
#include <set>
using namespace std;
#define ll long long
#define P pair<ll, ll>
#define x first
#define y second
bool cmp(P p1, P p2)
{
return p1.x + p1.y > p2.x + p2.y; //排序公式
}
int main()
{
int n;
scanf("%d", &n);
vector<P> vec(n);
for (int i = 0; i < n; ++i)
{
scanf("%lld%lld", &vec[i].x, &vec[i].y);
}
sort(vec.begin(), vec.end(), cmp);
vector<ll> sum(n);
sum[n - 1] = 0;
for (int i = n - 2; i >= 0; --i)
{
sum[i] = sum[i + 1] + vec[i + 1].x;
}
ll ans = -10 * (ll)0x7fffffff; //小心答案为负
for (int i = 0; i < n; ++i)
{
ll tp;
tp = sum[i] - vec[i].y;
if (ans < tp) ans = tp;
}
printf("%lld\n", ans);
return 0;
}