题目: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; }
原文:https://www.cnblogs.com/simon-chou/p/13620126.html