标签:
Time Limit: 12000MS | Memory Limit: 65536K | |
Total Submissions: 53037 | Accepted: 15207 | |
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
[Submit] [Go Back] [Status] [Discuss]
————————————————————————我是分割线————————————————————————————————
单调队列模板题。
维护两个单调队列,一个单调增,一个单调减。
1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include<cmath> 5 #include<algorithm> 6 #include<queue> 7 #include<cstdlib> 8 #include<iomanip> 9 #include<cassert> 10 #include<climits> 11 #define maxn 1000001 12 #define F(i,j,k) for(int i=j;i<=k;i++) 13 #define M(a,b) memset(a,b,sizeof(a)) 14 #define FF(i,j,k) for(int i=j;i>=k;i--) 15 #define inf 0x7fffffff 16 using namespace std; 17 int read(){ 18 int x=0,f=1;char ch=getchar(); 19 while(ch<‘0‘||ch>‘9‘){if(ch==‘-‘)f=-1;ch=getchar();} 20 while(ch>=‘0‘&&ch<=‘9‘){x=x*10+ch-‘0‘;ch=getchar();} 21 return x*f; 22 } 23 int a[maxn],q1[maxn],q2[maxn]; 24 int main() 25 { 26 std::ios::sync_with_stdio(false);//cout<<setiosflags(ios::fixed)<<setprecision(1)<<y; 27 #ifdef LOCAL 28 freopen("data.in","r",stdin); 29 freopen("data.out","w",stdout); 30 #endif 31 int n,m,k; 32 cin>>n>>k; 33 F(i,1,n) 34 { 35 cin>>a[i]; 36 } 37 int head=1,tail=0; 38 F(i,1,n) 39 { 40 while(head<=tail&&a[i]<=a[q1[tail]]) tail--; 41 q1[++tail]=i; 42 while(head<=tail&&q1[tail]-q1[head]>=k) head++; 43 if(i>=k) cout<<a[q1[head]]<<" "; 44 } 45 cout<<endl; 46 head=1,tail=0; 47 F(i,1,n) 48 { 49 while(head<=tail&&a[i]>=a[q2[tail]]) tail--; 50 q2[++tail]=i; 51 while(head<=tail&&q2[tail]-q2[head]>=k) head++; 52 if(i>=k) cout<<a[q2[head]]<<" "; 53 } 54 return 0; 55 }
标签:
原文地址:http://www.cnblogs.com/SBSOI/p/5646323.html