POJ2823:Sliding Window(单调队列||线段树)
2012-11-23 12:47
369 查看
SlidingWindow
Description
Anarrayofsizen≤106isgiventoyou.Thereisaslidingwindowofsizekwhichismovingfromtheveryleftofthearraytothe
veryright.Youcanonlyseetheknumbersinthewindow.Eachtimetheslidingwindowmovesrightwardsbyoneposition.Followingisanexample:
Thearrayis[13-1-35367],andkis3.
Yourtaskistodeterminethemaximumandminimumvaluesintheslidingwindowateachposition.
Input
Theinputconsistsoftwolines.Thefirstlinecontainstwointegersnandkwhicharethelengthsofthearrayandtheslidingwindow.Therearenintegers
inthesecondline.
Output
Therearetwolinesintheoutput.Thefirstlinegivestheminimumvaluesinthewindowateachposition,fromlefttoright,respectively.Thesecondlinegivesthemaximumvalues.
SampleInput
SampleOutput
Source
POJMonthly--2006.04.28,Ikki
MYCode(线段树版本)
线段树11094MS
单调队列6735MS
TimeLimit:12000MS | MemoryLimit:65536K | |
TotalSubmissions:28126 | Accepted:8389 | |
CaseTimeLimit:5000MS |
Anarrayofsizen≤106isgiventoyou.Thereisaslidingwindowofsizekwhichismovingfromtheveryleftofthearraytothe
veryright.Youcanonlyseetheknumbersinthewindow.Eachtimetheslidingwindowmovesrightwardsbyoneposition.Followingisanexample:
Thearrayis[13-1-35367],andkis3.
Windowposition | Minimumvalue | Maximumvalue |
---|---|---|
[13-1]-35367 | -1 | 3 |
1[3-1-3]5367 | -3 | 3 |
13[-1-35]367 | -3 | 5 |
13-1[-353]67 | -3 | 5 |
13-1-3[536]7 | 3 | 6 |
13-1-35[367] | 3 | 7 |
Input
Theinputconsistsoftwolines.Thefirstlinecontainstwointegersnandkwhicharethelengthsofthearrayandtheslidingwindow.Therearenintegers
inthesecondline.
Output
Therearetwolinesintheoutput.Thefirstlinegivestheminimumvaluesinthewindowateachposition,fromlefttoright,respectively.Thesecondlinegivesthemaximumvalues.
SampleInput
83 13-1-35367
SampleOutput
-1-3-3-333 335567
Source
MYCode(线段树版本)
#include<iostream> #include<cstring> #include<cstdio> usingnamespacestd; #defineMAX1000010 structnode { intlt; intrt; intmini; intmost; }a[4*MAX]; intv[MAX]; intlt[MAX]; intrt[MAX]; intmin(inta,intb){returna<b?a:b;} intmax(inta,intb){returna>b?a:b;} voidbuild(ints,intt,intstep) { a[step].lt=s; a[step].rt=t; if(s==t) { a[step].mini=a[step].most=v[s]; return; } intmid=(a[step].lt+a[step].rt)/2; build(s,mid,2*step); build(mid+1,t,2*step+1); a[step].mini=min(a[2*step].mini,a[2*step+1].mini); a[step].most=max(a[2*step].most,a[2*step+1].most); } nodequery(ints,intt,intstep) { if(s==a[step].lt&&t==a[step].rt) { returna[step]; } if(a[step].lt==a[step].rt)returna[step]; intmid=(a[step].lt+a[step].rt)/2; if(mid>=t) returnquery(s,t,2*step); elseif(mid<s) returnquery(s,t,2*step+1); else { nodetp1=query(s,mid,2*step); nodetp2=query(mid+1,t,2*step+1); nodetp; tp.lt=s; tp.rt=t; tp.mini=min(tp1.mini,tp2.mini); tp.most=max(tp1.most,tp2.most); returntp; } } intmain() { intn,k; while(scanf("%d%d",&n,&k)!=EOF) { inti; for(i=1;i<=n;i++) { scanf("%d",&v[i]); } build(1,n,1); intct=0; for(i=1;i<=n-k+1;i++) { nodeans=query(i,i+k-1,1); lt[ct]=ans.mini; rt[ct]=ans.most; ct++; } for(i=0;i<ct;i++) { printf("%d",lt[i]); if(i!=ct-1) printf(""); } printf("\n"); for(i=0;i<ct;i++) { printf("%d",rt[i]); if(i!=ct-1) printf(""); } printf("\n"); } }
MYCode:(单调队列版本)
#include<iostream>
#include<cstring>
#include<cstdio>
usingnamespacestd;
#defineMAX1000010
intv[MAX];
intlt[MAX];
intrt[MAX];
intst[MAX];
intn;
intk;
voidsolve1()
{
inthead=0,tail=0;
intct=0;
inti;
for(i=1;i<=n;i++)
{
while(head<tail&&v[st[tail-1]]<v[i])tail--;
st[tail++]=i;
while(i-st[head]>=k)head++;
rt[ct++]=st[head];
}
}
voidsolve2()
{
inthead=0,tail=0;
intct=0;
inti;
for(i=1;i<=n;i++)
{
while(head<tail&&v[st[tail-1]]>v[i])tail--;
st[tail++]=i;
while(i-st[head]>=k)head++;
lt[ct++]=st[head];
}
}
intmain()
{
while(scanf("%d%d",&n,&k)!=EOF)
{
inti;
for(i=1;i<=n;i++)
{
scanf("%d",&v[i]);
}
solve1();
solve2();
for(i=k-1;i<n;i++)
{
printf("%d",v[lt[i]]);
if(i!=n-1);
printf("");
}
printf("\n");
for(i=k-1;i<n;i++)
{
printf("%d",v[rt[i]]);
if(i!=n-1);
printf("");
}
printf("\n");
}
}
RunID | User | Problem | Result | Memory | Time | Language | CodeLength | SubmitTime |
11039225 | Accepted | 43848K | 11094MS | 1959B | 2012-11-2215:22:12 | |||
11039159 | Accepted | 11940K | 6735MS | 1208B | 2012-11-2215:00:32 |
单调队列6735MS
相关文章推荐
- POJ2823 Sliding Window单调队列
- ACM-单调队列之Sliding Window——poj2823
- 单调队列与滑动窗口(Sliding window, poj2823)
- Sliding Window POJ - 2823 单调队列
- POJ 2823 Sliding Window + 单调队列
- Sliding Window POJ - 2823 单调队列
- Sliding Window(单调队列讲解与例题)
- [NOIP模拟][POJ 2823][单调队列]滑动的窗户(Sliding Window)
- POJ 2823 Sliding Window + 单调队列
- Sliding Window POJ - 2823 单调队列
- [POJ 2823] Sliding Window · 单调队列
- Sliding Window POJ - 2823 单调队列
- Sliding Window poj 单调队列的简单应用
- Sliding Window POJ - 2823 单调队列
- Sliding Window POJ - 2823 单调队列
- Sliding Window POJ - 2823 单调队列
- Sliding Window 单调队列求区间内的最值
- Sliding Window POJ - 2823 单调队列
- POJ 2823-Sliding Window单调队列解题报告
- sliding window 滑动窗口(单调队列)