题目:https://pintia.cn/problem-sets/1268384564738605056/problems/1291317697694580738
题解:https://www.liuchuo.net/archives/2301
代码:

#include <iostream>
using namespace std;
int main() {
    int n, t, cnt = 0, a[100010];
    cin >> n;
    for(int i = 0; i < n; i++){
        cin >> t;
        a[t] = i;
    }
    for(int i = 1; i < n; i++) {
        if(i != a[i]) {
            while(a[0] != 0) {
                swap(a[0],a[a[0]]);
                cnt++;
            }
            if(i != a[i]) {
                swap(a[0],a[i]);
                cnt++;
            }
        }
    }
    cout << cnt;
    return 0;
}

 

 posted on 2020-09-05 22:38  邢涌芝  阅读(139)  评论(0编辑  收藏  举报