#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int main(){
ios::sync_with_stdio(false);
cin.tie(0);
int n,s;cin>>n>>s;
vector<int> arr(n);
for (int i = 0; i < n; ++i)
{
cin>>arr[i];
}
vector<pair<int,int>> ansarr;
if (s==2){
for (int bit = 22; bit >= 0; bit--){
for (int i = 0; i+1 < n; i++){
if (arr[i]&(1LL<<bit)){
if (arr[i+1]&(1LL<<bit)){
ansarr.push_back({i+1,i+2});
arr[i]^=arr[i+1];
}
else {
ansarr.push_back({i+2,i+1});
ansarr.push_back({i+1,i+2});
arr[i+1]^=arr[i];
arr[i]^=arr[i+1];
}
}
}
if ((n>=1) && (arr[n-1]&(1LL<<bit))){
n--;
}
}
}
else {
for (int i = n; i >= 1; i--){
int maele = 0;
for (int j = 0; j < i; j++){
if (arr[j]>arr[maele]) maele=j;
if (j+1<i){
ansarr.push_back({j+1,j+2});
}
}
for (int j = maele-2; j >= 0; j--){
ansarr.push_back({j+1,j+2});
}
for (int j = maele; j+1 < i; j++){
ansarr.push_back({j+2,j+1});
}
for (int k = maele; k + 1 < i; k++){
swap(arr[k],arr[k+1]);
}
}
for (int i = n; i >= 2; i--){
ansarr.push_back({i-1,i});
}
}
cout<<ansarr.size()<<endl;
for (int i = 0; i < ansarr.size(); ++i)
{
cout<<ansarr[i].first<<" "<<ansarr[i].second<<endl;
}
}
Compilation message
xorsort.cpp: In function 'int main()':
xorsort.cpp:59: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]
59 | for (int i = 0; i < ansarr.size(); ++i)
| ~~^~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
3 ms |
348 KB |
Output is correct |
4 |
Correct |
23 ms |
840 KB |
Output is correct |
5 |
Correct |
32 ms |
736 KB |
Output is correct |
6 |
Correct |
23 ms |
736 KB |
Output is correct |
7 |
Correct |
23 ms |
736 KB |
Output is correct |
8 |
Correct |
27 ms |
876 KB |
Output is correct |
9 |
Correct |
23 ms |
872 KB |
Output is correct |
10 |
Correct |
24 ms |
732 KB |
Output is correct |
11 |
Correct |
23 ms |
860 KB |
Output is correct |
12 |
Correct |
24 ms |
732 KB |
Output is correct |
13 |
Correct |
23 ms |
736 KB |
Output is correct |
14 |
Correct |
23 ms |
732 KB |
Output is correct |
15 |
Correct |
23 ms |
872 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
3 ms |
348 KB |
Output is correct |
4 |
Correct |
23 ms |
840 KB |
Output is correct |
5 |
Correct |
32 ms |
736 KB |
Output is correct |
6 |
Correct |
23 ms |
736 KB |
Output is correct |
7 |
Correct |
23 ms |
736 KB |
Output is correct |
8 |
Correct |
27 ms |
876 KB |
Output is correct |
9 |
Correct |
23 ms |
872 KB |
Output is correct |
10 |
Correct |
24 ms |
732 KB |
Output is correct |
11 |
Correct |
23 ms |
860 KB |
Output is correct |
12 |
Correct |
24 ms |
732 KB |
Output is correct |
13 |
Correct |
23 ms |
736 KB |
Output is correct |
14 |
Correct |
23 ms |
732 KB |
Output is correct |
15 |
Correct |
23 ms |
872 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
23 ms |
732 KB |
Output is correct |
18 |
Correct |
42 ms |
1140 KB |
Output is correct |
19 |
Correct |
41 ms |
1000 KB |
Output is correct |
20 |
Correct |
41 ms |
1056 KB |
Output is correct |
21 |
Correct |
41 ms |
988 KB |
Output is correct |
22 |
Correct |
41 ms |
992 KB |
Output is correct |
23 |
Correct |
47 ms |
992 KB |
Output is correct |
24 |
Correct |
42 ms |
1132 KB |
Output is correct |
25 |
Correct |
42 ms |
988 KB |
Output is correct |
26 |
Correct |
41 ms |
992 KB |
Output is correct |
27 |
Correct |
41 ms |
988 KB |
Output is correct |
28 |
Correct |
41 ms |
992 KB |
Output is correct |
29 |
Correct |
49 ms |
992 KB |
Output is correct |
30 |
Correct |
57 ms |
988 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
2 ms |
480 KB |
Output is correct |
4 |
Correct |
6 ms |
580 KB |
Output is correct |
5 |
Correct |
31 ms |
988 KB |
Output is correct |
6 |
Correct |
39 ms |
988 KB |
Output is correct |
7 |
Correct |
30 ms |
980 KB |
Output is correct |
8 |
Correct |
31 ms |
984 KB |
Output is correct |
9 |
Correct |
31 ms |
988 KB |
Output is correct |
10 |
Correct |
30 ms |
936 KB |
Output is correct |
11 |
Correct |
32 ms |
984 KB |
Output is correct |
12 |
Correct |
33 ms |
988 KB |
Output is correct |
13 |
Correct |
33 ms |
1056 KB |
Output is correct |
14 |
Correct |
37 ms |
988 KB |
Output is correct |
15 |
Correct |
44 ms |
988 KB |
Output is correct |