#include <bits/stdc++.h>
#include <unordered_map>
#define ll long long
#define ld long double
#define pll pair <ll,ll>
#define F first
#define S second
#define pb push_back
#define IOS ios_base::sync_with_stdio(0),cin.tie(0),cout.tie(0)
using namespace std;
int n, type;
vector <ll> a;
vector <pll> ans;
void solve1() {
int mn = 2e9;
vector <pll> ans2;
for (int i = 0; i < n - 1; i++)
a[i] ^= a[i + 1];
for (int j = 0; j < n; j++) {
vector<ll> aa;
aa = a;
for (int i = 0; i < n; i++)
if (i != j) a[i] ^= a[j];
ans.clear();
vector<ll> ab;
ab = aa;
for (int i = 0; i < n - 1; i++)
ans.pb({ i + 1,i + 2 });
for (int i = j + 1; i < n; i++)
{
ans.pb({ i + 1,i });
ab[i] ^= ab[i - 1];
}
for (int i = j - 1; i >= 0; i--)
{
ans.pb({ i + 1,i + 2 });
ab[i] ^= ab[i + 1];
}
a = ab;
unordered_map<int, int> mp;
bool YY = true;
for (int i = 0; i < n; i++)
{
if (mp[a[i]]) { YY = false; break; }
++mp[a[i]];
}
if (YY == false) {
a = aa;
continue;
}
while (1) {
bool found = true;
int mx = -1;
int pos = -1;
for (int i = 0; i < n - 1; i++)
if (a[i] > a[i + 1]) {
pos = i;
break;
}
if (pos < 0) break;
ans.pb({ pos + 2,pos + 1 });
ans.pb({ pos + 1, pos + 2 });
ans.pb({ pos + 2,pos + 1 });
swap(a[pos], a[pos + 1]);
}
if (ans.size() < mn) {
mn = ans.size();
ans2 = ans;
}
a = aa;
}
ans = ans2;
cout << ans.size() << '\n';
for (auto to : ans)
cout << to.F << " " << to.S << '\n';
}
void solve2() {
int m = n;
for (int i = 19; i >= 0; i--)
{
int st = 0;
for (int j = 0; j < m - 1; j++)
{
if (a[j] & (1 << i) && a[j + 1] & (1 << i))
{
st = 1;
ans.push_back({ j, j + 1 });
a[j] ^= a[j + 1];
}
else if (a[j] & (1 << i))
{
st = 1;
ans.push_back({ j + 1, j });
ans.push_back({ j, j + 1 });
a[j + 1] ^= a[j];
a[j] ^= a[j + 1];
}
}
if (st)
m--;
}
cout << ans.size() << '\n';
for (auto to : ans)
cout << to.F << " " << to.S << '\n';
}
int main() {
cin >> n >> type;
a.resize(n);
for (int i = 0; i < n; i++)
cin >> a[i];
if (type == 1) solve1();
else solve2();
}
Compilation message
xorsort.cpp: In function 'void solve1()':
xorsort.cpp:53:9: warning: unused variable 'found' [-Wunused-variable]
53 | bool found = true;
| ^~~~~
xorsort.cpp:54:8: warning: unused variable 'mx' [-Wunused-variable]
54 | int mx = -1;
| ^~
xorsort.cpp:67:18: warning: comparison of integer expressions of different signedness: 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
67 | if (ans.size() < mn) {
| ~~~~~~~~~~~^~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
3 ms |
340 KB |
Output is correct |
4 |
Correct |
75 ms |
1104 KB |
Output is correct |
5 |
Correct |
73 ms |
1008 KB |
Output is correct |
6 |
Correct |
70 ms |
1000 KB |
Output is correct |
7 |
Correct |
66 ms |
1004 KB |
Output is correct |
8 |
Correct |
63 ms |
1024 KB |
Output is correct |
9 |
Correct |
66 ms |
1036 KB |
Output is correct |
10 |
Correct |
63 ms |
992 KB |
Output is correct |
11 |
Correct |
58 ms |
896 KB |
Output is correct |
12 |
Correct |
61 ms |
1416 KB |
Output is correct |
13 |
Correct |
57 ms |
1348 KB |
Output is correct |
14 |
Correct |
62 ms |
1364 KB |
Output is correct |
15 |
Correct |
59 ms |
1416 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
3 ms |
340 KB |
Output is correct |
4 |
Correct |
75 ms |
1104 KB |
Output is correct |
5 |
Correct |
73 ms |
1008 KB |
Output is correct |
6 |
Correct |
70 ms |
1000 KB |
Output is correct |
7 |
Correct |
66 ms |
1004 KB |
Output is correct |
8 |
Correct |
63 ms |
1024 KB |
Output is correct |
9 |
Correct |
66 ms |
1036 KB |
Output is correct |
10 |
Correct |
63 ms |
992 KB |
Output is correct |
11 |
Correct |
58 ms |
896 KB |
Output is correct |
12 |
Correct |
61 ms |
1416 KB |
Output is correct |
13 |
Correct |
57 ms |
1348 KB |
Output is correct |
14 |
Correct |
62 ms |
1364 KB |
Output is correct |
15 |
Correct |
59 ms |
1416 KB |
Output is correct |
16 |
Correct |
1 ms |
300 KB |
Output is correct |
17 |
Correct |
69 ms |
1104 KB |
Output is correct |
18 |
Correct |
180 ms |
1524 KB |
Output is correct |
19 |
Correct |
174 ms |
1916 KB |
Output is correct |
20 |
Correct |
188 ms |
1868 KB |
Output is correct |
21 |
Correct |
187 ms |
1532 KB |
Output is correct |
22 |
Correct |
173 ms |
1560 KB |
Output is correct |
23 |
Correct |
178 ms |
1848 KB |
Output is correct |
24 |
Correct |
184 ms |
1924 KB |
Output is correct |
25 |
Correct |
193 ms |
1572 KB |
Output is correct |
26 |
Correct |
184 ms |
1408 KB |
Output is correct |
27 |
Correct |
180 ms |
1348 KB |
Output is correct |
28 |
Correct |
170 ms |
1404 KB |
Output is correct |
29 |
Correct |
193 ms |
1380 KB |
Output is correct |
30 |
Correct |
164 ms |
1456 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
212 KB |
Integer 0 violates the range [1, 5] |
2 |
Halted |
0 ms |
0 KB |
- |