HDU 3415(Max Sum of Max-K-sub-sequence-单调队列优化DP)

本文介绍了一道关于计算圆序列中长度不超过K的最大子序列和的问题,通过使用单调队列进行优化,提供了完整的代码实现及解析。

Max Sum of Max-K-sub-sequence

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 4729    Accepted Submission(s): 1723


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.
 

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

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
 

Author
shǎ崽@HDU
 

Source
 

Recommend
lcy
 

单调队列优化(裸的)
做完这题发觉自己完全不会单调队列
首先,sum[i]=min(s[i]-s[j]) (i-k<j<i) 
所以要求出一段的最小值,k向左移。
这样就是裸的单调队列

#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<functional>
#include<cmath>
#include<cctype>
#include<cassert>
#include<climits>
#include<queue>
using namespace std;
#define For(i,n) for(int i=1;i<=n;i++)
#define Rep(i,n) for(int i=0;i<n;i++)
#define Fork(i,k,n) for(int i=k;i<=n;i++)
#define ForD(i,n) for(int i=n;i;i--)
#define Forp(x) for(int p=pre[x];p;p=next[p])
#define RepD(i,n) for(int i=n;i>=0;i--)
#define MEM(a) memset(a,0,sizeof(a))
#define MEMI(a) memset(a,127,sizeof(a))
#define MEMi(a) memset(a,128,sizeof(a))
#define INF (2139062143)
#define F (1000000009)
#define MAXT (100+10)
#define MAXN (200000+10)
#define MAXAi (1000)
typedef long long ll;
struct node
{
	int i,v;
	node(){}
	node(int _i,int _v):i(_i),v(_v){}
	node(int _v):v(_v){}
	friend bool operator<(node a,node b){return a.v>b.v;}
//	friend bool operator>(node a,node b){return a.v>b.v;}	
};
struct mo_q
{
	int i,j;
	priority_queue <node> q;
	mo_q(){i=-1;j=0;}
	void ins(int v)
	{
		while (!q.empty()&&q.top().v>v) q.pop();
		q.push(node(++i,v));
	}
	void pop(){j++;}
	int qur()
   {
      while (!q.empty()&&q.top().i<j) q.pop();
      return q.top().i;
   }
	void clear(){i=-1,j=0;while (!q.empty()) q.pop();}
}q;
int T,n,k,a[MAXN],s[MAXN]={0};
int main()
{
//	freopen("hdu3415.in","r",stdin);
	scanf("%d",&T);
	while (T--)
	{
		scanf("%d%d",&n,&k);
		For(i,n) {scanf("%d",&a[i]);s[i]=s[i-1]+a[i];}
		For(i,n) {a[n+i]=a[i];s[n+i]=s[n+i-1]+a[n+i];}
		Rep(i,k) q.ins(s[i]);
		int ansl=1,ansr=1,ans=s[1];
		Fork(i,k,2*n)
      {
         int j=q.qur();
         int nowl=j+1>n?j+1-n:j+1,nowr=i>n?i-n:i,p=s[i]-s[j];
         if (p>ans) ansl=nowl,ansr=nowr,ans=p;
         else if (p==ans&&nowl<ansl) ansl=nowl,ansr=nowr;
         else if (p==ans&&nowl==ansl&&nowr<ansr) ansl=nowl,ansr=nowr;         
         q.ins(s[i]);
         q.pop();
      } 		
      cout<<ans<<' '<<ansl<<' '<<ansr<<endl;
      q.clear();
	}
	
	
//	while (1);
	return 0;
}







考虑柔性负荷的综合能源系统低碳经济优化调度【考虑碳交易机制】(Matlab代码实现)内容概要:本文围绕“考虑柔性负荷的综合能源系统低碳经济优化调度”展开,重点研究在碳交易机制下如何实现综合能源系统的低碳化与经济性协同优化。通过构建包含风电、光伏、储能、柔性负荷等多种能源形式的系统模型,结合碳交易成本与能源调度成本,提出优化调度策略,以降低碳排放并提升系统运行经济性。文中采用Matlab进行仿真代码实现,验证了所提模型在平衡能源供需、平抑可再生能源波动、引导柔性负荷参与调度等方面的有效性,为低碳能源系统的设计与运行提供了技术支撑。; 适合人群:具备一定电力系统、能源系统背景,熟悉Matlab编程,从事能源优化、低碳调度、综合能源系统等相关领域研究的研究生、科研人员及工程技术人员。; 使用场景及目标:①研究碳交易机制对综合能源系统调度决策的影响;②实现柔性负荷在削峰填谷、促进可再生能源消纳中的作用;③掌握基于Matlab的能源系统建模与优化求解方法;④为实际综合能源项目提供低碳经济调度方案参考。; 阅读建议:建议读者结合Matlab代码深入理解模型构建与求解过程,重点关注目标函数设计、约束条件设置及碳交易成本的量化方式,可进一步扩展至多能互补、需求响应等场景进行二次开发与仿真验证。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值