题目链接
题解
我们将已知字符排序,由循环就可以得到一个对应关系
如样例就是:
0->第5行
1->第1行
1->第2行
1->第3行
1->第5行
2>第6行
3->第4行
按照这个循序加入答案即可
#include<algorithm>
#include<iostream>
#include<cstring>
#include<cstdio>
#include<cmath>
#include<map>
#define Redge(u) for (int k = h[u],to; k; k = ed[k].nxt)
#define REP(i,n) for (int i = 1; i <= (n); i++)
#define mp(a,b) make_pair<int,int>(a,b)
#define cls(s) memset(s,0,sizeof(s))
#define cp pair<int,int>
#define LL long long int
using namespace std;
const int maxn = 200005,maxm = 100005,INF = 1000000000;
inline int read(){
int out = 0,flag = 1; char c = getchar();
while (c < 48 || c > 57){if (c == '-') flag = -1; c = getchar();}
while (c >= 48 && c <= 57){out = (out << 3) + (out << 1) + c - 48; c = getchar();}
return out * flag;
}
int n,m,A[maxn],B[maxn],ans[maxn];
cp lk[maxn];
int main(){
n = read(); m = read();
REP(i,n + 1) A[i] = B[i] = read();
sort(B + 1,B + 1 + n + 1);
REP(i,n + 1) lk[i] = mp(A[i],i);
sort(lk + 1,lk + 1 + n + 1);
for (int now = 1,i = 1; i <= n; i++,now = lk[now].second){
ans[i] = B[lk[now].second];
}
REP(i,n) printf("%d ",ans[i]);
return 0;
}