CF797E. Array Queries
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
- 3
1 1 1
3
1 1
2 1
3 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
题解:
存粹暴力必然超时,因此需要打个表,
- #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的更多相关文章
- lightoj Again Array Queries
1100 - Again Array Queries PDF (English) Statistics Forum Time Limit: 3 second(s) Memory Limit: 32 ...
- Codeforces 797E - Array Queries
E. Array Queries 题目链接:http://codeforces.com/problemset/problem/797/E time limit per test 2 seconds m ...
- codeforces 797 E. Array Queries【dp,暴力】
题目链接:codeforces 797 E. Array Queries 题意:给你一个长度为n的数组a,和q个询问,每次询问为(p,k),相应的把p转换为p+a[p]+k,直到p > n为 ...
- AC日记——Array Queries codeforces 797e
797E - Array Queries 思路: 分段处理: 当k小于根号n时记忆化搜索: 否则暴力: 来,上代码: #include <cmath> #include <cstdi ...
- Light OJ-1082 - Array Queries,线段树区间查询最大值,哈哈,水过~~
...
- Light oj-1100 - Again Array Queries,又是这个题,上次那个题用的线段树,这题差点就陷坑里了,简单的抽屉原理加暴力就可以了,真是坑~~
1100 - Again Array Queries ...
- [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: ...
- 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 ...
- Light oj 1100 - Again Array Queries (鸽巢原理+暴力)
题目链接:http://www.lightoj.com/volume_showproblem.php?problem=1100 给你n个数,数的范围是1~1000,给你q个询问,每个询问问你l到r之间 ...
随机推荐
- 写英文bug的经验总结
本文链接: https://www.cnblogs.com/hchengmx/p/10800855.html 由于工作原因,开bug的时候需要由英文开,刚开的时候比较痛苦,因为有些词汇老师用的不太准确 ...
- win10 安装mysql zip 压缩包版
从官网下载zip https://www.mysql.com/downloads/ 解压 D:\devtool\mysql-5.7.17-winx64\ 将 D:\devtool\mysql--wi ...
- Struts_OGNL(Object Graph Navigation Language) 对象图导航语言
1.访问值栈中的action的普通属性: 请求: <a href="ognl.action?username=u&password=p">访问属性</a& ...
- 03、IDEA下Spark API编程
03.IDEA下Spark API编程 3.1 编程实现Word Count 3.1.1 创建Scala模块 3.1.2 添加maven支持,并引入spark依赖 <?xml version=& ...
- Python基础学习之标识符
1.合法的Python标识符 Python标识符字符串规则和其他大部分用C编写的高级语言相似: 第一个字符必须是字母或下划线(_) 剩下的字符可以是字母和数字或下滑线 大小写敏感 标识符不能以数字开头 ...
- PHP:return与exit的区别
return 虽然返回数据,并且不再往下执行,但是它会返回执行上一步的操作,所以return的只是当前function而不会影响其他function的执行: exti 是完全将整个项 ...
- poj 2057 树形DP,数学期望
题目链接:http://poj.org/problem?id=2057 题意:有一只蜗牛爬上树睡着之后从树上掉下来,发现后面的"房子"却丢在了树上面, 现在这只蜗牛要求寻找它的房子 ...
- 2017.9.16 Web 应用开发环境搭建与开发工具安装
1.JDK的下载与安装 1.1 在网址:http://javase/downloads/index.jsp网站下载最新的JDK版本 1.2 安装jdk,双击下载好的.exe文件运行,一般默认安装在c盘 ...
- sql server 基础
1 .左连接 select a.* ,b.* from student as aleft join hobby as bon a.hobbyid=b.hobbyid 2. 右 连接 select a. ...
- CentOS 7与 Windows双系统丢失Windows启动项及默认启动项修改
1.Windows启动项消失的原因: 在安装Win7.8/10系统+CentOS7双系统后,默认会将mbr(Main Boot Record)改写为grub2,默认的CentOS7无法识别Wind ...