标签:ase min mini include eterm oid utc stdio.h lap
Time Limit: 12000MS | Memory Limit: 65536K | |
Total Submissions: 63635 | Accepted: 18150 | |
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<stdio.h> 2 #include<string.h> 3 #define maxn 1000010 4 struct node{ 5 int pos,val; 6 }; 7 node maxque[maxn]; 8 node minque[maxn]; 9 int read(); 10 void getmax(); 11 void getmin(); 12 int n,m,a[maxn],ma[maxn],mi[maxn],op; 13 int main(){ 14 n=read();m=read(); 15 for(int i=1;i<=n;i++) a[i]=read(); 16 getmax(); 17 getmin(); 18 for(int i=1;i<=op;i++) printf("%d ",mi[i]); 19 putchar(‘\n‘); 20 for(int i=1;i<=op;i++) printf("%d ",ma[i]); 21 return 0; 22 } 23 void getmax(){ 24 int i,head=1,tail=0; 25 op=0; 26 for(i=1;i<m;i++){ 27 while(head<=tail&&maxque[tail].val<a[i]) tail--; 28 maxque[++tail].pos=i;maxque[tail].val=a[i]; 29 } 30 for(;i<=n;i++){ 31 while(head<=tail&&maxque[tail].val<a[i]) tail--; 32 maxque[++tail].pos=i;maxque[tail].val=a[i]; 33 while(maxque[head].pos<i+1-m) head++; 34 ma[++op]=maxque[head].val; 35 } 36 } 37 void getmin(){ 38 int i,head=1,tail=0; 39 op=0; 40 for(i=1;i<m;i++){ 41 while(head<=tail&&minque[tail].val>a[i]) tail--; 42 minque[++tail].pos=i;minque[tail].val=a[i]; 43 } 44 for(;i<=n;i++){ 45 while(head<=tail&&minque[tail].val>a[i]) tail--; 46 minque[++tail].pos=i;minque[tail].val=a[i]; 47 while(minque[head].pos<i+1-m) head++; 48 mi[++op]=minque[head].val; 49 } 50 } 51 int read(){ 52 int ans=0,f=1;char c=getchar(); 53 while(‘0‘>c||c>‘9‘){if(c==‘-‘)f=-1;c=getchar();} 54 while(‘0‘<=c&&c<=‘9‘)ans=ans*10+c-48,c=getchar();return ans*f; 55 }
标签:ase min mini include eterm oid utc stdio.h lap
原文地址:http://www.cnblogs.com/lpl-bys/p/7647622.html