题目:传送门
很明显,用单调队列
code:
1 #include<bits/stdc++.h> 2 using namespace std; 3 int n,k,Fmax[1000010],num[1000010],q[1000010],Fmin[1000010]; 4 int a[1000010]; 5 int head,tail; 6 void dmin() 7 { 8 head=1; 9 tail=0; 10 for(int i=1;i<=n;i++) 11 { 12 while(num[head]<i-k+1&&head<=tail)head++; 13 while(a[i]<q[tail]&&head<=tail)tail--; 14 num[++tail]=i; 15 q[tail]=a[i]; 16 Fmin[i]=q[head]; 17 } 18 } 19 void dmax() 20 { 21 head=1; 22 tail=0; 23 for(int i=1;i<=n;i++) 24 { 25 while(num[head]<i-k+1&&head<=tail)head++; 26 while(a[i]>q[tail]&&head<=tail)tail--; 27 num[++tail]=i; 28 q[tail]=a[i]; 29 Fmax[i]=q[head]; 30 // cout<<"i-k+1= "<<i-k+1<<endl; 31 // cout<<"i= "<<i<<endl; 32 // cout<<"num[head]= "<<num[head]<<endl; 33 // cout<<"head= "<<head<<endl; 34 // cout<<"tail= "<<tail<<endl; 35 // cout<<"num[++tail]= "<<num[++tail]<<endl; 36 // cout<<"q[tail]= "<<q[tail]<<endl; 37 // cout<<"q[head]= "<<q[head]<<endl; 38 } 39 } 40 int main() 41 { 42 cin>>n>>k; 43 for(int i=1;i<=n;i++) 44 cin>>a[i]; 45 dmin(); 46 dmax(); 47 for(int i=k;i<n;i++) 48 cout<<Fmin[i]<<" "; 49 cout<<Fmin[n]<<endl; 50 for(int i=k;i<n;i++) 51 cout<<Fmax[i]<<" "; 52 cout<<Fmax[n]<<endl; 53 return 0; 54 }