zoukankan      html  css  js  c++  java
  • Max Sum of Max-K-sub-sequence 单调队列(有技巧)

    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
    ***************************************************************************************************************************
    单调队列(有技巧)
    ***************************************************************************************************************************
     1 #include<iostream>
     2 #include<string>
     3 #include<cstring>
     4 #include<cstdio>
     5 #include<cmath>
     6 #include<algorithm>
     7 #define  inf  0x7fffffff
     8 using namespace std;
     9 int a[100011],sum[201001];
    10 int que[201001];
    11 int cas,n,k;
    12 int st,en,result;
    13 void solve()
    14 {
    15     int head=1,tail=0;
    16     result=-inf;
    17     st=inf;
    18     int it;
    19     for(it=1;it<=n+k;it++)
    20     {
    21         while(head<=tail&&sum[it-1]<sum[que[tail]])//不满足条件,从尾部弹出
    22          tail--;
    23         while(head<=tail&&que[head]<it-k)//如果超出范围,从顶部弹出
    24          head++;
    25         tail++;
    26         que[tail]=it-1;//此处压值时有技巧
    27         if(sum[it]-sum[que[head]]>result)//每次更新极优值
    28         {
    29             result=sum[it]-sum[que[head]];
    30             st=que[head]+1;
    31             en=it;
    32         }
    33     }
    34     if(en>n)
    35      en-=n;
    36 }
    37 int main()
    38 {
    39     int i,j;
    40     scanf("%d",&cas);
    41     while(cas--)
    42     {
    43         sum[0]=0;
    44         scanf("%d %d",&n,&k);
    45         for(i=1;i<=n;i++)
    46         {
    47             scanf("%d",&a[i]);
    48             sum[i]=sum[i-1]+a[i];
    49         }
    50         for(i=n+1;i<=n+k;i++)
    51          sum[i]=sum[i-1]+a[i-n];
    52         solve();
    53         printf("%d %d %d
    ",result,st,en);
    54 
    55     }
    56 }
    View Code
  • 相关阅读:
    测试驱动开发的意义何在
    Web自动化测试模式page object的小利器:gizmo
    在NANT使用Nunit2标签运行Nunit测试
    小试牛刀 Ruby on Rails
    敏捷回顾会议的思考
    ThoughtWorks技术校园行第二波 课程资料 CleanCode&DirtyCode
    从git merge 和 git rebase想到……
    Ruby中的深浅拷贝
    NUnit Extension小介绍
    如何写好的测试呢?
  • 原文地址:https://www.cnblogs.com/sdau--codeants/p/3450678.html
Copyright © 2011-2022 走看看