#include <bits/stdc++.h>
#define FOR(i, a, b) for (int i = (a); i <= (b); ++i)
#define FORD(i, a, b) for (int i = (a); i >= (b); --i)
#define REP(i, a) for (int i = 0; i < (a); ++i)
#define DEBUG(x) { cerr << #x << '=' << x << endl; }
#define Arr(a, l, r) { cerr << #a << " = {"; FOR(_, l, r) cerr << ' ' << a[_]; cerr << "}\n"; }
#define N 1001000
#define pp pair<int, int>
#define endl '\n'
#define IO ios::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL)
#define taskname ""
#define bit(S, i) (((S) >> (i)) & 1)
using namespace std;
int n, p[N], pos[N], mark[N];
bool Ok(int crazy) {
FOR(i, 1, n) mark[i] = 0;
mark[p[1]] = 1;
int now = 1;
FOR(i, 1, n) if (i != crazy) {
now++;
if (mark[i] == 0) {
if (p[now] != i) return false;
} else {
if (mark[p[now]]) return false;
}
mark[p[now]] = 1;
}
return true;
}
void Solve() {
cin >> n;
FOR(i, 1, n) cin >> p[i];
int cnt = 0, z;
FOR(i, 1, n) if (Ok(i)) {
cnt++; z = i;
}
if (cnt == 1) cout << z << '\n';
else cout << 0 << '\n';
}
int main() {
#ifdef NERO
freopen("test.inp","r",stdin);
freopen("test.out","w",stdout);
double stime = clock();
#else
//freopen(taskname".inp","r",stdin);
//freopen(taskname".out","w",stdout);
#endif //NERO
IO;
int test;
cin >> test;
while (test--) Solve();
#ifdef NERO
double etime = clock();
cerr << "Execution time: " << (etime - stime) / CLOCKS_PER_SEC * 1000 << " ms.\n";
#endif // NERO
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
380 KB |
Output is correct |
2 |
Correct |
2 ms |
452 KB |
Output is correct |
3 |
Correct |
2 ms |
452 KB |
Output is correct |
4 |
Correct |
2 ms |
492 KB |
Output is correct |
5 |
Correct |
2 ms |
492 KB |
Output is correct |
6 |
Correct |
2 ms |
500 KB |
Output is correct |
7 |
Correct |
2 ms |
604 KB |
Output is correct |
8 |
Correct |
2 ms |
604 KB |
Output is correct |
9 |
Correct |
3 ms |
604 KB |
Output is correct |
10 |
Correct |
3 ms |
604 KB |
Output is correct |
11 |
Correct |
4 ms |
604 KB |
Output is correct |
12 |
Correct |
4 ms |
604 KB |
Output is correct |
13 |
Correct |
4 ms |
604 KB |
Output is correct |
14 |
Correct |
6 ms |
648 KB |
Output is correct |
15 |
Correct |
6 ms |
648 KB |
Output is correct |
16 |
Correct |
7 ms |
716 KB |
Output is correct |
17 |
Correct |
7 ms |
748 KB |
Output is correct |
18 |
Correct |
10 ms |
748 KB |
Output is correct |
19 |
Correct |
10 ms |
748 KB |
Output is correct |
20 |
Correct |
12 ms |
748 KB |
Output is correct |