#include "bits/stdc++.h"
using namespace std;
#define ar array
vector<ar<int, 2>> get(vector<int>& a, int b = 20){
if(a.empty()) return {};
vector<int> tmp;
for(auto x : a) tmp.push_back(x>>b&1);
vector<ar<int, 2>> rr;
int i=0;
while(i < (int)tmp.size() && tmp[i]) i++;
if(i && i < (int)tmp.size()){
i--;
for(;i>0;i--){
tmp[i] ^= 1;
rr.push_back({i, i-1});
rr.push_back({i, i+1});
a[i] ^= a[i+1];
a[i] ^= a[i-1];
}
rr.push_back({i, i+1});
rr.push_back({i+1, i});
rr.push_back({i, i+1});
swap(tmp[i], tmp[i+1]);
swap(a[i], a[i+1]);
}
for(int i=1;i+1<(int)tmp.size();i++){
if(tmp[i] && tmp[i-1] == 0 && tmp[i+1]){
tmp[i] ^= 1;
rr.push_back({i, i+1});
rr.push_back({i, i-1});
a[i] ^= a[i+1];
a[i] ^= a[i-1];
} else if(tmp[i] && tmp[i+1] == 0){
swap(tmp[i], tmp[i+1]);
rr.push_back({i, i+1});
rr.push_back({i+1, i});
rr.push_back({i, i+1});
swap(a[i], a[i+1]);
}
}
if(b){
if(tmp.back()) a.pop_back();
vector<ar<int, 2>> tt = get(a, b - 1);
rr.insert(rr.end(), tt.begin(), tt.end());
}
return rr;
}
void solve(){
int n, s; cin>>n>>s;
vector<int> a(n);
for(auto& x : a) cin>>x;
vector<ar<int, 2>> res = get(a);
cout<<(int)res.size()<<"\n";
for(auto x : res) cout<<x[0] + 1<<" "<<x[1] + 1<<"\n";
cout<<"\n";
}
signed main(){
ios_base::sync_with_stdio(0); cin.tie(0);
int t = 1;
//~ cin>>t;
while(t--) solve();
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Incorrect |
1 ms |
252 KB |
Not sorted |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Incorrect |
1 ms |
252 KB |
Not sorted |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
3 ms |
588 KB |
Output is correct |
5 |
Incorrect |
11 ms |
1696 KB |
Integer 49444 violates the range [0, 40000] |
6 |
Halted |
0 ms |
0 KB |
- |