C. Pekora and Trampoline

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
inputCopy
3
7
1 4 2 2 2 2 2
2
2 3
5
1 1 1 1 1
outputCopy
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.

#include<bits/stdc++.h>
using namespace std;
#define ll long long
const int N=1e5+5;
const ll inf=3e9+10;
int t;
ll a[N],b[N];
int main() {
	scanf("%d",&t);
	while(t--) {
		int n;
		scanf("%d",&n);
		for(int i=1; i<=n; i++)
			scanf("%lld",&a[i]);
		ll ans=0;
		memset(b,0,sizeof(b));
		for(int i=1;i<=n;i++){
			if (a[i]==1) continue;
			for(int j=i+2;j<=min(1ll*n,a[i]+i);j++) b[j]++; 
		} 
		for(int i=1;i<=n;i++){
			if(a[i]-b[i]>1) ans+=(a[i]-b[i]-1);
			else b[i+1]+=(b[i]-a[i]+1);
		}
		printf("%lld\n",ans);
	}
	return 0 ;
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
这是一个PLT trampoline的汇编代码示例,用于实现延迟绑定: ``` .section .text .globl trampoline .hidden trampoline .type trampoline, @function .align 16 trampoline: push %rbp mov %rsp, %rbp jmp *got(%rip) # 跳转到GOT中存储的实际函数入口地址 nop .section .text .globl runtimeResolve .type runtimeResolve, @function runtimeResolve: # Your code for resolving the function address here # 将解析出的函数地址存储到GOT中 mov %rax, got(%rip) mov %rax, (%rax) # 跳转回PLT trampoline的下一条指令 mov %rbp, %rsp pop %rbp jmp *got+8(%rip) # 跳转回PLT trampoline的下一条指令 nop .section .data .globl got .type got, @object got: .quad 0 # 第一个元素必须为0 .section .bss .globl plt .type plt, @object .align 16 plt: .quad trampoline .quad got+8 .quad runtimeResolve .quad 0 .quad 0 ``` 这个汇编代码中,首先定义了一个GOT表和一个PLT表。在调用一个动态链接库中的函数时,程序首先会跳转到PLT中的相应函数指针,即trampoline函数。trampoline函数中通过jmp指令跳转到GOT表中存储的实际函数入口地址,实现了延迟绑定的效果。 在第一次调用函数时,GOT表中对应的函数地址为0,因此程序会跳转到runtimeResolve函数中进行函数地址的解析。解析出函数地址后,将其存储到GOT表中,下次调用该函数时就可以直接跳转到实际函数入口地址执行函数了。 注意,在runtimeResolve函数中,解析出的函数地址需要存储到GOT表中,而不是直接跳转到该函数地址执行。这是因为在动态链接库中,同一个函数可能被多个程序共享,如果直接跳转到函数地址执行,可能会出现不同程序之间的地址冲突问题。因此,需要将函数地址存储到GOT表中,由程序去跳转到对应的函数入口执行。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值