题解 P2123 【皇后游戏】
做法
令
先按
求出
证明
偏序关系
令
(即按上述做法重载
因为
接下来只需要证明若
何时更优
注意到
化简:
两边同时减去
乘以
拆解成逻辑表达式:
在此相遇
至此,我们只需要验证
代码
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 50000;
struct Secretary
{
int a, b, d;
} sty[N + 5];
inline bool cmp(const Secretary &x, const Secretary &y)
{
if (x.d != y.d)
return x.d < y.d;
if (x.d <= 0)
return x.a < y.a;
else
return x.b > y.b;
}
int main()
{
int T;
scanf("%d", &T);
while (T--)
{
int n;
scanf("%d", &n);
for (int i = 1; i <= n; ++i)
{
scanf("%d%d", &sty[i].a, &sty[i].b);
sty[i].d = sty[i].a - sty[i].b;
if (sty[i].d)
sty[i].d /= abs(sty[i].d);
}
sort(sty + 1, sty + n + 1, cmp);
ll c = 0, s = 0;
for (int i = 1; i <= n; ++i)
{
s += sty[i].a;
c = max(c, s) + sty[i].b;
}
printf("%lld\n", c);
}
}