题意:给出一段数字序列,找出序列中字典序最小的子序列。
思路:维护一个递增的单调栈,特殊考虑下末位的数情况,如果在栈中不抛出,如果不在栈中直接加入。
#include<iostream>
using namespace std;
int l=1,r=0;
const int maxn=2e5+5;
int q[maxn];
int st[maxn];
int tt[maxn];
int main()
{
int n,k;
cin>>n>>k;
for(int i=1;i<=n;i++)
{
cin>>st[i];
tt[st[i]]++;
}
for(int i=1;i<=n;i++)
{
tt[st[i]]--;
if(l>r||st[i]>q[l])
{
while(l<=r&&st[i]<q[r]&&tt[q[r]])r--;
q[++r]=st[i];
}
else
{
if(!tt[st[i]]){
q[++r]=st[i];
}
}
}
for(int i=l;i<=r;i++)
cout<<q[i]<<' ';
}