【题目链接】:http://codeforces.com/problemset/problem/797/E

【题意】



给你一个n个元素的数组;

每个元素都在1..n之间;

然后给你q个询问;

每个询问由p和k构成;

会对p进行

p=p+a[p]+k操作若干次;

你要输出p第一次大于n之后操作了多少次;

【题解】



部分DP;

这里对于k>400的询问;

我们直接暴力求解;

因为这时暴力所花费的时间已经可以接受了;

而对于剩余的k<=400的询问;

我们写一个记忆化搜索来求解;

很棒的思路吧



【Number Of WA】



0



【完整代码】

#include <bits/stdc++.h>
using namespace std;
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define LL long long
#define rep1(i,a,b) for (int i = a;i <= b;i++)
#define rep2(i,a,b) for (int i = a;i >= b;i--)
#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define ms(x,y) memset(x,y,sizeof x) typedef pair<int,int> pii;
typedef pair<LL,LL> pll; const int dx[9] = {0,1,-1,0,0,-1,-1,1,1};
const int dy[9] = {0,0,0,-1,1,-1,1,-1,1};
const double pi = acos(-1.0);
const int N = 1e5+100; int n,a[N],q;
int f[N][400+20]; int dfs(int p,int k)
{
if (p>n)
return 0;
if (k>400)
return 1+dfs(p+a[p]+k,k);
else
return f[p][k]=(f[p][k]?f[p][k]:1+dfs(p+a[p]+k,k));
} int main()
{
//freopen("F:\\rush.txt","r",stdin);
ios::sync_with_stdio(false);
cin >> n;
rep1(i,1,n)
cin >> a[i];
cin >> q;
rep1(i,1,q)
{
int p,k;
cin >> p >> k;
cout << dfs(p,k) << endl;
}
//printf("\n%.2lf sec \n", (double)clock() / CLOCKS_PER_SEC);
return 0;
}

【codeforces 797E】Array Queries的更多相关文章

  1. 【Codeforces 1108E1】Array and Segments (Easy version)

    [链接] 我是链接,点我呀:) [题意] 题意 [题解] 枚举最大值和最小值在什么地方. 显然,只要包含最小值的区间,都让他减少. 因为就算那个区间包含最大值,也无所谓,因为不会让答案变小. 但是那些 ...

  2. 【CodeForces 624D】Array GCD

    题 You are given array ai of length n. You may consecutively apply two operations to this array: remo ...

  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. 【codeforces 415D】Mashmokh and ACM(普通dp)

    [codeforces 415D]Mashmokh and ACM 题意:美丽数列定义:对于数列中的每一个i都满足:arr[i+1]%arr[i]==0 输入n,k(1<=n,k<=200 ...

  5. 【层次查询】Hierarchical Queries之亲兄弟间的排序(ORDER SIBLINGS BY)

    http://blog.itpub.net/519536/viewspace-624176 有关层次查询之前的文章参考如下. [层次查询]Hierarchical Queries之"树的遍历 ...

  6. 【36.86%】【codeforces 558B】Amr and The Large Array

    time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...

  7. 【24.17%】【codeforces 721D】Maxim and Array

    time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...

  8. 【codeforces 754A】Lesha and array splitting

    time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...

  9. 【Codeforces 258B】 Sort the Array

    [题目链接] http://codeforces.com/contest/451/problem/B [算法] 模拟 在序列中找到一段单调递增的子序列,将这段序列反转,然后判断序列是否变得单调递增,即 ...

随机推荐

  1. ASP.NET Web Projects

    https://msdn.microsoft.com/en-us/library/ywdtth2f.aspx The topics in this section describe how to cr ...

  2. mst

    https://www.zybuluo.com/ysner/note/1245941 题面 给一个\(n\)点完全图,点权均小于\(2^m\).定义边权等于两端点点权的与和(即\(a_i\&b ...

  3. c3p0-config.xml文件简单说明与备忘

    <?xml version="1.0" encoding="UTF-8"?> <c3p0-config> <named-confi ...

  4. codevs3304水果姐逛街(线段数)

    3304 水果姐逛水果街Ⅰ  时间限制: 2 s  空间限制: 256000 KB  题目等级 : 钻石 Diamond   题目描述 Description 水果姐今天心情不错,来到了水果街. 水果 ...

  5. Unity项目 - 吃豆人Pacman

    项目展示 Github项目地址:Pacman 涉及知识 切片制作 Animations 状态机设置,any state切换,重写状态机 按键读取进行整数距离的刚体移动 用射线检测碰撞性 渲染顺序问题 ...

  6. VUE移动端禁止双手放大缩小

    //index.html <meta name="viewport" content="width=device-width,initial-scale=1.0,u ...

  7. 题解报告:hdu 1285 确定比赛名次

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1285 Problem Description 有N个比赛队(1<=N<=500),编号依次 ...

  8. VS2015环境配置: VS2015 未能正确加载“ResourceManagerPackage”包的问题

    启动vs2015专业版时,出现类似于这样的提示框,有好几个,点击是或否,但下次打开还是会出现.笔者也寻找了网上的一些解决办法,例如用vs命令窗口或其他,但都无疾而终,下面提供的这个办法,顺利解决此问题 ...

  9. 使用A*寻路小记

    前几天做另一个DEMO 要用实现自动寻路功能,看到普遍都是A* 学习了下 我的主循环代码: isFindEndPoint = false; //主循环 do { CreateOutSkirtsNode ...

  10. 实现X*N

    #include<iostream> using namespace std; double foo(int n,double x) { if(1==n) { return x; } el ...