Time Limit: 12000MS | Memory Limit: 65536K | |
Total Submissions: 26178 | Accepted: 7738 | |
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 <iostream> #include <stdio.h> #include <string.h> #include <algorithm> #include <queue> #include <cmath> #define N 1000004 #define lson l,m,k<<1 #define rson m+1,r,k<<1|1 using namespace std; struct node { int min,max; }; node st[N<<2]; void build(int l,int r,int k) { if(l==r) { scanf("%d",&st[k].min); st[k].max=st[k].min; return ; } int m=(l+r)>>1; build(lson); build(rson); st[k].max=max(st[k<<1].max,st[k<<1|1].max); st[k].min=min(st[k<<1].min,st[k<<1|1].min); } int quMin(int &L,int &R,int l,int r,int k) { if(L<=l&&R>=r) { return st[k].min; } int m=(l+r)>>1; int t1=1000000000,t2=1000000000; if(L<=m) t1=quMin(L,R,lson); if(R>m) t2=quMin(L,R,rson); return t1<t2?t1:t2; } int quMax(int &L,int &R,int l,int r,int k) { if(L<=l&&R>=r) { return st[k].max; } int m=(l+r)>>1; int t1=-1000000000,t2=-1000000000; if(L<=m) t1=quMax(L,R,lson); if(R>m) t2=quMax(L,R,rson); return t1>t2?t1:t2; } int main() { int i,k; int n,m,r; while(scanf("%d%d",&n,&k)!=EOF) { build(1,n,1); m=n-k; for(i=1;i<=m;i++) r=i+k-1,printf("%d ",quMin(i,r,1,n,1)); r=m+1; printf("%d\n",quMin(r,n,1,n,1)); for(i=1;i<=m;i++) r=i+k-1,printf("%d ",quMax(i,r,1,n,1)); r=m+1; printf("%d\n",quMax(r,n,1,n,1)); } return 0; }
#include <iostream> #include <stdio.h> #include <string.h> #include <algorithm> #include <queue> #include <cmath> #define N 1000004 using namespace std; struct node { int id,val; }; node M[N]; int front,back; int a[N]; int main() { int n,k,i; while(scanf("%d%d",&n,&k)!=EOF) { front=back=1; scanf("%d",&a[1]); M[front].val=a[1];M[front].id=1; for(i=2;i<=k;i++) { scanf("%d",&a[i]); if(a[i]<=M[back].val) { while(1) { M[back].val=a[i],M[back].id=i; if(back>front&&a[i]<=M[back-1].val) back--; else break; } } else { M[++back].val=a[i]; M[back].id=i; } } printf("%d",M[front].val); for(i=k+1;i<=n;i++) { scanf("%d",&a[i]); if(a[i]<=M[back].val) { while(1) { M[back].val=a[i],M[back].id=i; if(back>front&&a[i]<=M[back-1].val) back--; else break; } } else { M[++back].val=a[i]; M[back].id=i; } while(M[front].id<i-k+1) front++; printf(" %d",M[front].val); } printf("\n"); front=back=1; M[front].val=a[1];M[front].id=1; for(i=2;i<=k;i++) { if(a[i]>=M[back].val) { while(1) { M[back].val=a[i],M[back].id=i; if(back>front&&a[i]>=M[back-1].val) back--; else break; } } else { M[++back].val=a[i]; M[back].id=i; } } printf("%d",M[front].val); for(i=k+1;i<=n;i++) { if(a[i]>=M[back].val) { while(1) { M[back].val=a[i],M[back].id=i; if(back>front&&a[i]>=M[back-1].val) back--; else break; } } else { M[++back].val=a[i]; M[back].id=i; } while(M[front].id<i-k+1) front++; printf(" %d",M[front].val); } printf("\n"); } return 0; }