【hdu3415】【单调队列 】Max Sum of Max-K-sub-sequence【求长度不大于k的区间最大子串和】

探讨解决环状序列中寻找长度不超过K的最大和子序列问题的方法,利用单调队列优化算法实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

传送门:http://acm.hdu.edu.cn/showproblem.php?pid=3415

题意:给出一个有N个数字(N<=10^5)的环状序列,让你求一个和最大的连续子序列(子串)。这个连续子序列的长度小于等于K。 

思路:

因为序列是环状的,所以可以在序列后面复制前k-1个数字。

如果用s[i]来表示复制过后的序列的前i个数的和,那么任意一个子序列[i..j]的和就等于s[j]-s[i-1]。
对于每一个j,用s[j]减去最小的一个s[i](i>=j-k)就可以得到以j为终点长度不大于k的和最大的序列了。将原问题转化为这样一个问题后,就可以用单调队列解决了。 


单调队列即保持队列中的元素单调递增(或递减)的这样一个队列,可以从两头删除,只能从队尾插入。单调队列的具体作用在于,由于保持队列中的元素满足单调性,
对于上述问题中的每个j,可以用O(1)的时间找到对应的s[i]。(保持队列中的元素单调递增的话,队首元素便是所要的元素了)。 


维护方法:对于每个j,我们插入s[j-1]的下标,插入时从队尾插入。为了保证队列的单调性,我们从队尾开始删除元素,直到队尾元素对应的值比当前需要插入的s[j-1]小,
就将当前元素下标插入到队尾。之所以可以将之前的队列尾部元素全部删除,是因为它们已经不可能成为最优的元素了,因为当前要插入的元素位置比它们靠前,
对应的值比它们小。我们要找的,是满足(i>=j-k)的i中最小的s[i]。在插入元素后,从队首开始,将不符合限制条件(i<j-k)的元素全部删除,此时队列一定不为空。(因为刚刚插入了一个一定符合条件的元素)。


代码:

#include <bits/stdc++.h>
using  namespace  std;
#define rep(i,k,n) for(int i=k;i<=n;i++)

template<class T> void read(T&num) {
    char CH; bool F=false;
    for(CH=getchar();CH<'0'||CH>'9';F= CH=='-',CH=getchar());
    for(num=0;CH>='0'&&CH<='9';num=num*10+CH-'0',CH=getchar());
    F && (num=-num);
}

const int inf=0x3f3f3f3f;
const int N=1e5+10;

int a[N], sum[2 * N];
int n, k;

int  main(){
  int T;
  read(T);
  while(T--){
    read(n), read(k);
    rep(i, 1, n)
      read(a[i]), sum[i] = sum[i - 1] + a[i];
    for(int i = n + 1; i < n + k; i++)
      sum[i] = sum[i - 1] + a[i - n];
    int tmp = n;
    n = n + k - 1;
    
    deque<int>deq; int s, t, ans = -inf;
    deq.clear();
    rep(i, 1, n){
      while(!deq.empty() && sum[deq.back()] > sum[i - 1])//不是>=,为了保留the minimum start position
        deq.pop_back();  //保持队列的单调性  
      while(!deq.empty() && deq.front() < i - k)
        deq.pop_front(); //超过k的长度则消除队列前面的元素 
      deq.push_back(i - 1);
      if(sum[i] - sum[deq.front()] > ans){//记录,sum[n]-sum[m]所得出的是m+1到n之间的和  
        ans = sum[i] - sum[deq.front()];
        s = deq.front() + 1;
        t = i;
      }
    }
    if(t > tmp)t %= tmp;
    printf("%d %d %d\n", ans, s, t);
  }
  return 0;
}
描述:

Max Sum of Max-K-sub-sequence

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


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 
 



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值