-
Notifications
You must be signed in to change notification settings - Fork 2
/
shuffle.cpp
35 lines (33 loc) · 848 Bytes
/
shuffle.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#include <bits/stdc++.h>
using namespace std;
int main(){
ifstream fin("shuffle.in");
ofstream fout("shuffle.out");
int N;
array<int, 100000> mov;
array<bool, 100000> gVisit;
fin >> N;
for(int i = 0; i < N; i++){
fin >> mov[i];
mov[i]--;
}
int loopN = 0;
for(int i = 0; i < N; i++){
if(gVisit[i]) continue;
unordered_map<int, int> lVisit;
int c = i;
while(true){
gVisit[c] = true;
lVisit.insert(make_pair(c, lVisit.size()));
c = mov[c];
unordered_map<int, int>::iterator it = lVisit.find(c);
if(it != lVisit.end()){
loopN += lVisit.size() - it->second;
break;
}
if(gVisit[c]) break;
}
}
fout << loopN << endl;
return 0;
}