#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(0), cin.tie(0);
int T; cin >> T;
while(T--) {
int N; cin >> N;
int p[N]; for(int &i : p) cin >> i, --i;
int ans = N;
for(int i = N; i--; ) {
bool vis[N] {}, ok = 1;
vis[*p] = 1;
for(int j = 1; j < N; vis[p[j++]] = 1) {
int k = j - (j <= i);
if(!vis[k] && p[j] != k) ok = 0;
}
if(ok && ans >= 0)
ans = ans == N ? i : -1;
}
cout << (ans % N) + 1 << '\n';
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
320 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
3 ms |
212 KB |
Output is correct |
9 |
Correct |
4 ms |
212 KB |
Output is correct |
10 |
Correct |
5 ms |
332 KB |
Output is correct |
11 |
Correct |
6 ms |
332 KB |
Output is correct |
12 |
Correct |
7 ms |
336 KB |
Output is correct |
13 |
Correct |
8 ms |
336 KB |
Output is correct |
14 |
Correct |
10 ms |
340 KB |
Output is correct |
15 |
Correct |
12 ms |
340 KB |
Output is correct |
16 |
Correct |
13 ms |
344 KB |
Output is correct |
17 |
Correct |
16 ms |
340 KB |
Output is correct |
18 |
Correct |
25 ms |
340 KB |
Output is correct |
19 |
Correct |
22 ms |
352 KB |
Output is correct |
20 |
Correct |
31 ms |
340 KB |
Output is correct |