博客
关于我
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/

你可能感兴趣的文章
mysql5.7命令总结
查看>>
mysql5.7安装
查看>>
mysql5.7性能调优my.ini
查看>>
MySQL5.7新增Performance Schema表
查看>>
Mysql5.7深入学习 1.MySQL 5.7 中的新增功能
查看>>
Webpack 之 basic chunk graph
查看>>
Mysql5.7版本单机版my.cnf配置文件
查看>>
mysql5.7的安装和Navicat的安装
查看>>
mysql5.7示例数据库_Linux MySQL5.7多实例数据库配置
查看>>
Mysql8 数据库安装及主从配置 | Spring Cloud 2
查看>>
mysql8 配置文件配置group 问题 sql语句group不能使用报错解决 mysql8.X版本的my.cnf配置文件 my.cnf文件 能够使用的my.cnf配置文件
查看>>
MySQL8.0.29启动报错Different lower_case_table_names settings for server (‘0‘) and data dictionary (‘1‘)
查看>>
MYSQL8.0以上忘记root密码
查看>>
Mysql8.0以上重置初始密码的方法
查看>>
mysql8.0新特性-自增变量的持久化
查看>>
Mysql8.0注意url变更写法
查看>>
Mysql8.0的特性
查看>>
MySQL8修改密码报错ERROR 1819 (HY000): Your password does not satisfy the current policy requirements
查看>>
MySQL8修改密码的方法
查看>>
Mysql8在Centos上安装后忘记root密码如何重新设置
查看>>