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

    Normal COM.cpp #include "resource.h" // 主符号 #include "ATLCOM_i.h" #include " ...

  2. window系统安装jdk,jre

    java开发少不了安装jdk.当然如果只是想运行其他人的java项目,只需要安装jre就行了,不需要安装jdk,jdk是编译用的.jdk可以同时安装多个 版本,只需要在项目部署时注意切换版本选择.在这 ...

  3. iphone 微信下浏览器中数字去除下划线

    在开发iphone应用程序的时候,safari下手机号码默认是有下划线的,通过下面的方法就可以去掉: <meta name="format-detection" conten ...

  4. SharePoint 2010 网络上的开发经验和资源

    sharepoint 集成 Exchange 基于OWA方式获取Exchange中未读邮件  http://www.cnblogs.com/jinho/archive/2011/09/17/21798 ...

  5. 力不从心 Leetcode(ugly number heap) 263, 264,313

    Leetcode ugly number set (3 now) new ugly number is generated by multiplying a prime with previous g ...

  6. LCT入门

    前言 \(LCT\),真的是一个无比神奇的数据结构. 它可以动态维护链信息.连通性.边权.子树信息等各种神奇的东西. 而且,它其实并不难理解. 就算理解不了,它简短的代码也很好背. \(LCT\)与实 ...

  7. 2.NBU管理NetBackup

    2管理NetBackup2.1NetBackup作业任务监视 NetBackup任务监视器可以监视备份.恢复和归档任务的状态,也可以监视NetBackup本身数据库的备份. 2.1.1Activity ...

  8. 1.4 NBU配置备份策略(Policy)

    1.4 配置备份策略(Policy) 一个备份策略由四部分组成. Attributes(属性) Policy是否Active Policy类型 由此Policy产生的任务的优先级 使用的Storage ...

  9. CentOS7 设置开机自启

    [root@master-1 ~]# systemctl enable mariadb ln -s '/usr/lib/systemd/system/mariadb.service' '/etc/sy ...

  10. OnCollisionEnter和OnTriggerEnter

    之前对这两个的用法不是特别清楚,重新学习了下,再做个测试看看效果如何: 1.新建一个场景test 2.添加一个cube,点击Inspector面板会发现系统已经默认添加了Box collisder组件 ...