#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <set>
#include <string>
#include <cmath>
#include <map>
#include <unordered_map>
#include <fstream>
#include <iomanip>
#include <iterator>
#include <stack>
using namespace std;
using ll = long long;
vector <int> v;
vector <pair<int, int>> ans;
void bubbleSort()
{
int i, j;
bool swapped;
for (i = 0; i < v.size() - 1; i++)
{
swapped = false;
for (j = 0; j < v.size() - i - 1; j++)
{
if (v[j] > v[j + 1])
{
swap(v[j], v[j + 1]);
swapped = true;
ans.push_back({ j, j + 1 });
ans.push_back({ j + 1, j });
ans.push_back({ j, j + 1 });
}
}
if (swapped == false)
break;
}
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(0);
int x, n, i, j, s;
cin >> n >> s;
if (s == 1)
{
for (i = 0; i < n; i++)
{
cin >> x;
v.push_back(x);
}
bubbleSort();
cout << ans.size() << endl;
for (i = 0; i < ans.size(); i++)
{
cout << ans[i].first + 1 << ' ' << ans[i].second + 1 << '\n';
}
}
else
{
for ( i = 0; i < n; i++)
{
cin >> x;
v.push_back(x);
}
int m = n;
for ( i = 19; i >= 0; i--)
{
int st = 0;
for ( j = 0; j < m - 1; j++)
{
if (v[j] & (1 << i) && v[j + 1] & (1 << i))
{
st = 1;
ans.push_back({ j, j + 1 });
v[j] ^= v[j];
}
else if(v[j] & (1 << i))
{
st = 1;
ans.push_back({ j + 1, j });
ans.push_back({ j, j + 1 });
v[j + 1] ^= v[j];
v[j] ^= v[j + 1];
}
}
if (st)
m--;
}
cout << ans.size() << endl;
for (i = 0; i < ans.size(); i++)
{
cout << ans[i].first + 1 << ' ' << ans[i].second + 1 << '\n';
}
}
return 0;
}
Compilation message
xorsort.cpp: In function 'void bubbleSort()':
xorsort.cpp:22:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
22 | for (i = 0; i < v.size() - 1; i++)
| ~~^~~~~~~~~~~~~~
xorsort.cpp:25:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
25 | for (j = 0; j < v.size() - i - 1; j++)
| ~~^~~~~~~~~~~~~~~~~~
xorsort.cpp: In function 'int main()':
xorsort.cpp:55:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
55 | for (i = 0; i < ans.size(); i++)
| ~~^~~~~~~~~~~~
xorsort.cpp:92:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
92 | for (i = 0; i < ans.size(); i++)
| ~~^~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
3 ms |
596 KB |
Output is correct |
5 |
Correct |
3 ms |
600 KB |
Output is correct |
6 |
Correct |
3 ms |
600 KB |
Output is correct |
7 |
Correct |
3 ms |
664 KB |
Output is correct |
8 |
Correct |
4 ms |
600 KB |
Output is correct |
9 |
Correct |
4 ms |
600 KB |
Output is correct |
10 |
Correct |
5 ms |
600 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
9 ms |
900 KB |
Output is correct |
13 |
Correct |
7 ms |
856 KB |
Output is correct |
14 |
Correct |
8 ms |
856 KB |
Output is correct |
15 |
Correct |
9 ms |
924 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
3 ms |
596 KB |
Output is correct |
5 |
Correct |
3 ms |
600 KB |
Output is correct |
6 |
Correct |
3 ms |
600 KB |
Output is correct |
7 |
Correct |
3 ms |
664 KB |
Output is correct |
8 |
Correct |
4 ms |
600 KB |
Output is correct |
9 |
Correct |
4 ms |
600 KB |
Output is correct |
10 |
Correct |
5 ms |
600 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
9 ms |
900 KB |
Output is correct |
13 |
Correct |
7 ms |
856 KB |
Output is correct |
14 |
Correct |
8 ms |
856 KB |
Output is correct |
15 |
Correct |
9 ms |
924 KB |
Output is correct |
16 |
Correct |
1 ms |
212 KB |
Output is correct |
17 |
Correct |
4 ms |
688 KB |
Output is correct |
18 |
Correct |
8 ms |
856 KB |
Output is correct |
19 |
Correct |
6 ms |
856 KB |
Output is correct |
20 |
Correct |
5 ms |
856 KB |
Output is correct |
21 |
Correct |
6 ms |
856 KB |
Output is correct |
22 |
Correct |
6 ms |
800 KB |
Output is correct |
23 |
Correct |
6 ms |
856 KB |
Output is correct |
24 |
Correct |
7 ms |
872 KB |
Output is correct |
25 |
Correct |
8 ms |
788 KB |
Output is correct |
26 |
Incorrect |
10 ms |
1236 KB |
Integer 59568 violates the range [0, 40000] |
27 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
212 KB |
Not sorted |
2 |
Halted |
0 ms |
0 KB |
- |