1 #include<cstdio> 2 #define MAXN 1000010 3 int a[MAXN],q[MAXN]; 4 int main() 5 { 6 int n,k,i,j,front,rear; 7 while(~scanf("%d%d",&n,&k)) 8 { 9 for(i=1;i<=n;i++) 10 scanf("%d",&a[i]); 11 front=0; 12 rear=-1; 13 for(i=1;i<=k;i++) 14 { 15 for(;front<=rear&&a[q[rear]]>=a[i];rear--); 16 q[++rear]=i; 17 } 18 printf("%d",a[q[front]]); 19 for(j=1;i<=n;i++,j++) 20 { 21 if(front<=rear&&q[front]<=j) 22 front++; 23 for(;front<=rear&&a[q[rear]]>=a[i];rear--); 24 q[++rear]=i; 25 printf(" %d",a[q[front]]); 26 } 27 putchar('\n'); 28 front=0; 29 rear=-1; 30 for(i=1;i<=k;i++) 31 { 32 for(;front<=rear&&a[q[rear]]<=a[i];rear--); 33 q[++rear]=i; 34 } 35 printf("%d",a[q[front]]); 36 for(j=1;i<=n;i++,j++) 37 { 38 if(front<=rear&&q[front]<=j) 39 front++; 40 for(;front<=rear&&a[q[rear]]<=a[i];rear--); 41 q[++rear]=i; 42 printf(" %d",a[q[front]]); 43 } 44 putchar('\n'); 45 } 46 return 0; 47 }
![](https://img2024.cnblogs.com/blog/35695/202408/35695-20240826070647288-243310440.jpg)