CSES - Putka Open 2020 – 3/5 - Results
Submission details
Task:Vaihdot
Sender:Lieska
Submission time:2020-10-16 18:21:31 +0300
Language:C++ (C++17)
Status:READY
Result:0
Feedback
groupverdictscore
#10
#20
Test results
testverdicttimegroup
#10.01 s1, 2details
#2ACCEPTED0.03 s2details

Compiler report

input/code.cpp: In function 'void testi()':
input/code.cpp:64:33: warning: unused variable 'b' [-Wunused-variable]
                     int a=t[i], b=t[i+1], c=t[n];
                                 ^
input/code.cpp:80:25: warning: unused variable 'a' [-Wunused-variable]
                     int a=t[1], b=t[i], c=t[i+1];
                         ^

Code

#include <bits/stdc++.h>
using namespace std;

typedef long long ll;

void testi(){
    int n;
    cin >> n;
    int t[n+2]={}, p[n+2]={}, k=0, ans[5*n+2][3];
    for (int i=1; i<=n; ++i){
        int a;
        cin >> a;
        t[i]=a;
        p[a]=i;
    }
    if (n==1) {
        cout << "0\n";
        return;
    }
    if (n==2){
        if (t[1]==1){
            cout << "0\n";
            return;
        }
        else{
            cout << "-1\n";
            return;
        }
    }
    if (n==3){
        if (t[2]!=2){
            cout << "-1\n";
            return;
        }
        else{
            if (t[1]==1){
                cout << "0\n";
                return;
            }
            else{
                cout << "1\n1 3\n";
                return;
            }
        }
    }
    for (int i=1; i<n; ++i){
        if (t[i]!=i){
            if (p[i]!=i+1){
                k++;
                ans[k][1]=i;
                ans[k][2]=p[i];
                t[p[i]]=t[i];
                p[t[i]]=p[i];
                t[i]=i;
            }
            else{
                if ((n-(i+1))>=2){
                    k++;
                    ans[k][1]=i+1;
                    ans[k][2]=n;
                    k++;
                    ans[k][1]=i;
                    ans[k][2]=n;
                    int a=t[i], b=t[i+1], c=t[n];
                    t[i+1]=c;
                    t[n]=a;
                    p[a]=n;
                    p[c]=i+1;
                }
                else if (i>=3){
                    k++;
                    ans[k][1]=i+1;
                    ans[k][2]=1;
                    k++;
                    ans[k][1]=i;
                    ans[k][2]=1;
                    k++;
                    ans[k][1]=1;
                    ans[k][2]=i+1;
                    int a=t[1], b=t[i], c=t[i+1];
                    t[i]=c;
                    t[i+1]=b;
                    p[b]=i+1;
                }
                else if (n==4){
                    k++;
                    ans[k][1]=1;
                    ans[k][2]=3;
                    k++;
                    ans[k][1]=2;
                    ans[k][2]=4;
                    k++;
                    ans[k][1]=1;
                    ans[k][2]=4;
                    k++;
                    ans[k][1]=1;
                    ans[k][2]=3;
                    k++;
                    ans[k][1]=2;
                    ans[k][2]=4;
                    t[2]=2;
                    t[3]=3;
                    p[3]=3;
                }
            }
        }
    }
    cout << k << "\n";
    for (int i=1; i<=k; ++i){
        cout << ans[i][1] << " " << ans[i][2] << "\n";
    }
}

int main(){
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    //Älä käytä, jos tehtävä on interaktiivinen!
    int t;
    cin >> t;
    while (t--) testi();
}





Test details

Test 1

Group: 1, 2

Verdict:

input
1000
1
1
2
1 2
...

correct output
0
0
-1
0
-1
...

user output
0
0
-1
0
-1
...
Truncated

Test 2

Group: 2

Verdict: ACCEPTED

input
1000
79
49 42 77 41 37 61 46 55 7 72 4...

correct output
81
67 79
70 78
3 77
60 76
...

user output
79
1 58
2 45
3 16
4 29
...
Truncated