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.

Input

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).

Output

Print q integers, ith integer must be equal to the answer to ith query.

Example

input
3
1 1 1
3
1 1
2 1
3 1
output
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.

题意:

给你q次查询,每次会有两个数字p,k,问每次使p=p+a[p]+k,总共需要多少次会使p>n

题解:

存粹暴力必然超时,因此需要打个表,

#include<bits/stdc++.h>
using namespace std;
const int MAXN=1e5+10;
const int INF=-0x3f3f3f3f;
int a[MAXN];
int dp[MAXN][510];
int main()
{
int n;
scanf("%d",&n);
for (int i = 1; i <=n ; ++i) {
scanf("%d",&a[i]);
}
for(int i=n;i>=1;i--) {//表示为p,由大->小,我们需要变化的次数增加
for (int j = 1; j <=500; ++j) {//表示为k的大小
if(i+a[i]+j>n) dp[i][j]=1;
else
dp[i][j]=dp[i+a[i]+j][j]+1;
}
}
int ans=0;
int m;
scanf("%d",&m);
int p,k;
while(m--)
{
ans=0;
scanf("%d%d",&p,&k);
if(k>400)
{
while(p<=n)
{
p=p+a[p]+k;
ans++;
}
printf("%d\n",ans);
}
else
printf("%d\n",dp[p][k]);
}
return 0;
}

  

CF797E. Array Queries的更多相关文章

  1. lightoj Again Array Queries

    1100 - Again Array Queries   PDF (English) Statistics Forum Time Limit: 3 second(s) Memory Limit: 32 ...

  2. Codeforces 797E - Array Queries

    E. Array Queries 题目链接:http://codeforces.com/problemset/problem/797/E time limit per test 2 seconds m ...

  3. codeforces 797 E. Array Queries【dp,暴力】

    题目链接:codeforces 797 E. Array Queries   题意:给你一个长度为n的数组a,和q个询问,每次询问为(p,k),相应的把p转换为p+a[p]+k,直到p > n为 ...

  4. AC日记——Array Queries codeforces 797e

    797E - Array Queries 思路: 分段处理: 当k小于根号n时记忆化搜索: 否则暴力: 来,上代码: #include <cmath> #include <cstdi ...

  5. Light OJ-1082 - Array Queries,线段树区间查询最大值,哈哈,水过~~

                                                                                                        ...

  6. Light oj-1100 - Again Array Queries,又是这个题,上次那个题用的线段树,这题差点就陷坑里了,简单的抽屉原理加暴力就可以了,真是坑~~

                                                                              1100 - Again Array Queries ...

  7. [Codeforces 863D]Yet Another Array Queries Problem

    Description You are given an array a of size n, and q queries to it. There are queries of two types: ...

  8. Yet Another Array Queries Problem CodeForces - 863D (暴力/思维)

    You are given an array a of size n, and q queries to it. There are queries of two types: 1 li ri — p ...

  9. Light oj 1100 - Again Array Queries (鸽巢原理+暴力)

    题目链接:http://www.lightoj.com/volume_showproblem.php?problem=1100 给你n个数,数的范围是1~1000,给你q个询问,每个询问问你l到r之间 ...

随机推荐

  1. 修改Android系统关机动画

    文件路径:frameworks\base\services\core\java\com\android\server\power\ShutdownThread.java 在beginShutdownS ...

  2. Angular JS + Express JS入门搭建网站

    3月份开始,接到了新的任务,跟UI开发有关,用的是Angular JS,Express JS等技术.于是周末顺便学习下新技术. 组里产品UI架构如下: 其中前端,主要使用Angular JS框架,另外 ...

  3. Struts2_访问Web元素

    取得Map 类型的 request,session,application, HttpServletRequest,HttpSession,ServletContext的引用. 分访问 Map 类型和 ...

  4. python3基础13(format的使用)

    #!/usr/bin/env python# -*- coding:utf-8 -*- from string import Templatedict={'name':'python','age':2 ...

  5. Python数字、字符串

    1. 数字 byte 在python3中最重要的特性是对文本和二进制数据做了更加清晰的区分,python3不会以任意隐式方式混用字节型和字符型,也因此在python3中不能拼接字符串和字节包(pyth ...

  6. 笨办法学Python(八)

    习题 8: 打印,打印 formatter = "%r %r %r %r" print formatter % (1, 2, 3, 4) print formatter % (&q ...

  7. ABI 管理

    https://developer.android.google.cn/ndk/guides/abis.html 不同 Android 手机使用不同的 CPU,因此支持不同的指令集.CPU 与指令集的 ...

  8. Linux高性能server编程——定时器

    版权声明:本文为博主原创文章.未经博主允许不得转载. https://blog.csdn.net/walkerkalr/article/details/36869913  定时器 服务器程序通常管 ...

  9. 最终类object 和内部类

    Object 类 性质:[1]是所有类的根类.             [2]如果一个类没有显示继承另外一个类,那么该类一定继承于Object toString() 返回对象的字符串表示形式 特殊:[ ...

  10. React的安装

    创建: 2019/05/01 完成: 2019/05/01 create-react-app  学习及创建单页app npx create-react-app my-app cd my-app npm ...