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位置开始,这样不会影响答案

扫描二维码关注公众号,回复: 12614194 查看本文章

虽然每次都是从起点进行起跳,但是在跳的过程中也可能会跳到后面的位置上,因此我们可以设: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; 
}

猜你喜欢

转载自blog.csdn.net/li_wen_zhuo/article/details/114264413
今日推荐