ICPC 2019 Pacific Northwest B(单调栈)
90nwyn
2020-10-21 22:35:44
[题目链接](https://vjudge.net/problem/Gym-102433B)
------------
------------
```cpp
#include <bits/stdc++.h>
using namespace std;
const int M=2e5+5;
int n,k,a[M],st[M],top,cnt[M],in[M];
int main()
{
scanf("%d%d",&n,&k);
for(int i=1;i<=n;i++)
{
scanf("%d",&a[i]);
cnt[a[i]]++;
}
for(int i=1;i<=n;i++)
{
cnt[a[i]]--;
if(in[a[i]])continue;
while(top&&a[i]<st[top]&&cnt[st[top]])in[st[top--]]=0;
in[st[++top]=a[i]]=1;
}
for(int i=1;i<=k;i++)printf("%d ",st[i]);
return 0;
}
```