a is an array of n positive integers, all of which are not greater than n.
You have to process q queries to this array. Each query is represented by two numbers p and k. Several operations are performed in each query; each operation changes p to p + ap + k. There operations are applied until p becomes greater than n. The answer to the query is the number of performed operations.
The first line contains one integer n (1 ≤ n ≤ 100000).
The second line contains n integers — elements of a (1 ≤ ai ≤ n for each i from 1 to n).
The third line containts one integer q (1 ≤ q ≤ 100000).
Then q lines follow. Each line contains the values of p and k for corresponding query (1 ≤ p, k ≤ n).
Print q integers, ith integer must be equal to the answer to ith query.
3
1 1 1
3
1 1
2 1
3 1
2
1
1
Consider first example:
In first query after first operation p = 3, after second operation p = 5.
In next two queries p is greater than n after the first operation.
题目大意:英文很简单,略。
方法:dp.
状态:dp[j][i]表示对i进行操作,每次加上a[i]+j(同时更新i的值),最后使i>n所需的操作次数。
┏ dp[j][i+a[i]+j]+1 , i+a[i]+j<=n;
dp[j][i]=┃
┗ 1 , i+a[i]+j>n.
代码:
#include<iostream> #include<cstdio> using namespace std; const int N=1e5+5; const int S=320;//j上界取sqrt(n),大一点也可以 int a[N]; int dp[S+1][N]; int main() { int n; cin>>n; for(int i=1;i<=n;i++)cin>>a[i]; int q,p,k; cin>>q; for(int j=1;j<=S;j++) { for(int i=n;i>=1;i--) { if(i+a[i]+j>n)dp[j][i]=1; else dp[j][i]=dp[j][i+a[i]+j]+1; } } for(int i=0;i<q;i++) { cin>>p>>k; int cnt=0; if(k<=S)cout<<dp[k][p]<<endl; else { while(p<=n) { cnt++; p+=a[p]+k; } cout<<cnt<<endl; } } return 0; }