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个询问; 每个询问 ...
随机推荐
- Vue小案例 之 商品管理------学习过滤器 使用过滤器处理日期的格式
代码学习过滤器 过滤器介绍:过滤模型数据,在数据显示前做预处理操作: 内置过滤器:在1.x中,Vue提供了内置过滤器,但是在2.x中已经完全废除: 解决办法: (1)使用第三方库来替代1.x中的内置过 ...
- P2590 [ZJOI2008]树的统计(树链剖分)
P2590 [ZJOI2008]树的统计 虽然是入门树剖模板 但是我终于1A了(大哭) 懒得写啥了(逃 #include<iostream> #include<cstdio> ...
- P1357 花园
洛咕原题 题解 状压dp+矩乘 首先看到题目说M<=5,这么小的数据明显可以用状压保存相邻状态,于是可以得到一个80分的dp: 先筛出所有可用的状态,然后建立一个矩阵保存可转移的状态,再然后把每 ...
- Machine Learning - Andrew Ng - Coursera
Machine Learning - Andrew Ng - Coursera Contents 1 Notes 1 Notes What is Machine Learning? Two defin ...
- PHP 将大量数据导出到 Excel 的方法
原文链接:https://blog.csdn.net/bkcnl/article/details/76152505 public function getxiajiueser(){ $id = req ...
- Adobe阅读器渗透攻击
Adobe阅读器渗透攻击 实验前准备 1.两台虚拟机,其中一台为kali,一台为windows xp sp3(老师给的xp虚拟机winxpAttaker,密码:mima1234). 2.设置虚拟机网络 ...
- USB通信基础知识
1 USB系统组成 主机:提供USB接口和接口管理功能的硬件.软件.固件的复合体.PC机或OTG设备,一个USB系统只能有一个主机 设备:1.集线器HUB:扩展主机接口,设备可以通过其接入主机 2. ...
- 【Python028--引入文件】
一.打开文件 1.open()函数 打开模式 执行操作 ‘r’ 以只读方式打开文件(默认) ‘w’ 以写入的方式打开文件,会覆盖已存在的文件 ‘x’ 如果文件已经存在,使用此模式打开将引发异常 ...
- Android 系统(64)---Android中m、mm、mmm、mma、mmma的区别【转】
本文转载自:https://blog.csdn.net/zhangbijun1230/article/details/80196379 Android中m.mm.mmm.mma.mmma的区别 m ...
- thinkphp留言板开发笔记 1 - 新的
关于php数组的排序函数的总结: 有很多种排序方式和排序规则: 正常排序和反向排序, 使用 -r来表示 排序时是否考虑索引/下标, 如果考虑则加上-a, a=associate. sort是按值来排序 ...