Codeforces Global Round 13 C. Pekora and Trampoline(贪心)

题目描述

There is a trampoline park with n trampolines in a line. The i-th of which has strength Si.
Pekora can jump on trampolines in multiple passes. She starts the pass by jumping on any trampoline of her choice.
If at the moment Pekora jumps on trampoline i, the trampoline will launch her to position i+Si, and Si will become equal to max(Si−1,1). In other words, Si will decrease by 1, except of the case Si=1, when Si will remain equal to 1.
If there is no trampoline in position i+Si, then this pass is over. Otherwise, Pekora will continue the pass by jumping from the trampoline at position i+Si by the same rule as above.
Pekora can’t stop jumping during the pass until she lands at the position larger than n (in which there is no trampoline). Poor Pekora!
Pekora is a naughty rabbit and wants to ruin the trampoline park by reducing all Si to 1. What is the minimum number of passes she needs to reduce all Si to 1?

Input

The first line contains a single integer t (1≤t≤500) — the number of test cases.
The first line of each test case contains a single integer n (1≤n≤5000) — the number of trampolines.
The second line of each test case contains n integers S1,S2,…,Sn (1≤Si≤109), where Si is the strength of the i-th trampoline.
It’s guaranteed that the sum of n over all test cases doesn’t exceed 5000.

Output

For each test case, output a single integer — the minimum number of passes Pekora needs to do to reduce all Si to 1.

Example

input
3
7
1 4 2 2 2 2 2
2
2 3
5
1 1 1 1 1
output
4
3
0

Note

For the first test case, here is an optimal series of passes Pekora can take. (The bolded numbers are the positions that Pekora jumps into during these passes.)
[1,4,2,2,2,2,2]
[1,4,1,2,1,2,1]
[1,3,1,2,1,1,1]
[1,2,1,2,1,1,1]
For the second test case, the optimal series of passes is show below.
[2,3]
[1,3]
[1,2]
For the third test case, all Si are already equal to 1.

题目大意

给出一个数组a[],可以从任意地方开始跳。假如在第i个开始跳,那么跳到下一个的位置是i+a[i],并且当前位置a[i]变为min(1,a[i]-1),跳到超过n为止,这算一个操作数。问,当操作数为多少时,可以使数组全部变成1,求出最少的操作数。

题目分析

首先要想让跳的轮数最少,那么就要使跳的长度最长,即:每次都从起点起跳可以得到最优解。此外,我们可以对其进行一次优化:如果从1到i-1的a[]都已经等于1了,那么我们就可以直接从i位置开始,这样不会影响答案

虽然每次都是从起点进行起跳,但是在跳的过程中也可能会跳到后面的位置上,因此我们可以设:cnt[i] //表示i位置被从前面起跳的操作跳到了多少次,此处的a[i]=原a[i]-1-cnt[i]。
当从i位置起跳时(此时a[1一i-1]=1),有两种可能:

  1. a [ i ] − 1 > c n t [ i ] a[i]-1>cnt[i] a[i]1>cnt[i],此时说明现a[i]还没到1,因此我们要以这里为起点再跳 a [ i ] − 1 − c n t [ i ] a[i]-1-cnt[i] a[i]1cnt[i]次。这时,经过此处跳到过的位置有i+2到min(n,a[i]+i),这些位置的cnt[]要+1。
    因为当a[i]=1时就不在从这起跳了,因此i+1位置不会由i跳到。
  2. c n t [ i ] > a [ i ] − 1 cnt[i]>a[i]-1 cnt[i]>a[i]1,此时说明a[i]已经跳到1了,因此不需要再以这里为起点进行跳跃。这时,经过此处跳到过的位置有i+2到min(n,a[i]+i),这些位置的cnt[]要+1。
    因为当a[i]=1之后还在这里跳了cnt[i]-a[i]-1次,因此cnt[i+1]要加上cnt[i]-a[i]-1。

在以上过程中统计在每个位置上的起跳次数即为最终的答案。

代码如下
#include <iostream>
#include <cstdio>
#include <cmath>
#include <string>
#include <cstring>
#include <map>
#include <unordered_map>
#include <queue>
#include <vector>
#include <set>
#include <bitset>
#include <algorithm>
#define LL long long
#define PII pair<int,int>
#define x first
#define y second
using namespace std;
const int N=1e4+5,mod=1e9+7;
int a[N],cnt[N];
int main()
{
	int t;
	scanf("%d",&t);
	while(t--)
	{
		memset(cnt,0,sizeof cnt);		//清空cnt[]
		int n;
		scanf("%d",&n);
		for(int i=1;i<=n;i++) scanf("%d",&a[i]);
		LL ans=0;
		for(int i=1;i<=n;i++)
		{
			if(a[i]-1>cnt[i]) ans+=a[i]-cnt[i]-1;			//情况1,答案增加而cnt[i+1]不增加
			if(cnt[i]>a[i]-1) cnt[i+1]+=cnt[i]-a[i]+1;		//情况2,答案不增加而cnt[i+1]增加
			for(int j=i+2;j<=min(n,a[i]+i);j++) cnt[j]++;
		}
		printf("%lld\n",ans);
	}
	return 0; 
}
  • 7
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 4
    评论
根据提供的引用内容,Codeforces Round 511 (Div. 1)是一个比赛的名称。然而,引用内容中没有提供与这个比赛相关的具体信息或问题。因此,我无法回答关于Codeforces Round 511 (Div. 1)的问题。如果您有关于这个比赛的具体问题,请提供更多的信息,我将尽力回答。 #### 引用[.reference_title] - *1* [Codeforces Round 860 (Div. 2)题解](https://blog.csdn.net/qq_60653991/article/details/129802687)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* [Codeforces Round 867 (Div. 3)(A题到E题)](https://blog.csdn.net/wdgkd/article/details/130370975)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control,239^v3^insert_chatgpt"}} ] [.reference_item] - *3* [Codeforces Round 872 (Div. 2)(前三道](https://blog.csdn.net/qq_68286180/article/details/130570952)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

lwz_159

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值