Submission #1319045

#TimeUsernameProblemLanguageResultExecution timeMemory
1319045Ghulam_JunaidCat (info1cup19_cat)C++20
0 / 100
3 ms480 KiB
#include <bits/stdc++.h>
using namespace std;

void solve(){
    int n;
    scanf("%d", &n);
    int a[n + 1], pos[n + 1] = {};
    for (int i = 1; i <= n; i ++)
        scanf("%d", &a[i]), pos[a[i]] = i;

    vector<int> bad;
    vector<pair<int, int>> vec;
    for (int i = 1; i <= n / 2; i ++){
        if (pos[i] == i) continue;
        int p = pos[i];
        if (n - i + 1 == p){
            bad.push_back(i);
            continue;
        }

        vec.push_back({i, p});
        swap(a[p], a[i]);
        swap(pos[a[p]], pos[a[i]]);

        int o1 = n - i + 1;
        int o2 = n - p + 1;
        swap(a[o1], a[o2]);
        swap(pos[a[o1]], pos[a[o2]]);
    }

    while (bad.size() >= 2){
        int i = bad.back(); bad.pop_back();
        int p = bad.back(); bad.pop_back();

        p = n - p + 1;
        vec.push_back({i, p});

        swap(a[p], a[i]);
        swap(pos[a[p]], pos[a[i]]);

        int o1 = n - i + 1;
        int o2 = n - p + 1;
        swap(a[o1], a[o2]);
        swap(pos[a[o1]], pos[a[o2]]);

        p = n - p + 1;
        vec.push_back({i, p});

        swap(a[p], a[i]);
        swap(pos[a[p]], pos[a[i]]);

        o1 = n - i + 1;
        o2 = n - p + 1;
        swap(a[o1], a[o2]);
        swap(pos[a[o1]], pos[a[o2]]);
    }

    bool good = 1;
    for (int i = 1; i <= n; i ++)
        good &= (a[i] == i);

    if (!good){
        cout << -1 << endl;
        return ;
    }

    cout << vec.size() << " " << vec.size() << '\n';
    for (auto [i, j] : vec)
        cout << i << " " << j << '\n';
}

int main(){
    ios::sync_with_stdio(0);
    cin.tie(0); cout.tie(0);

    int t;
    cin >> t;

    while (t--)
        solve();
    return 0;
}

Compilation message (stderr)

cat.cpp: In function 'void solve()':
cat.cpp:6:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
    6 |     scanf("%d", &n);
      |     ~~~~~^~~~~~~~~~
cat.cpp:9:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
    9 |         scanf("%d", &a[i]), pos[a[i]] = i;
      |         ~~~~~^~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...