E. Array Queries
time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

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
Note

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+;
const int S=;//j上界取sqrt(n),大一点也可以
int a[N];
int dp[S+][N];
int main()
{
int n;
cin>>n;
for(int i=;i<=n;i++)cin>>a[i];
int q,p,k;
cin>>q;
for(int j=;j<=S;j++)
{
for(int i=n;i>=;i--)
{
if(i+a[i]+j>n)dp[j][i]=;
else dp[j][i]=dp[j][i+a[i]+j]+;
}
}
for(int i=;i<q;i++)
{
cin>>p>>k;
int cnt=;
if(k<=S)cout<<dp[k][p]<<endl;
else
{
while(p<=n)
{
cnt++;
p+=a[p]+k;
}
cout<<cnt<<endl;
}
}
return ;
}

Codeforces 797E - Array Queries的更多相关文章

  1. CodeForcs 797E Array Queries

    $dp$预处理,暴力. 如果$k > sqrt(n)$,那么答案不会超过$sqrt(n)$,暴力模拟即可.如果$k <= sqrt(n)$,那么可以$dp$预处理打表. #include ...

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

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

  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. [Codeforces 266E]More Queries to Array...(线段树+二项式定理)

    [Codeforces 266E]More Queries to Array...(线段树+二项式定理) 题面 维护一个长度为\(n\)的序列\(a\),\(m\)个操作 区间赋值为\(x\) 查询\ ...

  5. lightoj Again Array Queries

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

  6. [Codeforces]817F. MEX Queries 离散化+线段树维护

    [Codeforces]817F. MEX Queries You are given a set of integer numbers, initially it is empty. You sho ...

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

                                                                                                        ...

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

                                                                              1100 - Again Array Queries ...

  9. 【codeforces 797E】Array Queries

    [题目链接]:http://codeforces.com/problemset/problem/797/E [题意] 给你一个n个元素的数组; 每个元素都在1..n之间; 然后给你q个询问; 每个询问 ...

随机推荐

  1. django图书管理系统实例

    首页,其他页面全部继承首页的上半部分 点击发布图书页面 首页点击书名,跳转到图书信息界面,该界面可删除图书 项目结构 #views.py from django.shortcuts import re ...

  2. Tree Implementation with Python

    Tree Implementation with Python List of List 代码如下: def binary_tree(val): return [val, [], []] def in ...

  3. php7安装redis拓展

    phpredis下载地址https://github.com/phpredis/phpredis   解压并进入源码包 unzip phpredis-develop.zip cd phpredis-d ...

  4. (4opencv)如何基于GOCW,创建一个实时视频程序

    直接使用提供的代码框架进行修改,是最快得到效果的方法:但是这样的灵活性较差,而且真正的程序员从来都不会停滞在这一步:我们需要的是"将框架解析到最小化.理清楚每个构建之间的关系",只 ...

  5. AnswerOpenCV一周佳作欣赏(0615-0622)

    一.How to make auto-adjustments(brightness and contrast) for image Android Opencv Image Correction i' ...

  6. win10 实现 iPad AVplayer 搭建 ftp 共享 PC 端

    1.首先是 iPad 端直接去 App Store 下载 AVplayer 即可,我下载的时候这个APP收费 18 元.这个app内置了加速播放视频的功能,非常适合学习时使用. 2.PC 端搭建 ft ...

  7. Python中对象的引用与复制

    在python进行像b = a这样的赋值时,只会创建一个对a的新引用,使a的引用计数加1,而不会创建新的对象: >>> a = 'xyz' >>> import s ...

  8. 【python003-变量】

    变量 一.在使用变量之前,需要先对其进行赋值 二.变量命名的规则:可以包含字母,数字,下划线,但是不能以数字开头 三.字符串: 1.引号内的一切东西 2.python的字符串是要在两边加上引号,对于单 ...

  9. Oracle使用——oracle用户相关操作

    前提 以dba角色登录数据库(普通用户没有操作权限):sqlplus / as sysdba 具体操作 创建用户 创建用户 使用默认表空间创建用户 create user xzgxh identifi ...

  10. C#操作字符串方法总结

    /* ######### ############ ############# ## ########### ### ###### ##### ### ####### #### ### ####### ...