题目链接:https://ac.nowcoder.com/acm/contest/12606/E
贪心选择小数放在前面,且保证每个数均出现一次
#include <iostream>
#include <cstdio>
#include <cmath>
#include <vector>
#include <stack>
using namespace std;
#define ll long long
const int maxn = 200005;
ll k,n;
ll a[maxn] = {0};
bool v[maxn] = {0};
ll last[maxn] = {0};
ll ans[maxn] = {0};
stack<ll> sta;
int main() {
cin >> n >> k;
for(int i = 1; i <= n; ++i) {
cin >> a[i];
}
for(int i = n; i >= 1; --i) {
if(v[a[i]] == 0) {
v[a[i]] = 1;
last[a[i]] = i;
}
}
// for(int i = 1; i <= k; ++i) {
// cout << last[i] << " ";
// }
// cout << endl;
for(int i = 1; i <= k; ++i) {
v[i] = 0;
}
for(int i = 1; i <= n; ++i) {
if(v[a[i]] == 1) continue;
while(!sta.empty() && a[i] <= sta.top() && i <= last[sta.top()]) {
v[sta.top()] = 0;
sta.pop();
}
sta.push(a[i]);
v[a[i]] = 1;
}
for(int i = k; i >= 1; --i) {
ans[i] = sta.top();
sta.pop();
}
for(int i = 1; i <= k; ++i) {
cout << ans[i];
if(i == k) cout << endl;
else cout << ' ';
}
return 0;
}