#include <bits/stdc++.h>
using namespace std;
struct Elem{
bool isSelec = false;
int data;
};
int ElemCntinEachCircle(Elem* A, int pos);
int main()
{
#ifdef ONLINE_JUDGE
#else
freopen("in.txt", "r", stdin);
#endif
int N;
scanf("%d", &N);
Elem A[N];
int i;
int ElemCnt, SwapCnt = 0;
for(i = 0; i < N; i++){
scanf("%d", &A[i].data);
}
ElemCnt = ElemCntinEachCircle(A, 0);
if(ElemCnt > 1) SwapCnt += ElemCnt - 1;
for(i = 1; i < N-1; i++){
if(A[i].isSelec) continue;
ElemCnt = ElemCntinEachCircle(A, i);
if(ElemCnt > 1) SwapCnt += ElemCnt + 1;
}
printf("%d", SwapCnt);
return 0;
}
int ElemCntinEachCircle(Elem* A, int pos)
{
int ret = 1;
int tmp(pos);
while(A[tmp].data != pos){
tmp = A[tmp].data;
A[tmp].isSelec = true;
ret++;
}
return ret;
}
PTA 7-16 Sort with Swap(0, i) (25 分)
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.