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 = ;
const int maxm = ;
const int inf = 2.1e9;
const ll Inf = ;
const int mod = ;
const double eps = 1e-;
const double pi = acos(-);
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=;i<len;i++){
vec.push_back(vec[i]);
}
for(int i=;i<len;i++){
vec.push_back(vec[i]);
}
ll ans=;
sum[]=vec[];
for(int i=;i<len*;i++){
sum[i]=sum[i-]+vec[i];
}
for(int i=;i<len*;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=;
while(T--){
ll ans=;
cases++;
scanf("%lld%lld%lld%lld",&n,&s,&m,&k);
for(int i=;i<=n;i++){
scanf("%lld",&num[i]);
}
memset(vis,,sizeof(vis));
for(int i=;i<=n;i++){
vec.clear();
if(vis[i]){continue;}
int pos=i;
for(int j=;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=;
for(int i=;i<len;i++){
sum+=vec[i];
}
if(sum<){
ans=max(ans,solve(len));
}
else{
ans=max(ans,solve(m-(m/len-)*len)+(m/len-)*sum);
}
}
printf("Case #%d: %lld\n",cases,max(0ll,s-ans));
}
return ;
}

HDU 6444 Neko's loop(单调队列)的更多相关文章

  1. hdu 6444 Neko's loop 单调队列优化DP

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

  2. HDU - 6444 Neko's loop(循环节+最大子段和)

    http://acm.hdu.edu.cn/showproblem.php?pid=6444 题意 一个有n个数的环,每次循环走k步,走到每个点都有具体的权值,问在任意点出发最多走m步的情况下,一开始 ...

  3. HDU 6444 Neko's loop ( 2018 CCPC 网络赛 && 裴蜀定理 && 线段树 )

    题目链接 题意 : 给出一个 n 个元素的环.可以任意选择起点.选完起点后.可以行走 m 步.每次前进 k 个单位.所走到的点将产生正或负贡献.问你一开始得准备多少才能使得初始资金加上在环上获取最大利 ...

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

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

  5. hdu 3410 Passing the Message(单调队列)

    题目链接:hdu 3410 Passing the Message 题意: 说那么多,其实就是对于每个a[i],让你找他的从左边(右边)开始找a[j]<a[i]并且a[j]=max(a[j])( ...

  6. hdu 6319 逆序建单调队列

    题目传送门//res tp hdu 维护递增单调队列 根据数据范围推测应为O(n)的. 我们需要维护一个区间的信息,区间内信息是"有序"的,同时需要在O(1)的时间进行相邻区间的信 ...

  7. HDU - 5289 Assignment (RMQ+二分)(单调队列)

    题目链接: Assignment  题意: 给出一个数列,问其中存在多少连续子序列,使得子序列的最大值-最小值<k. 题解: RMQ先处理出每个区间的最大值和最小值(复杂度为:n×logn),相 ...

  8. HDU 2490 Parade(DPの单调队列)(2008 Asia Regional Beijing)

    Description Panagola, The Lord of city F likes to parade very much. He always inspects his city in h ...

  9. HDU - 5289:Assignment(单调队列||二分+RMQ||二分+线段树)

    Tom owns a company and he is the boss. There are n staffs which are numbered from 1 to n in this com ...

随机推荐

  1. 分享 KubeCon 2019 (上海)关于 Serverless 及 Knative 相关演讲会议

    有幸参加了 KubeCon 2019 上海大会,并参加了 Knative 及 Serverless 相关的几场分享会,收获满满.这里简单介绍一下各个演讲主题的主要内容.详细的演讲主题文档可以在Kube ...

  2. 巨蟒python全栈开发-第11阶段 ansible_project2

    一个NB的网站: https://www.toolfk.com/ CDN:将用户的需求送到最近的节点:内容分发网络 有些是专门做CDN的工具 常用的markdown是需要知道的,短信有字数限制. we ...

  3. Kafka 简易教程

    1.初识概念 Apache Kafka是一个分布式消息发布订阅系统. TopicKafka将消息种子(Feed)分门别类, 每一类的消息称之为话题(Topic). Producer发布消息的对象称之为 ...

  4. c#操作sqlite db3数据库

    首先添加引用 System.Data.SQLite.dll,引用只用添加这个,但SQLite.Interop.dll文件必须也和它同时放在Debug目录下 然后可用: SQLiteConnection ...

  5. 伪静态的实现方法:IIS环境下配置

    URL 静态化可以提高搜索引擎抓取,开启本功能需要对 Web 服务器增加相应的 Rewrite 规则,且会轻微增加服务器负担.本教程讲解如何在 IIS 环境下配置各个产品的 Rewrite 规则. 下 ...

  6. Xib设计UITableViewCell然后动态加载

    转自: http://www.2cto.com/kf/201202/120764.html (注:环境Mac OS X Lion 10.7.3 + Xcode 4.2.1 + iOS SDK 5.0. ...

  7. selenium webdriver学习(四)------------定位页面元素(转)

    selenium webdriver学习(四)------------定位页面元素 博客分类: Selenium-webdriver seleniumwebdriver定位页面元素findElemen ...

  8. 2018-10-20-WPF-通过位处理合并图片

    title author date CreateTime categories WPF 通过位处理合并图片 lindexi 2018-10-20 16:53:49 +0800 2018-10-20 1 ...

  9. H3C 错误提示信息

  10. 【Vue】基于nodejs的vue项目打包编译部署

    一·项目编译 1·进入项目目录下的终端执行命令 npm run build 正常情况如下图,如遇到错误不会编译成功,且编译后的html文件不能正常渲染. 2·编译完成后进入项目下的dist目录运行生成 ...