题目链接

非常好的一道题目,

分析,如果用暴力的话,时间复杂度是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的更多相关文章

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

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

  2. Codeforces 797E - Array Queries

    E. Array Queries 题目链接:http://codeforces.com/problemset/problem/797/E time limit per test 2 seconds m ...

  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. lightoj Again Array Queries

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

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

                                                                                                        ...

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

                                                                              1100 - Again Array Queries ...

  7. 【codeforces 797E】Array Queries

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

  8. [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: ...

  9. 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 ...

随机推荐

  1. 自动化测试基础篇--Selenium中JS处理滚动条

    摘自https://www.cnblogs.com/sanzangTst/p/7692285.html 前言 什么是JS? JS就是JavaScript: JavaScript 是世界上最流行的脚本语 ...

  2. VMWare 虚拟机设置网络访问

    使用桥接模式 将本机IP设置为和虚拟机IP在同一个可以上网的网段中

  3. 解决PowerDesigner不同表输入相同字段名被禁止问题

    选择"Tools -> Model Options"后,弹出如下图所示窗口: 这两个选项取消勾选

  4. Linux CFS调度器之pick_next_task_fair选择下一个被调度的进程--Linux进程的管理与调度(二十八)

    1. CFS如何选择最合适的进程 每个调度器类sched_class都必须提供一个pick_next_task函数用以在就绪队列中选择一个最优的进程来等待调度, 而我们的CFS调度器类中, 选择下一个 ...

  5. linux (fedora 28) 制作启动U盘,启动盘

    最近需要安装一款Linux, 由于使用的计算机系统为 fedora 28, 所以只能在linux 制作U盘 使用 df 或者 fdisk -l 查看 U盘文件: Disk /dev/sdb: byte ...

  6. background-image使用svg如何改变颜色

    结论 在我多番测试之后,才发现background-image使用svg,改变颜色根本做不了. 分析 当svg图片被使用成background-image,颜色的设置需要在svg内部才能生效.在外部C ...

  7. keepalived脑裂问题查找

    在自己环境做keepalived+redis实验时,当重启了备用redies机器后,发现两台redies主机都拿到了VIP [root@redis2 ~]# ip addr list 1: lo: & ...

  8. echarts图表大小随着外部div大小变化

    jquery有resize()事件,但直接调用没有起作用,引入jquery.ba-resize.js文件就可以了. 例如: <div class="chart" > & ...

  9. UVA1626-Brackets sequence(动态规划基础)

    Problem UVA1626-Brackets sequence Time Limit: 4500 mSec Problem Description Input The input begins w ...

  10. Quartz 的DisallowConcurrentExecution

    DisallowConcurrentExecution注解是指:一个JobKey对应的JobDetail实例不运行并发执行,而不是说你继承Job之后的子类不允许并发执行.