This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long unsigned long ull;
typedef double long ld;
int main() {
ios::sync_with_stdio(!cin.tie(0));
int n, s;
cin >> n >> s;
vector<int> a(n);
for (int i = 0; i < n; i++) {
cin >> a[i];
}
auto b = a;
sort(b.begin(), b.end());
vector<pair<int, int>> ans;
for (int i = 0; i < n; i++) {
int pos;
for (int j = 0; j < n; j++) {
if (a[j] == b[i]) {
pos = j;
}
}
while (pos > i) {
ans.push_back({pos, pos - 1}); // a = a ^ b
ans.push_back({pos - 1, pos}); // b = b ^ (a ^ b) = a
ans.push_back({pos, pos - 1}); // a = a ^ b ^ a = b
swap(a[pos], a[pos - 1]);
pos--;
}
}
cout << ans.size() << '\n';
for (auto& p : ans) {
cout << p.first + 1 << " " << p.second + 1 << '\n';
}
}
/*a, b
a = a ^ b
b = b ^ (a ^ b) = a
a = a ^ b ^ a
3 op to swap*/
Compilation message (stderr)
xorsort.cpp: In function 'int main()':
xorsort.cpp:25:9: warning: 'pos' may be used uninitialized in this function [-Wmaybe-uninitialized]
25 | int pos;
| ^~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |