Array Queries CodeForces - 797E
非常好的一道题目,
分析,如果用暴力的话,时间复杂度是O(q*n)稳稳的超时
如果用二维DP的话,需要O (n*n)的空间复杂度,会爆空间。
那么分析一下,如果k>sqrt(n)的话,不需要sqrt(n) 次就可以达到大于n
而如果k<sqrt(n)的情况下,不妨用DP来处理,时间复杂度和空间复杂度皆为O ( n*sqrt( n ) )
那么都可以分类处理了。
详细见code
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <vector>
#define rep(i,x,n) for(int i=x;i<n;i++)
#define repd(i,x,n) for(int i=x;i<=n;i++)
#define pii pair<int,int>
#define pll pair<long long ,long long>
#define gbtb std::ios::sync_with_stdio(false)
#define MS0(X) memset((X), 0, sizeof((X)))
#define MSC0(X) memset((X), '\0', sizeof((X)))
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define gg(x) getInt(&x)
using namespace std;
typedef long long ll;
inline void getInt(int* p);
const int maxn=;
/*** TEMPLATE CODE * * STARTS HERE ***/
int n;
int a[maxn];
int p,q;
int k;
const int maxm= ;
int dp[][maxm];
void init(void)
{
for(int j=;j<maxm;j++)
{
for(int i=n;i>=;i--)
{
if(i+j+a[i]>n)
dp[i][j]=;
else
dp[i][j]=+dp[i+j+a[i]][j];
}
}
} int solve(int p,int k)
{
int cnt=;
while(p<=n)
{
p=a[p]+p+k;
cnt++;
}
return cnt;
}
int main()
{
gg(n);
repd(i,,n)
{
gg(a[i]); }
init();
gg(q);
while(q--)
{
gg(p);
gg(k);
if(k>)
printf("%d\n",solve(p,k));
else
printf("%d\n",dp[p][k]);
}
return ;
} inline void getInt(int* p) {
char ch;
do {
ch = getchar();
} while (ch == ' ' || ch == '\n');
if (ch == '-') {
*p = -(getchar() - '');
while ((ch = getchar()) >= '' && ch <= '') {
*p = *p * - ch + '';
}
}
else {
*p = ch - '';
while ((ch = getchar()) >= '' && ch <= '') {
*p = *p * + ch - '';
}
}
}
Array Queries CodeForces - 797E的更多相关文章
- AC日记——Array Queries codeforces 797e
797E - Array Queries 思路: 分段处理: 当k小于根号n时记忆化搜索: 否则暴力: 来,上代码: #include <cmath> #include <cstdi ...
- 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为 ...
- lightoj Again Array Queries
1100 - Again Array Queries PDF (English) Statistics Forum Time Limit: 3 second(s) Memory Limit: 32 ...
- 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个询问; 每个询问 ...
- [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 ...
随机推荐
- MySQL8.0关于caching_sha2_password Plugin的一个Bug
今天在调试使用ansible进行标准化安装MySQL8.0时,发现关于caching_sha2_password plugin的一个bug. 在搭建主从复制时,按照手册说明需要创建用户: create ...
- Linux下1号进程的前世(kernel_init)今生(init进程)----Linux进程的管理与调度(六)
前面我们了解到了0号进程是系统所有进程的先祖, 它的进程描述符init_task是内核静态创建的, 而它在进行初始化的时候, 通过kernel_thread的方式创建了两个内核线程,分别是kernel ...
- c/c++ 图的创建(二维数组法)
c/c++ 图的创建(二维数组法) 图的概念 图由点和线组成 知道了图中有多少个点,和哪些点之间有线,就可以把一张图描绘出来 点之间的线,分有方向和无方向 创建图 创建图,实际就是创建出节点,和节点之 ...
- git笔记(1)-搭建
1. git下载 地址:https://git-scm.com/download/win 2. 安装(略) 3. 创建本地仓库 方法一:(1) 打开git bash here, 进入相应目录(打算创建 ...
- oracle数据库访问形式
1. sql plus访问, sqlplus.exe 2. sql developer访问,sqldeveloper.exe 3. pl/sql 自己下载 4. browse https://loca ...
- innerHTML的使用
inerHTML是html标签的属性,成对出现的标签大多数都有这个属性,用来设置或获取位于对象起始和结束标签 内的HTML.(获取HTML当前标签的起始和结束里面的内容)不包括标签本身. ...
- Java入门(四):运算符优先级、关键字与保留字
上次介绍了Java的运算符,今天来介绍下运算符的优先级,以及Java的关键字.保留字. 一.运算符优先级 序号 运算符 名称 目数 结合性 说明 1 [ ] 方括号 从左向右 优先级最高 . 点号 双 ...
- 微信H5开发,页面被缓存,不更新
原文:https://blog.csdn.net/qq_27471405/article/details/79295348 这里只是备份 前言:每一次请求,我们都知道浏览器会做一定处理,其中就包括对 ...
- Spring Boot 菜鸟教程 application.properties 常用配置
SPRING CONFIG (ConfigFileApplicationListener) spring.config.name 配置文件名称,默认为application spring.config ...
- 2293: Distribution Center 中南多校
Description The factory of the Impractically Complicated Products Corporation has many manufacturing ...