https://www.acwing.com/problem/content/description/422/
$O(m*n^2)$复杂度
本来打算慢慢优化,没想到过了,数据有 些微 $弱$
#include<bits/stdc++.h>
using namespace std;
const int N=100000,M=110;
int n,m;
int a[N];
bool b[N];
int main(){
cin>>n;
cin>>m;
for(int i=1;i<=n;i++)scanf("%d",&a[i]);
while(m--){
memset(b,false ,sizeof b);
for(int i=n-1;i>=1;i--){
b[a[i+1]]=true;
if(a[i+1]>a[i]){
for(int v=a[i]+1;v<=n;v++){
if(b[v]==true){
b[a[i]]=true;
a[i]=v;
b[v]=false;
break;
}
}
for(int v=1;v<=n;v++){
if(b[v]==true)a[++i]=v;
}
// for(int i=1;i<=n;i++)printf("%d ",a[i]);
// puts("");
break;
}
}
}
for(int i=1;i<=n;i++)printf("%d ",a[i]);
return 0;
}