HDU 3415(单调队列)
Max Sum of Max-K-sub-sequence
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 4080 Accepted Submission(s): 1453
Problem Description
Given a circle sequence A[1],A[2],A[3]......A[n]. Circle sequence means the left neighbour of A[1] is A[n] , and the right neighbour of A[n] is A[1].
Now your job is to calculate the max sum of a Max-K-sub-sequence. Max-K-sub-sequence means a continuous non-empty sub-sequence which length not exceed K.
Now your job is to calculate the max sum of a Max-K-sub-sequence. Max-K-sub-sequence means a continuous non-empty sub-sequence which length not exceed K.
Input
The first line of the input contains an integer T(1<=T<=100) which means the number of test cases.
Then T lines follow, each line starts with two integers N , K(1<=N<=100000 , 1<=K<=N), then N integers followed(all the integers are between -1000 and 1000).
Then T lines follow, each line starts with two integers N , K(1<=N<=100000 , 1<=K<=N), then N integers followed(all the integers are between -1000 and 1000).
Output
For each test case, you should output a line contains three integers, the Max Sum in the sequence, the start position of the sub-sequence, the end position of the sub-sequence. If there are more than one result, output the minimum start position, if still more than one , output the minimum length of them.
Sample Input
4 6 3 6 -1 2 -6 5 -5 6 4 6 -1 2 -6 5 -5 6 3 -1 2 -6 5 -5 6 6 6 -1 -1 -1 -1 -1 -1
Sample Output
7 1 3 7 1 3 7 6 2 -1 1 1
1 /* 2 题目大意:给出一个有N个数字(N<=10^5)的环状序列,让你求一个和最大的连续子序列。这个连续子序列的长度小于等于K。 3 输出和,起始和结束位置 4 */ 5 #include<iostream> 6 #include<queue> 7 using namespace std; 8 9 const int INF = 0x3fffffff; 10 const int maxn = 100010; 11 int num[maxn],sum[maxn]; 12 13 int main() 14 { 15 int T,i,j; 16 int N,K,n; 17 cin>>T; 18 while(T--) 19 { 20 cin>>N>>K; 21 sum[0]=0; 22 for(i=1;i<=N;i++) 23 { 24 cin>>num[i]; 25 sum[i]=sum[i-1]+num[i]; 26 } 27 for(i=N+1;i<N+K;i++) 28 { 29 sum[i]=sum[i-1]+num[i-N]; 30 } 31 n=N+K-1; 32 deque <int> q; 33 q.clear(); 34 int ans=-INF; 35 int start,end; 36 //枚举以j结尾的区间 37 for(j=1;j<=n;j++) 38 { 39 while(!q.empty() && sum[j-1]<sum[q.back()]) 40 q.pop_back(); 41 while(!q.empty() && q.front()<(j-K))//区间最大长度是K ,没有等号,因为入队的是 (j-1) 42 q.pop_front(); 43 q.push_back(j-1); 44 if(sum[j]-sum[q.front()]>ans) 45 { 46 ans=sum[j]-sum[q.front()]; 47 //i到j和是,sum[8] - sum[5] = sum[6]+sum[7]+sum[8]
start=q.front()+1; 48 end=j; 49 } 50 } 51 cout<<ans<<" "<<start<<" "<<(end>N?end%N:end)<<endl; 52 } 53 return 0; 54 }
////暴力枚举起点和终点估计会超时
作者:火星十一郎
本文版权归作者火星十一郎所有,欢迎转载和商用,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利.
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· .NET Core内存结构体系(Windows环境)底层原理浅谈
· C# 深度学习:对抗生成网络(GAN)训练头像生成模型
· .NET 适配 HarmonyOS 进展
· .NET 进程 stackoverflow异常后,还可以接收 TCP 连接请求吗?
· SQL Server统计信息更新会被阻塞或引起会话阻塞吗?
· 传国玉玺易主,ai.com竟然跳转到国产AI
· 本地部署 DeepSeek:小白也能轻松搞定!
· 自己如何在本地电脑从零搭建DeepSeek!手把手教学,快来看看! (建议收藏)
· 我们是如何解决abp身上的几个痛点
· 普通人也能轻松掌握的20个DeepSeek高频提示词(2025版)