Time Limit: 12000MS | Memory Limit: 65536K | |
Total Submissions: 35230 | Accepted: 10400 | |
Case Time Limit: 5000MS |
Description
Window position | Minimum value | Maximum value |
---|---|---|
[1 3 -1] -3 5 3 6 7 | -1 | 3 |
1 [3 -1 -3] 5 3 6 7 | -3 | 3 |
1 3 [-1 -3 5] 3 6 7 | -3 | 5 |
1 3 -1 [-3 5 3] 6 7 | -3 | 5 |
1 3 -1 -3 [5 3 6] 7 | 3 | 6 |
1 3 -1 -3 5 [3 6 7] | 3 | 7 |
Your task is to determine the maximum and minimum values in the sliding window at each position.
Input
Output
Sample Input
8 3 1 3 -1 -3 5 3 6 7
Sample Output
-1 -3 -3 -3 3 3 3 3 5 5 6 7
Source
#include<stdio.h> using namespace std; const int maxn=1000005; struct arr{int l,r,max,min;}a[maxn*2]; int num[maxn],k,n,i; void build(int k,int l,int r) { a[k].l=l;a[k].r=r; if (l==r) { a[k].max=l;a[k].min=r;return; } int mid=(l+r)/2; build(k*2,l,mid); build(k*2+1,mid+1,r); int o1=a[k*2].max;int o2=a[k*2+1].max; if (o1>0||o2>0) { if (o1==0) a[k].max=o2;else if (o2==0) a[k].max=o1;else a[k].max=num[o1]>num[o2]?o1:o2; } o1=a[k*2].min;o2=a[k*2+1].min; if (o1>0||o2>0) { if (o1==0) a[k].min=o2;else if (o2==0) a[k].min=o1;else a[k].min=num[o1]<num[o2]?o1:o2; } } int find_max(int k,int l,int r) { if (a[k].l>=l&&a[k].r<=r) return a[k].max; int mid=(a[k].l+a[k].r)/2; int o1=-1;int o2=-1; if (l<=mid) o1=find_max(k*2,l,r); if (r>mid) o2=find_max(k*2+1,l,r); if (o1==-1) return o2; if (o2==-1) return o1; return num[o1]>num[o2]?o1:o2; } int find_min(int k,int l,int r) { if (a[k].l>=l&&a[k].r<=r) return a[k].min; int mid=(a[k].l+a[k].r)/2; int o1=-1;int o2=-1; if (l<=mid) o1=find_min(k*2,l,r); if (r>mid) o2=find_min(k*2+1,l,r); if (o1==-1) return o2; if (o2==-1) return o1; return num[o1]<num[o2]?o1:o2; } int main() { scanf("%ld%ld",&n,&k); for (i=1;i<=n;i++) scanf("%ld",&num[i]); build(1,1,n); for (i=1;i<=n-k+1;i++) printf("%ld ",num[find_max(1,i,i+k-1)]); printf("\n"); for (i=1;i<=n-k+1;i++) printf("%ld ",num[find_min(1,i,i+k-1)]); return 0; }
#include <iostream>
#include <cstdio>
#include <cmath>
#include<algorithm>
#define MN 1000005
using namespace std;
int mi[MN][21],mx[MN][21],w[MN];
int n,q;
void rmqinit()
{
int i,j,m;
for(i=1;i<=n;i++){mi[i][0]=mx[i][0]=w[i];}
m=int(trunc(log2(n)));
for(i=1;i<=m;i++)
{
for(j=n;j>=1;j--)
{
mx[j][i]=mx[j][i-1];
if(j+(1<<(i-1))<=n)
mx[j][i]=max(mx[j][i],mx[j+(1<<(i-1))][i-1]);
mi[j][i]=mi[j][i-1];
if(j+(1<<(i-1)<=n))
mi[j][i]=min(mi[j][i],mi[j+(1<<(i-1))][i-1]);
}
}
}
int rmqmin(int l,int r)
{
int m=int(trunc(log2(r-l+1)));
return min(mi[l][m],mi[r-(1<<m)+1][m]);
}
int rmqmax(int l,int r)
{
int m=int(trunc(log2(r-l+1)));
return max(mx[l][m],mx[r-(1<<m)+1][m]);
}
int main()
{
cin>>n>>q;
for(int i=1;i<=n;i++)
scanf("%d",&w[i]);
rmqinit();
for(int i=1;i<=n-q+1;i++)
printf("%d ",rmqmin(i,i+q-1));
printf("\n");
for(int i=1;i<=n-q+1;i++)
printf("%d ",rmqmax(i,i+q-1));
return 0;
}
#include<stdio.h> using namespace std; const int maxn=1000005; int x[maxn],y[maxn],a[maxn],n,k,i,tail,now; int main() { scanf("%ld%ld",&n,&k); for (i=1;i<=n;i++) scanf("%ld",&a[i]); x[1]=a[1];y[1]=1;tail=1;now=1; for (i=2;i<k;i++) { while (a[i]<x[tail]&&tail>0) tail--; x[++tail]=a[i];y[tail]=i; } for (i=k;i<=n;i++) { while (a[i]<x[tail]&&tail>0) tail--; x[++tail]=a[i];y[tail]=i;if (now>tail) now=tail; while (y[now]<i-k+1) now++; if (i<n) printf("%ld ",x[now]);else printf("%ld\n",x[now]); } x[1]=a[1];y[1]=1;tail=1;now=1; for (i=2;i<k;i++) { while (a[i]>x[tail]&&tail>0) tail--; x[++tail]=a[i];y[tail]=i; } for (i=k;i<=n;i++) { while (a[i]>x[tail]&&tail>0) tail--; x[++tail]=a[i];y[tail]=i;if (now>tail) now=tail; while (y[now]<i-k+1) now++; if (i<n) printf("%ld ",x[now]);else printf("%ld\n",x[now]); } return 0; }
#include<cstdio> using namespace std; const int maxn=1000005; int x[maxn],y[maxn],a[maxn],n,k,i,tail,head; int main() { scanf("%ld%ld",&n,&k); for (i=1;i<=n;i++) scanf("%ld",&a[i]); x[1]=a[1];y[1]=1;head=1;tail=1; for (i=2;i<k;i++) { while (a[i]<x[tail]&&tail>=head) tail--; x[++tail]=a[i];y[tail]=i; } for (i=k;i<=n;i++) { while (a[i]<x[tail]&&tail>=head) tail--; x[++tail]=a[i];y[tail]=i; while (y[head]<i-k+1) head++; if (i<n) printf("%ld ",x[head]);else printf("%ld\n",x[head]); } x[1]=a[1];y[1]=1;head=1;tail=1; for (i=2;i<k;i++) { while (a[i]>x[tail]&&tail>=head) tail--; x[++tail]=a[i];y[tail]=i; } for (i=k;i<=n;i++) { while (a[i]>x[tail]&&tail>=head) tail--; x[++tail]=a[i];y[tail]=i; while (y[head]<i-k+1) head++; if (i<n) printf("%ld ",x[head]);else printf("%ld\n",x[head]); } return 0; }