博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Codeforces Global Round 13 C. Pekora and Trampoline(思维/差分)
阅读量:324 次
发布时间:2019-03-04

本文共 1832 字,大约阅读时间需要 6 分钟。


题目大意

给出 n n n个蹦床,其中每个蹦床有一个弹力值 s i s_i si。设每个蹦床的位置为 i i i,那么这个蹦床可以向右跳的距离为 i + s i i + s_i i+si,且每跳一次蹦床的弹力值都会减一,直到 s i = 1 s_i=1 si=1便再也不会发生变化。每次可以任选一个蹦床出发跳跃,跳出后可能被多次弹到右边的其他蹦床,直到离开蹦床这次跳跃才算结束。问将所有蹦床的弹力值都减少为 1 1 1时的最少跳跃次数。

解题思路

首先不难想到越靠左边出发就会使总的次数减少,因为左边每个蹦床出发的跳跃总可以使右边的蹦床的弹力值免费减少,这启发我们只需要从左向右遍历每个蹦床。但是我们需要模拟向后跳跃的过程吗,显然不行,但是有一点可以想到,我们可以累积每个蹦床从前面白嫖的次数,即每个点对后面第一次产生的影响可以通过区间加法维护,既可以暴力也可以差分。

  • s i + i > n s_i + i>n si+i>n,只需要直接使 s i s_i si减小到 s i + i ≤ n s_i + i \leq n si+in
  • s i + i ≤ n s_i + i \leq n si+in,显然这个蹦床我们要出发 n − 1 n - 1 n1次才能使它的弹力值减少为1。

但是上述只是单纯考虑从每个蹦床出发的贡献 r e s res res,还有从前面白嫖的次数,没有考虑,设白嫖的次数为 c n t i cnt_i cnti

  • c n t i < r e s cnt_i < res cnti<res,那么直接累加答案 r e s − c n t i res - cnt_i rescnti即可。
  • c n t i > r e s cnt_i > res cnti>res,这时我们已经可以通过白嫖免费将这个蹦床弹力值减少为 1 1 1了,但是最容易忽略的一点,即使弹力值为 1 1 1它也会给下一个位置给予白嫖次数,因此要 c n t [ i + 1 ] + = c n t i − r e s cnt[i+1] += cnt_i - res cnt[i+1]+=cntires

最后扫一遍就能得出答案了。

#include 
using namespace std;typedef long long ll;#define ENDL "\n"const int maxn = 5e3 + 10;int a[maxn];ll cnt[maxn];int main() {
ios::sync_with_stdio(0), cin.tie(0), cout.tie(0); int t, n; cin >> t; while (t--) {
cin >> n; for (int i = 1; i <= n; i++) {
cin >> a[i]; cnt[i] = 0; } ll ans = 0; for (int i = 1; i <= n; i++) {
cnt[i] += cnt[i - 1]; ll res = 0; if (i + a[i] > n) {
res += i + a[i] - n; a[i] = n - i; } res += a[i] - 1; if (cnt[i] > res) cnt[i + 1] += cnt[i] - res, cnt[i + 2] -= cnt[i] - res; cnt[i + 2]++, cnt[i + a[i] + 1]--; //for (int j = i + 2; j <= i + a[i]; j++) cnt[j]++; ans += max(0LL, res - cnt[i]); } cout << ans << ENDL; } return 0;}

转载地址:http://fvqq.baihongyu.com/

你可能感兴趣的文章