|
- #include<iostream>
- using namespace std;
- const int M=1000010;
- int n,k,b,j=1;
- int mina[M],maxa[M];
- struct sque
- {
- int a[M],head,tail;
- int q[M],p[M];
- void read()
- {
- cin>>n>>k;
- for(int i=1;i<=n;i++)
- cin>>a[i];
- }
- void findmax()
- {
- head=1;
- tail=0;
- for(int i=1;i<=n;i++)
- {
- while(q[tail]<=a[i] && head<=tail)tail--;
- q[++tail]=a[i];
- p[tail]=i;
- while(p[head]<=i-k)
- head++;
- if(i>=k)cout<<q[head]<<" ";
- }
- cout<<endl;
- }
- void findmin()
- {
- head=1;
- tail=0;
- for(int i=1;i<=n;i++)
- {
- while(q[tail]>=a[i] && head<=tail)tail--;
- q[++tail]=a[i];
- p[tail]=i;
- while(p[head]<=i-k)
- head++;
- if(i>=k)cout<<q[head]<<" ";
- }
- cout<<endl;
- }
- }q;
- int main()
- {
- q.read();
- q.findmin();
- q.findmax();
- }
复制代码 |
|