Submission #412838

# Submission time Handle Problem Language Result Execution time Memory
412838 2021-05-27T16:19:08 Z cadmiumsky Xor Sort (eJOI20_xorsort) C++14
100 / 100
10 ms 1024 KB
#include <iostream>
#include <algorithm>
#include <vector>

using namespace std;

int v[1001];
int temp[1001];
int ind[1001];


vector< pair<int,int> > solution;

static inline void pushsol(int x, int y) {
  solution.push_back({x+1,y+1});
  v[x]^=v[y];
}
static void printsol() {
  //for(int i=0; i<5; i++) {
    //cout << v[i] <<' ';
  //}
  //cout << "\n\n";
  cout << solution.size() <<'\n';
  for(int i=0; i<solution.size(); i++) {
    cout << solution[i].first <<' ' << solution[i].second <<'\n';
  }
  exit(0);
}

static void solve2(int n, int bit) {
  if(bit<0 || n<=0)
    return;
  int poz=-1;
  for(int i=0; i<n; i++) {
    temp[i]=v[i];
  }
  for(int i=0; i<n; i++) {
    if(v[i]&(1<<bit)) {
      poz=i;
      break;
    }
  }
  if(poz==-1) {
    solve2(n,bit-1);
    return;
  }
  for(int i=poz+1; i<n; i++) {
    if(!(temp[i]&(1<<bit)))
      pushsol(i,i-1);
  }
  for(int i=poz; i<n-1; i++) {
    pushsol(i,i+1);
  }
  solve2(n-1,bit-1);
  return;
}
static void solve1(int n) {
  if(n<=0)
    return;
  int poz=ind[n-1];
  for(int i=poz+1; i<n; i++)
    pushsol(i,i-1);
  for(int i=poz-2; i>=0; i--) 
    pushsol(i,i+1);
  for(int i=0; i<n-1; i++)
    pushsol(i,i+1);
  for(int i=0; i<n-1; i++) {
    if(ind[i]>ind[n-1])
      ind[i]--;
  }
  solve1(n-1);
}

static bool cmp(int x, int y) {
  return v[x]<v[y];
}

int main() {
  int s,n;
  cin >> n >> s;
  for(int i=0; i<n; i++)
    cin >> v[i],ind[i]=i;
  if(s==2) {
    solve2(n,20);
    printsol();
  }
  else {
    sort(ind,ind+n,cmp);
    for(int i=0; i<n-1; i++)
      pushsol(i,i+1);
    solve1(n);
    printsol();
  }
} 
/*
 * 12 23 34 45 5
 * 
 * 
 */ 

Compilation message

xorsort.cpp: In function 'void printsol()':
xorsort.cpp:24:17: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   24 |   for(int i=0; i<solution.size(); i++) {
      |                ~^~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 1 ms 204 KB Output is correct
3 Correct 1 ms 332 KB Output is correct
4 Correct 6 ms 712 KB Output is correct
5 Correct 6 ms 712 KB Output is correct
6 Correct 6 ms 744 KB Output is correct
7 Correct 6 ms 712 KB Output is correct
8 Correct 6 ms 712 KB Output is correct
9 Correct 6 ms 712 KB Output is correct
10 Correct 6 ms 712 KB Output is correct
11 Correct 6 ms 712 KB Output is correct
12 Correct 6 ms 712 KB Output is correct
13 Correct 6 ms 712 KB Output is correct
14 Correct 6 ms 720 KB Output is correct
15 Correct 6 ms 708 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 1 ms 204 KB Output is correct
3 Correct 1 ms 332 KB Output is correct
4 Correct 6 ms 712 KB Output is correct
5 Correct 6 ms 712 KB Output is correct
6 Correct 6 ms 744 KB Output is correct
7 Correct 6 ms 712 KB Output is correct
8 Correct 6 ms 712 KB Output is correct
9 Correct 6 ms 712 KB Output is correct
10 Correct 6 ms 712 KB Output is correct
11 Correct 6 ms 712 KB Output is correct
12 Correct 6 ms 712 KB Output is correct
13 Correct 6 ms 712 KB Output is correct
14 Correct 6 ms 720 KB Output is correct
15 Correct 6 ms 708 KB Output is correct
16 Correct 1 ms 204 KB Output is correct
17 Correct 6 ms 712 KB Output is correct
18 Correct 10 ms 940 KB Output is correct
19 Correct 10 ms 964 KB Output is correct
20 Correct 10 ms 964 KB Output is correct
21 Correct 10 ms 996 KB Output is correct
22 Correct 10 ms 940 KB Output is correct
23 Correct 10 ms 964 KB Output is correct
24 Correct 10 ms 964 KB Output is correct
25 Correct 10 ms 952 KB Output is correct
26 Correct 10 ms 964 KB Output is correct
27 Correct 10 ms 964 KB Output is correct
28 Correct 10 ms 936 KB Output is correct
29 Correct 10 ms 888 KB Output is correct
30 Correct 10 ms 964 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 1 ms 204 KB Output is correct
3 Correct 1 ms 332 KB Output is correct
4 Correct 2 ms 460 KB Output is correct
5 Correct 9 ms 840 KB Output is correct
6 Correct 8 ms 840 KB Output is correct
7 Correct 8 ms 840 KB Output is correct
8 Correct 9 ms 840 KB Output is correct
9 Correct 8 ms 840 KB Output is correct
10 Correct 8 ms 968 KB Output is correct
11 Correct 8 ms 800 KB Output is correct
12 Correct 8 ms 840 KB Output is correct
13 Correct 8 ms 936 KB Output is correct
14 Correct 8 ms 840 KB Output is correct
15 Correct 10 ms 1024 KB Output is correct