Neko has a loop of size nn. 
The loop has a happy value aiai on the i−th(0≤i≤n−1)i−th(0≤i≤n−1) grid. 
Neko likes to jump on the loop.She can start at anywhere. If she stands at i−thi−thgrid, she will get aiai happy value, and she can spend one unit energy to go to ((i+k)modn)−th((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 mm unit energies and she wants to achieve at least ss happy value. 
How much happy value does she need at least before she jumps so that she can get at least ss 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.

InputThe first line contains only one integer T(T≤50)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)n,s,m,k(1≤n≤104,1≤s≤1018,1≤m≤109,1≤k≤n). 
The next line contains nn integers, the i−thi−th integer is ai−1(−109≤ai−1≤109)ai−1(−109≤ai−1≤109) 
OutputFor 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

题意:给n个数字,当位于某一个数字时,可以得到这个数字的快乐值(可以重复获得),可以走m步,每次向后跳k格(循环),问快乐值要达到s,那么初始快乐值的最小值是多少?思路:这是2018ccpc的网络赛的题,暑假看着学长打比赛,在傍边连读题都帮不上。。。。

首先有一个显而易见的结论,对于某一个起点,跳着跳着就会回到起点,也就是说,一定存在循环节。其次显而易见的,就是对于某一个数,只会存在于一个循环节中。那么如果不考虑循环节的起点差异,找出所有循环节就是O(n)的。而实际上我们确实不用考虑,原因接下来再说。

我们可以轻易的算出某个循环节中所有元素的和--sum,因为存在负数,所以sum可能是负数。再声明一下,len是循环节长度。若sum<0,那么最多只需走min(m,len)步就可以得到最优解。若sum>0,则需要先走m/len-1圈,为什么减一,其他博客有详解(博主太懒了),然后最多走m-(m/len-1)*len)步
先说这后面多出来的步数,假设是p步,这p步在sum<0时,p<=len,sum<0时,len<=p<=2*len。为了求出这p步走出的最大值,我们使用单调队列。首先求出循环节的前缀和sum,单调队列维护长度为p的滑窗的sum最小值。维护  ans =( sum[i]-单调队列最小值 ) 的最大值就可以啦!由于p的长度,要把循环节扩展3倍哟。

然后我们看到,这个单调队列,在ans取得最大值时,单调队列的最小值位置是不定的,所以很容易想到,不一定就是循环节找出来时的起点,所以我们很容易想到,这一个过程,相当于已经枚举了实际问题中的起点。也就是,我们在取ans的最大值时的最小值位置,就可以当实际问题中的起点。

#include<iostream>
#include<algorithm>
#include<vector>
#include<stack>
#include<queue>
#include<map>
#include<set>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<ctime>
#define fuck(x) cout<<#x<<" = "<<x<<endl;
#define debug(a,i) cout<<#a<<"["<<i<<"] = "<<a[i]<<endl;
#define ls (t<<1)
#define rs ((t<<1)+1)
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const int maxn = 100086;
const int maxm = 100086;
const int inf = 2.1e9;
const ll Inf = 999999999999999999;
const int mod = 1000000007;
const double eps = 1e-6;
const double pi = acos(-1);
ll num[maxn];
vector<ll>vec;
bool vis[maxn];
ll sum[maxn];
ll n,s,m,k;
int len;
struct node
{ll x;int id;
};
deque<node>q;
ll solve(int p){q.clear();for(int i=0;i<len;i++){vec.push_back(vec[i]);}for(int i=0;i<len;i++){vec.push_back(vec[i]);}ll ans=0;sum[0]=vec[0];for(int i=1;i<len*3;i++){sum[i]=sum[i-1]+vec[i];}for(int i=0;i<len*3;i++){while(!q.empty()&&q.back().x>sum[i]){q.pop_back();}if(!q.empty()&&q.front().id<i-p){q.pop_front();}q.push_back(node{sum[i],i});ans=max(ans,1ll*sum[i]-q.front().x);}return ans;
}int main()
{int T;scanf("%d",&T);int cases=0;while(T--){ll ans=0;cases++;scanf("%lld%lld%lld%lld",&n,&s,&m,&k);for(int i=1;i<=n;i++){scanf("%lld",&num[i]);}memset(vis,0,sizeof(vis));for(int i=1;i<=n;i++){vec.clear();if(vis[i]){continue;}int pos=i;for(int j=1;j<=m;j++){vec.push_back(num[pos]);vis[pos]=true;pos+=k;if(pos>n){pos%=n;}if(vis[pos]){break;}}len=vec.size();ll sum=0;for(int i=0;i<len;i++){sum+=vec[i];}if(sum<0){ans=max(ans,solve(len));}else{ans=max(ans,solve(m-(m/len-1)*len)+(m/len-1)*sum);}}printf("Case #%d: %lld\n",cases,max(0ll,s-ans));}return 0;
}

View Code

转载于:https://www.cnblogs.com/ZGQblogs/p/10806800.html

HDU 6444 Neko's loop(单调队列)相关推荐

  1. Hdu 2430 Beans (数据结构_单调队列)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2430 题目大意:有n坨豌豆,每坨都有w[i]个,现在要从中选择连续的若干坨,然后用一个能装p个豌豆的背 ...

  2. HDU 5945 Fxx and game 单调队列优化dp

    题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=5945 题目 Young theoretical computer scientist Fxx desi ...

  3. HDU 3507 Print Article(单调队列)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3507 题意:给出一个数列C,一个数字M,将数列分成若干段,每段的代价为(设这段的数字为k个): 求一种 ...

  4. 2018CCPC网络赛 HDU 6444: G. Neko's loop(线段树)

    Neko's loop Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Tota ...

  5. hdu 3706 Second My Problem First 单调队列

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3706 Second My Problem First Time Limit: 12000/4000 M ...

  6. HDU 6194 string string string :后缀数组+单调队列 | 后缀自动机

    题意:给出一个字符串,求出出现了恰好k次的子串的个数. 题解:恰好k次 = 至少k次 - 至少k+1次.答案转化为求至少出现k次的子串个数统计.构造好后缀数组以及很重要的Height数组之后.用一个k ...

  7. Hdu 4193 Non-negative Partial Sums (数据结构_单调队列)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=4193 题目大意: 给定一个长度为n的循环序列,从n个不同位置开始,问有几个位置使得一下情况成立:所有 ...

  8. HDU 4122 Alice's mooncake shop 单调队列优化dp

    Alice's mooncake shop Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem ...

  9. hdu 3401 Trade(单调队列优化dp)

    hdu 3401 Trade(单调队列优化dp) 题意:lxhgww喜欢炒股票,他可以在第i天买入或者卖出若干张股票(一天只能买或者卖),两个交易日之间至少相隔w天,问他t天后最多能赚多少. 解题思路 ...

最新文章

  1. c语言中10转8和16的转换,(C语言)10进制转换2,8,16进制
  2. PHP从零开始--基础篇
  3. [NewLife.XCode]百亿级性能
  4. .net2.0 orm_Hibernate 4.3 ORM工具
  5. 浅谈进程间的消息传递
  6. android支付宝支付开发过程
  7. jquery2.1.1 checkbox
  8. Python实战之Selenium自动化测试web刷新FW
  9. tda7415c参数_TDA7415_TDA7415PDF资料详细参数下载_Powered by 奥伟斯
  10. C#-实现微信激活会员卡后响应激活动作并获取会员信息
  11. 电脑扬声器耳机已拔出
  12. (Ⅱ)NexT主题的优化定制修改指南
  13. 2019CCPC湖南全国邀请赛(广东省赛、江苏省赛)重现赛
  14. 青蛙过河程序及其解析
  15. jTopo入门 简单实现拓扑图
  16. 编程从键盘输入一个字符串,统计该字符串中从‘a‘到‘z‘共26个小写字母各自出现的次数, 将结果存入数组中,并输出
  17. FSK,PSK,ASK,BPSK调制
  18. windows API 第八篇 _tcsicmp _stricmp _wcsicmp _mbsicmp
  19. 2015移动互联网App行业发展趋势绿皮书
  20. 微笑到颠笑,各种笑不停的英语说法

热门文章

  1. 怎么用python自动梳理表格_Python将多份excel表格整理成一份表格
  2. 2021年淮南高考成绩查询,2021年淮南高考最高分多少分,历年淮南高考状元
  3. python远程linux服务器执行命令_基于使用paramiko执行远程linux主机命令(详解)
  4. aic值检验 p值_23. 假设检验的时候为什么常写p lt; 0.05,而不写具体的p值?
  5. cent os 7 mysql_centos – 百胜:Cent OS 7中没有包mysql-server
  6. mysql mvcc 读写阻塞_mysql面试题MVCC原理事务隔离级别_aiailingfei的博客-CSDN博客
  7. 使用PS从图片中抠取签章部分
  8. C++面试题:内存的分配方式有几种?
  9. Java零基础学习(三)封装继承多态
  10. Android 常用adb shell 命令