Codeforces 797E - Array Queries
2 seconds
256 megabytes
standard input
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.
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).
Print q integers, ith integer must be equal to the answer to ith query.
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.
题目大意:英文很简单,略。
方法: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的更多相关文章
- CodeForcs 797E Array Queries
$dp$预处理,暴力. 如果$k > sqrt(n)$,那么答案不会超过$sqrt(n)$,暴力模拟即可.如果$k <= sqrt(n)$,那么可以$dp$预处理打表. #include ...
- AC日记——Array Queries codeforces 797e
797E - Array Queries 思路: 分段处理: 当k小于根号n时记忆化搜索: 否则暴力: 来,上代码: #include <cmath> #include <cstdi ...
- codeforces 797 E. Array Queries【dp,暴力】
题目链接:codeforces 797 E. Array Queries 题意:给你一个长度为n的数组a,和q个询问,每次询问为(p,k),相应的把p转换为p+a[p]+k,直到p > n为 ...
- [Codeforces 266E]More Queries to Array...(线段树+二项式定理)
[Codeforces 266E]More Queries to Array...(线段树+二项式定理) 题面 维护一个长度为\(n\)的序列\(a\),\(m\)个操作 区间赋值为\(x\) 查询\ ...
- lightoj Again Array Queries
1100 - Again Array Queries PDF (English) Statistics Forum Time Limit: 3 second(s) Memory Limit: 32 ...
- [Codeforces]817F. MEX Queries 离散化+线段树维护
[Codeforces]817F. MEX Queries You are given a set of integer numbers, initially it is empty. You sho ...
- Light OJ-1082 - Array Queries,线段树区间查询最大值,哈哈,水过~~
...
- Light oj-1100 - Again Array Queries,又是这个题,上次那个题用的线段树,这题差点就陷坑里了,简单的抽屉原理加暴力就可以了,真是坑~~
1100 - Again Array Queries ...
- 【codeforces 797E】Array Queries
[题目链接]:http://codeforces.com/problemset/problem/797/E [题意] 给你一个n个元素的数组; 每个元素都在1..n之间; 然后给你q个询问; 每个询问 ...
随机推荐
- ora-12705解决方法
最近使用instant sqlplus测试时,遇到ora-12705,一开始以为是少了配置,经查是,NLS_LANG设置问题,设置为"SIMPLIFIED CHINESE_CHINA.ZHS ...
- rocketmq安装与基本操作
如果不是因为政治原因,就rocketmq的社区活跃度.版本.特性和文档完善度,我是无论如何也不会使用rocketmq的. rocketmq严格意义上并不支持高可靠性,因为其持久化只支持异步,有另外一个 ...
- Fiddler抓取手机端(ios+android)APP接口数据(http+https)
(1)android 环境要求: PC机和手机连接在同一网络下 工具下载地址: Fiddler网上可以下载,自行下载.注意:需要安装fiddlercertmaker(网上自行下载)进行认证 配置步骤: ...
- 单片机中printf函数的重映射
单片机中printf函数的重映射 一.源自于:大侠有话说 1.如果你在学习单片机之前学过C语言,那么一定知道printf这个函数.它最最好用的功能 除了打印你想要的字符到屏幕上外,还能把数字进行格式化 ...
- bzoj 4318 OSU! - 动态规划 - 概率与期望
Description osu 是一款群众喜闻乐见的休闲软件. 我们可以把osu的规则简化与改编成以下的样子: 一共有n次操作,每次操作只有成功与失败之分,成功对应1,失败对应0,n次操作对应为1 ...
- git用法-打补丁【转】
本文转载自:https://www.cnblogs.com/yandufeng/p/5580765.html 1. git cherry-pick 作用:从一个branch上选择一个commit,添加 ...
- Flutter的脚手架(Scaffold)
- Docker 安装Hadoop HDFS命令行操作
网上拉取Docker模板,使用singlarities/hadoop镜像 [root@localhost /]# docker pull singularities/hadoop 查看: [root@ ...
- MD5+salt 工具类
import java.io.UnsupportedEncodingException; import java.security.MessageDigest; import java.securit ...
- Learning to Track at 100 FPS with Deep Regression Networks ECCV 2016 论文笔记
Learning to Track at 100 FPS with Deep Regression Networks ECCV 2016 论文笔记 工程网页:http://davheld.git ...