时间限制:C/C++ 2秒,其他语言4秒
空间限制:C/C++ 262144K,其他语言524288K
64bit IO Format: %lld
空间限制:C/C++ 262144K,其他语言524288K
64bit IO Format: %lld
题目描述
You are given a list of integers
x1,x2,…,xnx_1, x_2, \ldots, x_nx1,x2,…,xn and a number k.
It is guaranteed that each i from 1 to k appears in the list at least once.
Find the lexicographically smallest subsequence of x that contains
each integer from 1 to k exactly once.
输入描述:
The first line will contain two integers n and k, with
1≤k≤n≤200 0001\le k\le n\le 200\,0001≤k≤n≤200000.
The following n lines will each contain an integer xix_ixi with
1≤xi≤k1\le x_i\le k1≤xi≤k.
输出描述:
Write out on one line, separated by spaces, the lexicographically smallest
subsequence of x that has each integer from 1 to k exactly once.
示例1
输入
6 3
3
2
1
3
1
3
6 3 3 2 1 3 1 3
输出
2 1 3
2 1 3
示例2
输入
10 5
5
4
3
2
1
4
1
1
5
5
10 5 5 4 3 2 1 4 1 1 5 5
思路:维护一个递增的单调栈,特殊考虑下末位的数情况,如果在栈中不抛出,如果不在栈中直接加入。
#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]<<' ';
}