Neko's loop

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 356    Accepted Submission(s): 56

Problem Description
Neko has a loop of size n.
The loop has a happy value ai on the i−th(0≤i≤n−1) grid. 
Neko likes to jump on the loop.She can start at anywhere. If she stands at i−th grid, she will get ai happy value, and she can spend one unit energy to go to ((i+k)modn)−th grid. If she has already visited this grid, she can get happy value again. Neko can choose jump to next grid if she has energy or end at anywhere. 
Neko has m unit energies and she wants to achieve at least s happy value.
How much happy value does she need at least before she jumps so that she can get at least s happy value? Please note that the happy value which neko has is a non-negative number initially, but it can become negative number when jumping.
 
Input
The first line contains only one integer T(T≤50), which indicates the number of test cases. 
For each test case, the first line contains four integers n,s,m,k(1≤n≤104,1≤s≤1018,1≤m≤109,1≤k≤n).
The next line contains n integers, the i−th integer is ai−1(−109≤ai−1≤109)
 
Output
For each test case, output one line "Case #x: y", where x is the case number (starting from 1) and y is the answer.
 
Sample Input
2
3 10 5 2
3 2 1
5 20 6 3
2 3 2 1 5
 
Sample Output
Case #1: 0
Case #2: 2
 
Source
 
Recommend
chendu

刚开始看 觉得是n^2的暴力 ,然后被蒋大佬说 必须O(n)过,最后在WA了四发以后,比赛最后半个小时A了这道题

n个数,最多跳m步,每次跳到(i+k)%n,然后求距 s 的最小差,大于s 计为0

很朴素的想法 枚举每个i从0到n-1 然后暴力跑循环节

假设循环节大小为len,最后 res = max(0, getRes(len)) *m/len + max(0, getRes(m%len)); getRes(x) 就是求一个循环节上 长度最大为x的最长子段和(注意是子段 不是子序列)

可以预处理O(n)求出每个循环节, 然后对每个循环节 求上面的结果

  1. #include <iostream>
  2. #include <cstring>
  3. #include <algorithm>
  4. #include <cmath>
  5. #include <vector>
  6.  
  7. typedef long long ll;
  8. using namespace std;
  9.  
  10. const int N = 1e4+;
  11.  
  12. int n,m,k,cnt ;
  13. ll s, MAX, v[N];
  14. bool vis[N]; vector<ll> g[N];
  15. ll que[N<<],mx[N<<],sta[N<<];
  16.  
  17. ll solve(const vector<ll>&vv, int count) {
  18. int sz= vv.size();
  19. for(int i=;i<sz;i++)
  20. que[i] = que[i+sz] = vv[i];
  21. sz = sz<<;
  22. int st=,ed=;
  23. ll res=;
  24. for(int i=;i<sz;i++) {
  25. if(i==)
  26. mx[i] = que[i];
  27. else
  28. mx[i] = mx[i-]+que[i];
  29.  
  30. if(i < count)
  31. res = max(res, mx[i]);
  32.  
  33. while (st < ed && sta[st]+count < i)
  34. st++;
  35. if(st < ed)
  36. res = max(res, mx[i] - mx[sta[st]]);
  37. while (st < ed && mx[i] <= mx[sta[ed-]])
  38. ed--;
  39. sta[ed++]=i;
  40. }
  41. return res;
  42. }
  43.  
  44. ll getRes(const vector<ll>& vv,int step,ll top) {
  45. ll mod = step % vv.size(); ll kk = step/ vv.size();
  46. ll sum = ;
  47. for(int i=; i<vv.size();i++)
  48. sum += vv[i];
  49. ll mx1 = solve(vv, mod);
  50. ll mx2 = solve(vv, vv.size());
  51. mx1 += max(0LL, sum)*kk;
  52. mx2 += max(0LL, sum)*((kk>)?kk-:);
  53. return max(mx1,mx2);
  54. }
  55.  
  56. int main ()
  57. {
  58. //freopen("in.txt","r",stdin);
  59. int T; scanf("%d",&T);
  60. for(int cas=; cas<=T; cas++) {
  61. memset(vis,,sizeof(vis));
  62. scanf("%d %lld %d %d", &n, &s, &m, &k);
  63. for(int i=;i<n;i++)
  64. scanf("%lld", &v[i]);
  65. cnt=; MAX=;
  66. for(int i=; i<n; i++) {
  67. g[cnt].clear();
  68. if(!vis[i]) {
  69. vis[i]=;
  70. g[cnt].push_back(v[i]);
  71. for(int j=(i+k)%n; j!=i && !vis[j]; j=(j+k)%n) {
  72. g[cnt].push_back(v[j]);
  73. vis[j]=;
  74. }
  75. //for(int j=0;j<g[cnt].size();j++)
  76. //cout << g[cnt][j]<<" ";
  77. //cout <<endl;
  78. MAX = max(MAX, getRes(g[cnt], m, s));
  79. cnt++;
  80. }
  81. }
  82. if(MAX >= s) MAX=;
  83. else MAX = s-MAX;
  84. printf("Case #%d: %lld\n", cas, MAX);
  85. }
  86. return ;
  87. }

hdu 6444 Neko's loop 单调队列优化DP的更多相关文章

  1. hdu 5945 Fxx and game(单调队列优化DP)

    题目链接:hdu 5945 Fxx and game 题意: 让你从x走到1的位置,问你最小的步数,给你两种走的方式,1.如果k整除x,那么你可以从x走一步到k.2.你可以从x走到j,j+t<= ...

  2. 【单调队列优化dp】HDU 3401 Trade

    http://acm.hdu.edu.cn/showproblem.php?pid=3401 [题意] 知道之后n天的股票买卖价格(api,bpi),以及每天股票买卖数量上限(asi,bsi),问他最 ...

  3. bzoj1855: [Scoi2010]股票交易 单调队列优化dp ||HDU 3401

    这道题就是典型的单调队列优化dp了 很明显状态转移的方式有三种 1.前一天不买不卖: dp[i][j]=max(dp[i-1][j],dp[i][j]) 2.前i-W-1天买进一些股: dp[i][j ...

  4. BestCoder Round #89 02单调队列优化dp

    1.BestCoder Round #89 2.总结:4个题,只能做A.B,全都靠hack上分.. 01  HDU 5944   水 1.题意:一个字符串,求有多少组字符y,r,x的下标能组成等比数列 ...

  5. 单调队列优化DP,多重背包

    单调队列优化DP:http://www.cnblogs.com/ka200812/archive/2012/07/11/2585950.html 单调队列优化多重背包:http://blog.csdn ...

  6. Parade(单调队列优化dp)

    题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=2490 Parade Time Limit: 4000/2000 MS (Java/Others)    ...

  7. 单调队列优化DP——习题收集

    前言 感觉可以用单调队列优化dp的模型还是挺活的,开个随笔记录一些遇到的比较有代表性的模型,断续更新.主要做一个收集整理总结工作. 记录 0x01 POJ - 1821 Fence,比较适合入门的题, ...

  8. bzoj1855: [Scoi2010]股票交易--单调队列优化DP

    单调队列优化DP的模板题 不难列出DP方程: 对于买入的情况 由于dp[i][j]=max{dp[i-w-1][k]+k*Ap[i]-j*Ap[i]} AP[i]*j是固定的,在队列中维护dp[i-w ...

  9. hdu3401:单调队列优化dp

    第一个单调队列优化dp 写了半天,最后初始化搞错了还一直wa.. 题目大意: 炒股,总共 t 天,每天可以买入na[i]股,卖出nb[i]股,价钱分别为pa[i]和pb[i],最大同时拥有p股 且一次 ...

随机推荐

  1. Unity3D中使用Profiler精确定位性能热点的优化技巧

    本文由博主(SunboyL)原创,转载请注明出处:http://www.cnblogs.com/xsln/p/BeginProfiler.html 简介 在使用Profiler定位代码的性能热点时,很 ...

  2. nodejs(五)同步异步--USING SETTIMEOUT INSTEAD OF SETINTERVAL TO FORCE SERIALIZATION

    Let’s say you want a function that does some I/O — such as parsing a log fi le — that will periodica ...

  3. 【转】锁(lock)知识及锁应用

    sql server锁(lock)知识及锁应用转自:http://blog.csdn.net/huwei2003/article/details/4047191 关键词:锁提示,锁应用 提示:这里所摘 ...

  4. HDU 1068 Girls And Boys 二分图题解

    版权声明:本文作者靖心.靖空间地址:http://blog.csdn.net/kenden23/,未经本作者同意不得转载. https://blog.csdn.net/kenden23/article ...

  5. HPC高性能计算知识: 异构并行计算

    版权声明:很多其它内容,请关注[架构师技术联盟]公众号 https://blog.csdn.net/BtB5e6Nsu1g511Eg5XEg/article/details/80059122 当摩尔定 ...

  6. android返回到第一个activity

    问题:Android顺序打开多个Activity,如何返回到第一个Activity(一般为首页)? 情形:如 A 打开 B, B 打开 C, C 打开 D, 然后如果从 D 一步返回到 A,并清楚掉 ...

  7. const与常量,傻傻分不清楚~

    当数组的长度在运行中才能够确定时,普通的静态数组无法满足要求,此时需要动态数组来实现. 今天突然想,const变量在初始化时可以接受变量的赋值,那么可不可以用它来定义一个静态数组呢?于是有下面的尝试: ...

  8. glide从入门到精通使用

    介绍 不论是开发Java还是你正在学习的Golang,都会遇到依赖管理问题.Java有牛逼轰轰的Maven和Gradle. Golang亦有godep.govendor.glide.gvt.gopac ...

  9. android 代码edittext删除或者替换光标处的字串

    https://stackoverflow.com/questions/3609174/android-insert-text-into-edittext-at-current-position Cp ...

  10. [py]py2自带Queue模块实现了3类队列

    py2自带Queue实现了3类队列 先搞清楚几个单词 Queue模块实现了三类队列: FIFO(First In First Out,先进先出,默认为该队列), 我们平时泛指的队列, LIFO(Las ...