【题解】P16993 [NWERC 2018] 倒霉下注 / Jinxed Betting
思路
贪心得出:一定不让 Julia 加分。对于一段长度为 \(x\) 且最长的一段数,为了保证 Julia 不获得分,每一次只能选择 \(\frac{x}{2}\) 个数进行修改,然后把后面的数进行修改。因此可以经过 \(\log x + 1\) 次操作可以将这 \(x\) 个数增加 \(\log x\),而且剩余的数增加 \(\log x + 1\)。所以直接维护一个数为 \(p_2\) 的右端点,每一次操作这个块后更新右端点即可。
代码
#include <bits/stdc++.h>
#define int long long
using namespace std;
const int MAXN = 1e5 + 5;
int n, ans = 0, tot = 1;
int p[MAXN], d[MAXN];
signed main() {
ios::sync_with_stdio(0);
cin.tie(0), cout.tie(0);
// freopen("lead.in","r",stdin);
// freopen("lead.out","w",stdout);
cin >> n;
for (int i = 1; i <= n; i++) cin >> p[i];
sort(p + 1, p + 1 + n, greater<int>());
for (int i = 1; i <= n; i++) d[i] = p[i] - p[i - 1];
ans -= d[3], d[3] = 0;
while (tot + 2 <= n && d[tot + 2] == 0) tot++;
while (tot + 2 <= n && d[2] + __lg(tot) < 0) {
int lg = __lg(tot);
int t = min(-d[tot + 2], (-d[2] - 1) / lg);
d[tot + 2] += t, d[2] += lg * t, ans += (1 + lg) * t;
while (tot + 2 <= n && d[tot + 2] == 0) tot++;
}
int lg = __lg(tot), x = -d[2];
ans += x / lg * (lg + 1) + x % lg;
cout << ans << "\n";
return 0;
}

浙公网安备 33010602011771号