Submission #813513

#TimeUsernameProblemLanguageResultExecution timeMemory
813513Sputnik123Xor Sort (eJOI20_xorsort)C++14
100 / 100
7 ms984 KiB
#include <bits/stdc++.h> using namespace std; #define pii pair <int, int> #define fi first #define se second #define mp make_pair #define getbit(x, i) ((x&(1<<i)) != 0) const int NM = 1000; int N, S, a[NM+5], a0[NM+5], id[NM+5]; vector <pii> ans; void op(int i, int j){ ans.push_back(mp(i, j)); a[i] ^= a[j]; } bool cmp(int x, int y){ return a[x] < a[y]; } void solve1(){ for (int i = 1; i <= N; i++) a0[i] = a[i]; for (int i = 1; i < N; i++) op(i, i+1); for (int i = N; i >= 1; i--){ int k = 1; for (int j = 2; j <= i; j++) if (a0[j] > a0[k]) k = j; for (int j = k+1; j <= i; j++) op(j, j-1); for (int j = k-1; j < i; j++) if (j > 0) op(j, j+1); for (int j = k; j < i; j++) swap(a0[j], a0[j+1]); } } void solve2(){ int tar = N+1; for (int i = 19; i >= 0; i--){ bool ok = 0; for (int j = 1; j <= N; j++) if (getbit(a[j], i)){ ok = 1; break; } if (ok == 0) continue; tar--; for (int j = 1; j < tar; j++) if (getbit(a[j], i) && !getbit(a[j+1], i)){ op(j+1, j); op(j, j+1); } else if (getbit(a[j], i) && getbit(a[j+1], i)){ op(j, j+1); } } } int main(){ ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin >> N >> S; for (int i = 1; i <= N; i++) cin >> a[i]; if (S == 1) solve1(); else solve2(); cout << ans.size() << '\n'; for (int i = 0; i < ans.size(); i++) cout << ans[i].fi << ' ' << ans[i].se << '\n'; return 0; }

Compilation message (stderr)

xorsort.cpp: In function 'int main()':
xorsort.cpp:73:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   73 |  for (int i = 0; i < ans.size(); i++)
      |                  ~~^~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...