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

题意:

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

题解:

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

  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. const int MAXN=1e5+10;
  4. const int INF=-0x3f3f3f3f;
  5. int a[MAXN];
  6. int dp[MAXN][510];
  7. int main()
  8. {
  9. int n;
  10. scanf("%d",&n);
  11. for (int i = 1; i <=n ; ++i) {
  12. scanf("%d",&a[i]);
  13. }
  14. for(int i=n;i>=1;i--) {//表示为p,由大->小,我们需要变化的次数增加
  15. for (int j = 1; j <=500; ++j) {//表示为k的大小
  16. if(i+a[i]+j>n) dp[i][j]=1;
  17. else
  18. dp[i][j]=dp[i+a[i]+j][j]+1;
  19. }
  20. }
  21. int ans=0;
  22. int m;
  23. scanf("%d",&m);
  24. int p,k;
  25. while(m--)
  26. {
  27. ans=0;
  28. scanf("%d%d",&p,&k);
  29. if(k>400)
  30. {
  31. while(p<=n)
  32. {
  33. p=p+a[p]+k;
  34. ans++;
  35. }
  36. printf("%d\n",ans);
  37. }
  38. else
  39. printf("%d\n",dp[p][k]);
  40. }
  41. return 0;
  42. }

  

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. 写英文bug的经验总结

    本文链接: https://www.cnblogs.com/hchengmx/p/10800855.html 由于工作原因,开bug的时候需要由英文开,刚开的时候比较痛苦,因为有些词汇老师用的不太准确 ...

  2. win10 安装mysql zip 压缩包版

    从官网下载zip https://www.mysql.com/downloads/ 解压 D:\devtool\mysql-5.7.17-winx64\ 将  D:\devtool\mysql--wi ...

  3. Struts_OGNL(Object Graph Navigation Language) 对象图导航语言

    1.访问值栈中的action的普通属性: 请求: <a href="ognl.action?username=u&password=p">访问属性</a& ...

  4. 03、IDEA下Spark API编程

    03.IDEA下Spark API编程 3.1 编程实现Word Count 3.1.1 创建Scala模块 3.1.2 添加maven支持,并引入spark依赖 <?xml version=& ...

  5. Python基础学习之标识符

    1.合法的Python标识符 Python标识符字符串规则和其他大部分用C编写的高级语言相似: 第一个字符必须是字母或下划线(_) 剩下的字符可以是字母和数字或下滑线 大小写敏感 标识符不能以数字开头 ...

  6. PHP:return与exit的区别

        return 虽然返回数据,并且不再往下执行,但是它会返回执行上一步的操作,所以return的只是当前function而不会影响其他function的执行:      exti 是完全将整个项 ...

  7. poj 2057 树形DP,数学期望

    题目链接:http://poj.org/problem?id=2057 题意:有一只蜗牛爬上树睡着之后从树上掉下来,发现后面的"房子"却丢在了树上面, 现在这只蜗牛要求寻找它的房子 ...

  8. 2017.9.16 Web 应用开发环境搭建与开发工具安装

    1.JDK的下载与安装 1.1 在网址:http://javase/downloads/index.jsp网站下载最新的JDK版本 1.2 安装jdk,双击下载好的.exe文件运行,一般默认安装在c盘 ...

  9. sql server 基础

    1 .左连接 select a.* ,b.* from student as aleft join hobby as bon a.hobbyid=b.hobbyid 2. 右 连接 select a. ...

  10. CentOS 7与 Windows双系统丢失Windows启动项及默认启动项修改

    1.Windows启动项消失的原因:   在安装Win7.8/10系统+CentOS7双系统后,默认会将mbr(Main Boot Record)改写为grub2,默认的CentOS7无法识别Wind ...