hdu3415——Max Sum of Max-K-sub-sequence

Max Sum of Max-K-sub-sequence

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


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   |   We have carefully selected several similar problems for you:   3423  3417  3418  3419  3421

先把环变成链,然后就是在1 - 2 * n - 1之间求
求出前缀和,那么ans = max(sum[i] - sum[j - 1])而且 i - j + 1 <=k

用一个单调队列来维护sum[j - 1],边维护边记录答案

PS:这题应该还能用线段树,RMQ等方法解决

#include <map>
#include <set>
#include <list>
#include <queue>
#include <stack>
#include <vector>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>

using namespace std;

const int N = 200010;

int dp[N], arr[N];
int in_que[N], sum[N];

int main()
{
	int n, t, k;
	scanf("%d", &t);
	while (t--)
	{
		scanf("%d%d", &n, &k);
		sum[0] = 0;
		for (int i = 1; i <= n; ++i)
		{
			scanf("%d", &arr[i]);
			sum[i] = sum[i - 1] + arr[i];
		}
		for (int i = n + 1; i < 2 * n; ++i)
		{
			sum[i] = sum[i - 1] + arr[i - n];
		}
		int front = 0, rear = 0;
		int s, e, maxs = -0x3f3f3f3f;
		for (int i = 1; i <= k; ++i)
		{

			while (front < rear && sum[in_que[rear - 1]] >= sum[i - 1])
			{
				rear--;
			}
			in_que[rear++] = i - 1;
			// printf("%d %d\n", sum[in_que[front]], sum[i]);
			if (maxs < sum[i] - sum[in_que[front]])
			{
				maxs = sum[i] - sum[in_que[front]];
				s = in_que[front] + 1;
				e = i;
			}
			else if (maxs == sum[i] - sum[in_que[front]] && in_que[front] + 1 < s)
			{
				s = in_que[front] + 1;
			}
			else if (maxs == sum[i] - sum[in_que[front]] && in_que[front] + 1 == s && e > i)
			{
				e = i;
			}
			// printf("%d\n", in_que[front]);
		}
		for (int i = k + 1; i < 2 * n; ++i)
		{
			while (front < rear && sum[in_que[rear - 1]] >= sum[i - 1])
			{
				rear--;
			}
			in_que[rear++] = i - 1;
			if (in_que[front] < i - k)
			{
				front++;
			}
			if (maxs < sum[i] - sum[in_que[front]])
			{
				maxs = sum[i] - sum[in_que[front]];
				s = in_que[front] + 1;
				e = i;
			}
			else if (maxs == sum[i] - sum[in_que[front]] && in_que[front] + 1 < s)
			{
				s = in_que[front] + 1;
			}
			else if (maxs == sum[i] - sum[in_que[front]] && in_que[front] + 1 == s && e > i)
			{
				e = i;
			}
		}
		if (s > n)
		{
			s -= n;
		}
		if (e > n)
		{
			e -= n;
		}
		printf("%d %d %d\n", maxs, s, e);
	}
	return 0;
}


 
数据驱动的两阶段分布鲁棒(1-范数和∞-范数约束)的电热综合能源系统研究(Matlab代码实现)内容概要:本文围绕“数据驱动的两阶段分布鲁棒(1-范数和∞-范数约束)的电热综合能源系统研究”展开,提出了一种结合数据驱动与分布鲁棒优化方法的建模框架,用于解决电热综合能源系统在不确定性环境下的优化调度问题。研究采用两阶段优化结构,第一阶段进行预决策,第二阶段根据实际场景进行调整,通过引入1-范数和∞-范数约束来构建不确定集,有效刻画风电、负荷等不确定性变量的波动特性,提升模型的鲁棒性和实用性。文中提供了完整的Matlab代码实现,便于读者复现和验证算法性能,并结合具体案例分析了不同约束条件下系统运行的经济性与可靠性。; 适合人群:具备一定电力系统、优化理论和Matlab编程基础的研究生、科研人员及工程技术人员,尤其适合从事综合能源系统、鲁棒优化、不确定性建模等相关领域研究的专业人士。; 使用场景及目标:①掌握数据驱动的分布鲁棒优化方法在综合能源系统中的应用;②理解1-范数和∞-范数在构建不确定集中的作用与差异;③学习两阶段鲁棒优化模型的建模思路与Matlab实现技巧,用于科研复现、论文写作或工程项目建模。; 阅读建议:建议读者结合提供的Matlab代码逐段理解算法实现细节,重点关注不确定集构建、两阶段模型结构设计及求解器调用方式,同时可尝试更换数据或调整约束参数以加深对模型鲁棒性的理解。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值