洛谷P1327 数列排序

https://www.luogu.org/problem/P1327

#include<bits/stdc++.h>
#define Ll long long
using namespace std;
const int N=1e5+5;
map<int,int>F;
int a[N],b[N];
int n,m,ans;
int main() {
    scanf("%d",&n);
    for(int i=1; i<=n; i++)scanf("%d",&a[i]),b[i]=a[i],F[a[i]]=i; //f记录a[i] 原来的位置
    sort(b+1,b+n+1);
    for(int i=1; i<=n; i++)
        if(a[i]!=b[i]) {
            ans++;   //交换
            int x=F[b[i]];   //取b【i】原来的位置
            F[a[i]]=x;  //把a【i】的位置更新为原来b【i】的位置,相当于交换位置
            a[x]=a[i];  // 把 x位置更新为a【i】
        }
    printf("%d",ans);
}
原文地址:https://www.cnblogs.com/QingyuYYYYY/p/11773205.html