题意:
给你一个长度为n的数组a和一个空的数组b,你每次可以选择最前面k个,求出其中的MEX,塞到b的最后面。
要使b的字典序最大,问答案是什么。
题解:
字典序有关的很多是贪心,那么这道题既然要字典序最大,那我们肯定前面取的时候要贪心地大呀,那就枚举每一个数是什么,然后看看现在a中是否能找得到,如果找不到了就是它。
同时要注意维护a中还剩下的数。
#include<bits/stdc++.h>
using namespace std;
const int N=2e5+5;
int a[N];
set<int>s[N];
vector<int>ans;
int main()
{
int t;
scanf("%d",&t);
while(t--){
ans.clear();
int n;
scanf("%d",&n);
for(int i=1;i<=n;i++)
scanf("%d",&a[i]),s[a[i]].insert(i);
int l=1;
while(l<=n){
int mx=-1,i;
for(i=0;i<=n;i++){
if(s[i].empty())break;
mx=max(mx,*s[i].begin());
}
if(mx==-1){
s[a[l]].erase(s[a[l]].begin());
ans.push_back(0);
l++;
continue;
}
ans.push_back(i);
while(l<=mx)s[a[l]].erase(s[a[l]].begin()),l++;
}
printf("%d\n",ans.size());
for(int i:ans)
printf("%d ",i);
printf("\n");
}
return 0;
}