标签:
感觉很不顺利,调了至少半个小时才调出来。原因是开头出的需要处理,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;
}
---------------------------------------------------------------------------------------------
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 6764 Accepted Submission(s): 2483
标签:
原文地址:http://www.cnblogs.com/fighting-to-the-end/p/5011366.html