博客
关于我
Codeforces Global Round 13 C. Pekora and Trampoline(思维/差分)
阅读量:327 次
发布时间:2019-03-04

本文共 1827 字,大约阅读时间需要 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/

你可能感兴趣的文章
MySQL及navicat for mysql中文乱码
查看>>
MySqL双机热备份(二)--MysqL主-主复制实现
查看>>
MySQL各个版本区别及问题总结
查看>>
MySql各种查询
查看>>
mysql同主机下 复制一个数据库所有文件到另一个数据库
查看>>
mysql启动以后会自动关闭_驾照虽然是C1,一直是开自动挡的车,会不会以后就不会开手动了?...
查看>>
mysql启动和关闭外键约束的方法(FOREIGN_KEY_CHECKS)
查看>>
Mysql启动失败解决过程
查看>>
MySQL启动失败:Can't start server: Bind on TCP/IP port
查看>>
mysql启动报错
查看>>
mysql启动报错The server quit without updating PID file几种解决办法
查看>>
MySQL命令行登陆,远程登陆MySQL
查看>>
mysql命令:set sql_log_bin=on/off
查看>>
mySQL和Hive的区别
查看>>
MySQL和Java数据类型对应
查看>>
mysql和oorcale日期区间查询【含左右区间问题】
查看>>
MYSQL和ORACLE的一些操作区别
查看>>
mysql和redis之间互相备份
查看>>
MySQL和SQL入门
查看>>
mysql在centos下用命令批量导入报错_Variable ‘character_set_client‘ can‘t be set to the value of ‘---linux工作笔记042
查看>>