题目链接:codeforces 797 E. Array Queries  

题意:给你一个长度为n的数组a,和q个询问,每次询问为(p,k),相应的把p转换为p+a[p]+k,直到p > n为止,求每次询问要转换的次数。

题解:纯暴力会TLE,所以在k为根号100000范围内dp打表

dp[i][j]表示初始p为i, k为j,需要转换几次可以大于n。

状态转移方程:dp[i][j] = dp[i+a[i]+j] + 1

#include <cstdio>
#include <algorithm>
#include <cstring>
using namespace std; const int N = 1e5+;
const int M = +; int dp[N][M];
int a[N];
int n, q; int main()
{
int i, j;
scanf("%d", &n);
for(i = ; i <= n; ++i) {
scanf("%d", &a[i]);
} memset(dp, , sizeof(dp));
for(i = n; i >= ; --i) {//dp , 打表
for(j = ; j <= M; ++j) {
if(i + a[i] + j > n) dp[i][j] = ;
else {
dp[i][j] = dp[i+a[i]+j][j] + ;
}
}
} scanf("%d", &q);
while(q--) {
int x, y;
scanf("%d%d", &x, &y);
if(y <= 320) printf("%d\n", dp[x][y]);
else {
int ans = ;
for(int p = x; p <= n; p += a[p]+y)
ans++;
printf("%d\n", ans);
}
}
return ;
}

codeforces 797 E. Array Queries【dp,暴力】的更多相关文章

  1. 【codeforces 797E】Array Queries

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

  2. Codeforces#86D Powerful array(分块暴力)

    Description An array of positive integers a1, a2, ..., an is given. Let us consider its arbitrary su ...

  3. CodeForces 754C Vladik and chat (DP+暴力)

    题意:给定n个人的m个对话,问能不能找一个方式使得满足,上下楼层人名不同,并且自己不提及自己. 析:首先预处理每一层能有多少个user可选,dp[i][j] 表示第 i 层是不是可以选第 j 个use ...

  4. CodeForces 402D Upgrading Array (数学+DP)

    题意:给出一个数列,可以进行一种操作将某一个前缀除去他们的gcd,有一个函数f(x),f(1) = 0 , f(x) = f(x/p)+1,f(x) = f(x/p)-1(p是坏素数), 求 sum( ...

  5. CodeForces 446A DZY Loves Sequences (DP+暴力)

    题意:给定一个序列,让你找出一个最长的序列,使得最多改其中的一个数,使其变成严格上升序列. 析:f[i] 表示以 i 结尾的最长上升长度,g[i] 表示以 i 为开始的最长上升长度,这两个很容易就求得 ...

  6. Codeforces 797E - Array Queries

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

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

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

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

                                                                              1100 - Again Array Queries ...

  9. lightoj Again Array Queries

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

随机推荐

  1. Sed - An Introduction and Tutorial by Bruce Barnett

    http://www.grymoire.com/unix/sed.html Quick Links - NEW Sed Commands : label # comment {....} Block ...

  2. WPF Window对象的生命周期

    WPF中所有窗口的基类型都是System.Windows.Window.Window通常用于SDI(SingleDocumentInterface).MDI(MultipleDocumentInter ...

  3. 使用webpack2.0 搭建react.js项目

    最近一段时间没有写react.js,发现webpack已经完全升级为2.0了,升级后导致以前的项目不能正常编译,只能重新实践一番 关于webpack2.0和1.x的区别概括起来就是tree shaki ...

  4. python-爬虫之re模块

    re模块中常用功能函数 1.compile() 编译正则表达式模式,返回一个对象的模式.(可以把那些常用的正则表达式编译成正则表达式对象,这样可以提高一点效率.) 格式: re.compile(pat ...

  5. yii1的笔记

    $sql = 'SELECT * FROM to8to_worker_item limit 10'; $res = Yii::app()->db->createCommand($sql)- ...

  6. IIS调试技术之 Debug Diagnostic (调试诊断)

    IIS 调试技术之 Debug Diagnostic (调试诊断) 1      概述 1.1  文档简介 系统出现错误或崩溃,免不了要进行调试.调试能进行的前提是错误能重现,但实际上要重现一个错误有 ...

  7. nodejs时间工具类

    /** * * @fmt 格式化字符串 * @Date 为需要格式化的日期 * * 示例:format(new Date(),'yyyy-MM-dd hh:mm:ss'); * 返回值为字符串 */ ...

  8. JQuery的一些基础知识

    JQuery的核心的一些方法 each(callback) '就像循环$("Element").length; ‘元素的个数,是个属性$("Element"). ...

  9. java实现mysql的备份还原

    此文章是基于 1. 搭建Jquery+SpringMVC+Spring+Hibernate+MySQL平台 2. jquery+springMVC实现文件上传 一. 简介 备份和导入是一个互逆的过程. ...

  10. jquery+springMVC实现文件上传

    此文章是基于 搭建Jquery+SpringMVC+Spring+Hibernate+MySQL平台 一. jar包介绍 1. commons-fileupload-1.3.1.jar 二. 相关程序 ...