CSES - Putka Open 2020 – 3/5 - Results
Submission details
Task:Vaihdot
Sender:kluopaja
Submission time:2020-10-18 19:32:22 +0300
Language:C++ (C++11)
Status:READY
Result:100
Feedback
groupverdictscore
#1ACCEPTED35
#2ACCEPTED65
Test results
testverdicttimegroup
#1ACCEPTED0.02 s1, 2details
#2ACCEPTED0.11 s2details

Compiler report

input/code.cpp: In function 'bool testAns(std::vector<std::pair<int, int> >, std::vector<int>)':
input/code.cpp:40:20: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
   for(int i = 0; i < v.size(); ++i) {
                  ~~^~~~~~~~~~
input/code.cpp: In function 'void solve(std::vector<int>)':
input/code.cpp:128:20: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
   for(int i = 0; i < v.size();) {
                  ~~^~~~~~~~~~

Code

#include <iostream>
#include <vector>
#include <utility>
#include <cmath>
using namespace std;
// assume n >= 4
// if i == 1
// a b   c
// a c   b
// b c   a
// b a   c

// if i == 2
// d a b c, a c
// d c b a, a d
// a c b d, a b
// b c a d, b d
// d c a b, b c
// d b a c

// if i == 3
// c   a b, c b
// b   a c, a b
// a   b c, a c
// c   b a 
// 1 2 3 4 5 6 7 8

// 2 4 3 1
// 2 1 3 4
// 4 1 3 2
// 4 2 3 1
// 1 2 3 4

bool testAns(vector<pair<int, int> > ans, vector<int> v) {
  for(auto x: ans) {
    if(abs(x.first-x.second) <= 1) return 0;
    swap(v[x.first], v[x.second]);
  }
  int ok = 1;
  for(int i = 0; i < v.size(); ++i) {
    if(v[i] != i) ok = 0;
    cout<<v[i]<<' ';
  }
  cout<<'\n';
  if(ok) {
    cout<<"OK"<<endl;
  }
  else {
    cout<<"FAIL"<<endl;
  }
  return ok;
}
void printAns(vector<pair<int, int> > v) {
  cout<<v.size()<<endl;
  for(auto x: v) {
    cout<<x.first+1<<' '<<x.second+1<<endl;
  }
}
void swapTwo(vector<int>& v, int a, int b, vector<pair<int, int> >& ans) {
  if(a > b) swap(a, b);
  int c;
  swap(v[a], v[b]);
  if(a+1 == b) {
    int d;
    if(b == 1) {
      c = 3;
      ans.push_back({b, c});
      swap(b, c);
      ans.push_back({a, b});
      swap(a, b);
      ans.push_back({a, c});
    }
    else if(b == 2) {
      d = 0;
      c = 3;
      ans.push_back({a, c});
      swap(a, c);
      ans.push_back({a, d});
      swap(a, d);
      ans.push_back({a, b});
      swap(a, b);
      ans.push_back({b, d});
      swap(b, d);
      ans.push_back({b, c});
    }
    else {
      c = 0;
      ans.push_back({c, b});
      swap(c, b);
      ans.push_back({a, b});
      swap(a, b);
      ans.push_back({a, c});
    }
  }
  else {
    ans.push_back({a, b});
  }
}
void solve(vector<int> v) {
  vector<int> v_copy = v;
  if(v.size() == 1) {
    cout<<0<<endl;
    return;
  }
  if(v.size() == 2) {
    if(v[0] == 0) {
      cout<<0<<endl;
      return;
    }
    cout<<-1<<endl;
    return;
  }
  vector<pair<int, int> > ans;
  if(v.size() == 3) {
    if(v[0] > v[2]) {
      swap(v[0], v[2]);
      ans.push_back({0, 2});
    }
    if(v[0] == 0 && v[1] == 1 && v[2] == 2) {
      printAns(ans);
      return;
    }
    else {
      cout<<-1<<endl;
      return;
    }
  }
  for(int i = 0; i < v.size();) {
    if(v[i] != i) {
      swapTwo(v, i, v[i], ans);
    }
    else {
      ++i;
    }
  }
  printAns(ans);
  //std::cerr << testAns(ans, v_copy) << std::endl;
}
int main() {
  int tt;
  cin>>tt;
  for(int xx = 0; xx < tt; ++xx) {
    int n;
    cin>>n;
    vector<int> v(n);
    for(int i = 0; i < n; ++i) {
      cin>>v[i];
      --v[i];
    }
    solve(v);
  }
}

Test details

Test 1

Group: 1, 2

Verdict: ACCEPTED

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 49
1 10
1 72
1 38
...
Truncated