Submission #47561

#TimeUsernameProblemLanguageResultExecution timeMemory
47561nickyrioCrazy old lady (IZhO13_crazy)C++17
100 / 100
12 ms748 KiB
#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
}
#Verdict Execution timeMemoryGrader output
Fetching results...