1. 题目描述:

    E. Array Queries
    time limit per test

    2 seconds

    memory limit per test

    256 megabytes

    input

    standard input

    output

    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.

    Input

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

    Output

    Print q integers, ith integer must be equal to the answer to ith query.

    Example
    input
    3
    1 1 1
    3
    1 1
    2 1
    3 1
    

    output
    2
    1
    1
    

    Note

    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.

  2. 题意概述:给你一个n个元素的数组; 每个元素都在1..n之间; 然后给你q个询问; 每个询问由p和k构成; 会对p进行 p=p+a[p]+k操作若干次; 你要输出p第一次大于n之后操作了多少次;
  3. 解题思路:一种很好的想法是对于小于350的结果先计算出来,直接查询,大于350的结果再逐个dp。还有每次query的结果也可以放入dp内,以免下次继续查询。
  4. AC代码:
    #include <bits/stdc++.h>
    #define INF 0x3f3f3f3f
    #define maxn 100100
    #define lson root << 1
    #define rson root << 1 | 1
    #define lent (t[root].r - t[root].l + 1)
    #define lenl (t[lson].r - t[lson].l + 1)
    #define lenr (t[rson].r - t[rson].l + 1)
    #define N 1111
    #define eps 1e-6
    #define pi acos(-1.0)
    #define e exp(1.0)
    using namespace std;
    const int mod = 1e9 + 7;
    typedef long long ll;
    typedef unsigned long long ull;
    int a[maxn], dp[maxn][350];
    int main()
    {#ifndef ONLINE_JUDGEfreopen("in.txt", "r", stdin);freopen("out.txt", "w", stdout);long _begin_time = clock();
    #endifint n;while (~scanf("%d", &n)){for (int i = 1; i <= n; i++)scanf("%d", &a[i]);for (int i = n; i >= 1; i--)for (int j = 1; j < 350; j++){if (i + a[i] + j > n)dp[i][j] = 1;elsedp[i][j] = dp[i + a[i] + j][j] + 1;}int q;scanf("%d", &q);while (q--){int p, k;scanf("%d%d", &p, &k);if (k < 350)printf("%d\n", dp[p][k]);else{int cnt = 0;while (p <= n){cnt++;p = p + a[p] + k;}printf("%d\n", cnt);}}}
    #ifndef ONLINE_JUDGElong _end_time = clock();printf("time = %ld ms.", _end_time - _begin_time);
    #endifreturn 0;
    }

CF - 797E. Array Queries - dp+有选择地暴力相关推荐

  1. CF797E. Array Queries

    a is an array of n positive integers, all of which are not greater than n. You have to process q que ...

  2. CF1155D Beautiful Array 贪心,dp

    CF115DBeautiful Array 题目大意:给一个有n个元素的a数组,可以选择其中一个区间的所有数都乘上x,也可以不选,求最大子序列和. 如果没有前面的操作,就是只求最大子序列和,我们都知道 ...

  3. CodeForces - 1480D2 Painting the Array II(dp)

    题目链接:点击查看 题目大意:给出一个长度为 nnn 的序列,现在要求拆分成两个子序列,使得两个子序列的贡献之和最 小.对于一个序列的贡献就是,去掉相邻且相同的字母后的长度,即 ∑i=1n[a[i]! ...

  4. codeforces 361 D. Levko and Array(dp+二分)

    题目链接:http://codeforces.com/contest/361/problem/D 题意:最多可以修改K次数字,每次修改一个数字变成任意值,C=max(a[i+1]-a[i]):求操作之 ...

  5. JavaScript入门→HTML引用JS、变量、表达式操作符、数组Array数组对象、选择结构循环结构、函数、JavaScript与JAVA区别

    插入引用JS 变量 表达式 操作符 数组 选择判断结构 循环结构 函数 JavaScript与JAVA区别 绝学无忧. 唯之与阿,相去几何? 善之与恶,相去何若? 人之所畏,不可不畏. 荒兮其未央哉! ...

  6. CF 940E Cashback (DP+multiset)

    题目链接:http://codeforces.com/problemset/problem/940/E 题意:给一个长度为n的序列a,将它任意分成几个连续子序列,对于一个长度为K的子序列,可以删掉它前 ...

  7. CF 2 B(dp)

    一道dp例题,给出一个矩阵,要找到从左上到右下的一条路径,使这条路径上的数相乘的结果的末尾0的个数最少.先考虑整个矩阵的数都是正的.dp[i][j]要记录两个值,到这一点2的最少个数和5的最少个数,最 ...

  8. 主席树 ---- CF 1422F. Boring Queries(由离线推出在线如何求的 ,求解多次询问的区间LCM)

    题目链接 题目大意: 给你nnn个数, 每次往第iii个数里面里面乘aaa,问你这nnn个数的LCM\text{LCM}LCM是多少? 解题思路: 多个数的lcm不是所有数的乘积除以所有数的gcd,如 ...

  9. CF 1093G Multidimensional Queries——线段树(消去绝对值符号)

    题目:http://codeforces.com/contest/1093/problem/G 只好看看题解:https://codeforces.com/blog/entry/63877 主要是把绝 ...

最新文章

  1. 互联网公司IT系统架构进化之路
  2. html 下标签,html标签下
  3. python下载图片被覆盖了_scrapy 将抓取内容中的图片下载到本地并替换内容中的原始图片...
  4. Sonata 0.7
  5. JavaScript 引擎和 Just-in-Time 编译概念,Hot Function 的简单介绍
  6. 【Zigbee技术入门教程-02】一图读懂ZStack协议栈的核心思想与工作机理
  7. 解决浏览器无法登陆pki问题,删除浏览器缓存
  8. [codewars] - int32 to IPv4 二进制十进制 ip地址转换
  9. CRF和LSTM 模型在序列标注上的优劣?
  10. 职场小白靠这三招PDF技巧逆袭为职场达人
  11. 阿里巴巴Java编程规范试题答案
  12. 黑马程序员Mybatis
  13. 指数分布java_终于搞清楚正态分布、指数分布到底是啥了!
  14. videobox,一个错误的名字
  15. HTTPS 证书认证流程
  16. Libre OJ #10064 黑暗城堡(spfa+STL求短路)
  17. 安装spacy遇到的问题
  18. NUnit 入门知识
  19. 7、JSON数据和Java对象的相互转换(客户端和服务器对象数据通讯用)
  20. Java钉钉开发_异常_01_error code:50002, error message:请求的员工userid不在授权范围内...

热门文章

  1. 【雷达通信】基于matlab无人机FMCW毫米波高度计雷达仿真【含Matlab源码 1261期】
  2. 让文章排版看起来更舒服的方法
  3. 超污的23种设计模式
  4. 尚鼎峰:抖音播放量不够高?如何制作吸睛封面?
  5. mac配置idea-scala-spark开发环境
  6. 弘辽科技:拼多多最容易拿到免费流量的两大核心。
  7. 联想主板更改UEFI启动顺序
  8. 数据库实验三--OpenGauss(查询和更新数据)
  9. selenium爬取煎蛋网
  10. javascript的参数_如何使用JavaScript制作参数家具