码迷,mamicode.com
首页 > 其他好文 > 详细

hdu3415:单调队列

时间:2015-12-01 23:02:20      阅读:188      评论:0      收藏:0      [点我收藏+]

标签:

感觉很不顺利,调了至少半个小时才调出来。原因是开头出的需要处理,i&&...如果缺了i&&,如果有数值是负数就会出错,第53行那里缺了+1,也是一直bug=>

1.每一个细节都要处理好,一个以为可以忽略的细节往往是关键;

2.出错了要耐心一步一步分析,中间输出看哪里错了,再耐心修改,即使废时间也要,不然这方面进步不了;

---------------------------------------------------------------------------------------------

#include<cstdio>
#include<deque>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
#define rep(i,n) for(int i=1;i<=n;i++)
#define clr(x,c) memset(x,c,sizeof(x))


int read(){
 int x=0;
 char c=getchar();
 int f=1;
 while(!isdigit(c)){
  if(c==‘-‘)
   f=-1;
  c=getchar();
 }
 while(isdigit(c)){
  x=x*10+c-‘0‘;
  c=getchar();
 }
 return x*f;
}


deque<int>nmin,minn;
int sum[200005];


int main(){
 int n=read();
 rep(i,n){
  int ans=-0x7fffffff;
  int s=0,t=0;
  while(!nmin.empty()) {
   nmin.pop_back();
   minn.pop_back();
  }
  clr(sum,0);
  int m=read(),k=read();
  rep(i,m){
      int tmp=read();
      sum[i]=sum[i-1]+tmp;
  }
  for(int i=m+1;i<=m*2;i++){
   sum[i]=sum[i-1]+sum[i-m]-sum[i-m-1];
  }
  /*for(int i=1;i<=m+m;i++){
   printf("%d ",sum[i]);
  }*/


  for(int i=0;i<=m+m;i++){
    if(i&&sum[i]-nmin.front()>ans){
     ans=sum[i]-nmin.front();
     s=minn.front()+1;
     t=i;
    }
    if(!nmin.empty()&&minn.front()<i-k+1){
     nmin.pop_front();
     minn.pop_front();
    }
    while(!nmin.empty()&&nmin.back()>sum[i]){
     nmin.pop_back();
     minn.pop_back();
    }
    nmin.push_back(sum[i]);
       minn.push_back(i);
      // printf("%d %d\n",nmin.front(),minn.front());
  }


  if(t>m)
   t%=m;
  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): 6764    Accepted Submission(s): 2483


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 
 

hdu3415:单调队列

标签:

原文地址:http://www.cnblogs.com/fighting-to-the-end/p/5011366.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!