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. 【MATLAB】产生FPGA中ROM需要的余弦表

    产生的二进制数为带符号二进制数,最高位是符号位. function [ DATA ] = cos_gen( Num_addr,Num_out ) % 用来产生ROM需要的余弦表,输出为有符号二进制数 ...

  2. Properties的使用以及配置文件值的获取

    一.项目的部署如下,现在要获取SystemGlobals.properties中的值 二.代码如下: package com.util; import java.io.IOException; imp ...

  3. ALPS语言学校(西雅图)|ALPS Language School (Seattle)

    http://www.swliuxue.com/school-3879.html 所属国家: 美国 所在省洲: 华盛顿州 所在城市: 华盛顿州 建校时间: 1992年 学校类型: 院校 学校类别: 私 ...

  4. HCNA配置浮动静态路由

    1.拓扑图 2.配置IP R1 Please press enter to start cmd line! ############ <Huawei> Dec ::-: Huawei %% ...

  5. Vue.js-项目目录结构解析

    1.Vue初始化项目目录如下: 2.目录各项含义如下:

  6. session登录超时跳出iframe页至登录窗口

    //当我们用权限框架控制登录超时跳至某一个页面时主页面都没什么问题:iframe会在当前窗口下再开一个会话很显然这不是我们想要达到的效果 在登录页中加入此判断即可 $(function(){ //if ...

  7. 关于硬盘分区使用exFat格式的优势及劣势(含摘抄)

    优势 可以设置最大32M的簇: 不记录日志. 劣势 无法使用windows的“文件共享”: 通过近期某个文件数量密级任务的测试发现,在大量文件的处理性能上,NTFS比exFAT文件系统的性能高出不少. ...

  8. hdu-2838 Cow Sorting---逆序对的花费

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=2838 题目大意: 就是求将之前的排列变成一个递增的排列,每交换两个数的代价为两个数的和,求变成递增的 ...

  9. VedioCapture

    国内的技术的浮躁可见一般,在一个用了七八年的项目里面使用的类,居然拼写都是错的,在网上一搜,转载的也大有人在,最低级的错误,你可以不懂编程,但是只要上过高中,Video这个单词总该学过吧,居然转载的时 ...

  10. 为什么实例没有prototype属性?什么时候对象会有prototype属性呢?

    为什么实例没有prototype属性?什么时候对象会有prototype属性呢? javascript loudou 1月12日提问 关注 9 关注 收藏 6 收藏,554 浏览 问题对人有帮助,内容 ...