标签:des style blog http color os io strong ar
Time Limit: 12000MS | Memory Limit: 65536K | |
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
1 #include<set> 2 #include<queue> 3 #include<cstdio> 4 #include<cstdlib> 5 #include<cstring> 6 #include<iostream> 7 #include<algorithm> 8 using namespace std; 9 const int N = 1000007; 10 #define For(i,n) for(int i=1;i<=n;i++) 11 #define Rep(i,l,r) for(int i=l;i<=r;i++) 12 13 int n,Loc[N],k,Q[N],A[N]; 14 15 void Max(){ 16 int l = 1 ,r = 0; 17 For(i,k-1){ 18 while(l<=r&&Q[r]<=A[i]) r--; 19 Q[++r]=A[i];Loc[r]=i; 20 } 21 Rep(i,k,n){ 22 while(l<=r&&Q[r]<=A[i]) r--; 23 Q[++r]=A[i];Loc[r]=i; 24 while(Loc[l]<=i-k) l++; 25 if(k==n) printf("%d\n",Q[l]); 26 else printf("%d ",Q[l]); 27 } 28 } 29 30 void Min(){ 31 int l = 1 ,r = 0; 32 For(i,k-1){ 33 while(l<=r&&Q[r]>=A[i]) r--; 34 Q[++r]=A[i];Loc[r]=i; 35 } 36 Rep(i,k,n){ 37 while(l<=r&&Q[r]>=A[i]) r--; 38 Q[++r]=A[i];Loc[r]=i; 39 while(Loc[l]<=i-k) l++; 40 if(k==n) printf("%d\n",Q[l]); 41 else printf("%d ",Q[l]); 42 } 43 puts(""); 44 } 45 46 int main(){ 47 scanf("%d%d",&n,&k); 48 For(i,n) scanf("%d",&A[i]); 49 Min();Max(); 50 return 0; 51 }
标签:des style blog http color os io strong ar
原文地址:http://www.cnblogs.com/kjerome/p/3948054.html