您的位置:首页 > 其它

poj 2566 Bound Found(尺取法 好题)

2015-08-28 02:46 405 查看
Description

Signalsofmostprobablyextra-terrestrialoriginhavebeenreceivedanddigitalizedbyTheAeronauticandSpaceAdministration(thatmustbegoingthroughadefiantphase:"ButIwanttousefeet,notmeters!").Eachsignalseemstocomeintwoparts:asequenceofnintegervaluesandanon-negativeintegert.We'llnotgointodetails,butresearchersfoundoutthatasignalencodestwointegervalues.Thesecanbefoundasthelowerandupperboundofasubrangeofthesequencewhoseabsolutevalueofitssumisclosesttot.
Youaregiventhesequenceofnintegersandthenon-negativetargett.Youaretofindanon-emptyrangeofthesequence(i.e.acontinuoussubsequence)andoutputitslowerindexlanditsupperindexu.Theabsolutevalueofthesumofthevaluesofthesequencefromthel-thtotheu-thelement(inclusive)mustbeatleastasclosetotastheabsolutevalueofthesumofanyothernon-emptyrange.


Input

Theinputfilecontainsseveraltestcases.Eachtestcasestartswithtwonumbersnandk.Inputisterminatedbyn=k=0.Otherwise,1<=n<=100000andtherefollownintegerswithabsolutevalues<=10000whichconstitutethesequence.Thenfollowkqueriesforthissequence.Eachqueryisatargettwith0<=t<=1000000000.

Output

Foreachqueryoutput3numbersonaline:someclosestabsolutesumandthelowerandupperindicesofsomerangewherethisabsolutesumisachieved.Possibleindicesstartwith1andgoupton.

SampleInput

51 -10-50510 3 102 -98-76-54-32-10 511 152 -1-1-1-1-1-1-1-1-1-1-1-1-1-1-1 15100 00

SampleOutput



544
528
911
15115
15115


Source

UlmLocal2001

尺取法,注意inf初始化

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<set>
#include<map>
usingnamespacestd;
#defineN106006
#defineinf1<<30
pair<int,int>g
;
intmain()
{
intn,k;
while(scanf("%d%d",&n,&k)==2)
{
if(n==0&&k==0)
break;
intsum=0;
g[0]=make_pair(0,0);
for(inti=1;i<=n;i++){
intx;
scanf("%d",&x);
sum=sum+x;
g[i]=make_pair(sum,i);
}
sort(g,g+n+1);
while(k--){

intval;
scanf("%d",&val);

intminn=inf;
intans,ansl=1,ansr=1;
ints=0,t=1;
for(;;){
if(t>n)
break;
if(minn==0)
break;
intnum=g[t].first-g[s].first;
if(abs(num-val)<minn){
minn=abs(num-val);
ans=num;
ansl=g[s].second;
ansr=g[t].second;
}

if(num<val)
t++;
if(num>val)
s++;
if(s==t)
t++;
}
if(ansl>ansr)
swap(ansl,ansr);
printf("%d%d%d\n",ans,ansl+1,ansr);
}

}
return0;
}


ViewCode

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: