답안 #464091

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
464091 2021-08-12T11:15:58 Z amunduzbaev Xor Sort (eJOI20_xorsort) C++14
100 / 100
10 ms 1124 KB
#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;
	
	for(int i=0;i+1<(int)tmp.size();i++){
		if(tmp[i] && tmp[i+1]){
			tmp[i] ^= 1;
			rr.push_back({i, 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+1, i});
			rr.push_back({i, i+1});
			a[i+1] ^= a[i];
			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 s, n; cin>>n>>s;
	vector<int> a(n);
	for(auto& x : a) cin>>x;
	if(s == 1){
		vector<array<int, 2>> mx, rr;
		for(int i=0;i<n;i++){
			mx.push_back({a[i], i});
		} sort(mx.rbegin(), mx.rend());
		
		for(int i=0;i<(int)mx.size();i++){
			int p = mx[i][1];
			
			for(int i=0;i+1<n;i++){
				a[i] ^= a[i+1];
				rr.push_back({i, i+1});
			}
			
			if(i) n--;
			
			for(int i=p;i+1<n;i++){
				a[i+1] ^= a[i];
				rr.push_back({i+1, i});
			}
			
			for(int i=p-2;i>=0;i--){
				a[i] ^= a[i+1];
				rr.push_back({i, i+1});
			}
			
			assert(a[n - 1] == mx[i][0]);
			for(int j=i + 1;j<(int)mx.size();j++){
				if(mx[j][1] > p) mx[j][1]--;
			}
			//~ for(int i=0;i<(int)a.size();i++) cout<<a[i]<<" ";
			//~ cout<<"\n";
		}
		
		cout<<(int)rr.size()<<"\n";
		for(auto x : rr) cout<<x[0] + 1<<" "<<x[1] + 1<<"\n";
	} else {
		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";
	}
}

/*

5 1
3 2 8 4 1

*/

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 Correct 0 ms 204 KB Output is correct
3 Correct 1 ms 332 KB Output is correct
4 Correct 5 ms 716 KB Output is correct
5 Correct 5 ms 716 KB Output is correct
6 Correct 7 ms 716 KB Output is correct
7 Correct 6 ms 716 KB Output is correct
8 Correct 5 ms 716 KB Output is correct
9 Correct 5 ms 716 KB Output is correct
10 Correct 5 ms 732 KB Output is correct
11 Correct 6 ms 716 KB Output is correct
12 Correct 5 ms 716 KB Output is correct
13 Correct 5 ms 716 KB Output is correct
14 Correct 5 ms 716 KB Output is correct
15 Correct 5 ms 716 KB Output is correct
# 결과 실행 시간 메모리 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 5 ms 716 KB Output is correct
5 Correct 5 ms 716 KB Output is correct
6 Correct 7 ms 716 KB Output is correct
7 Correct 6 ms 716 KB Output is correct
8 Correct 5 ms 716 KB Output is correct
9 Correct 5 ms 716 KB Output is correct
10 Correct 5 ms 732 KB Output is correct
11 Correct 6 ms 716 KB Output is correct
12 Correct 5 ms 716 KB Output is correct
13 Correct 5 ms 716 KB Output is correct
14 Correct 5 ms 716 KB Output is correct
15 Correct 5 ms 716 KB Output is correct
16 Correct 1 ms 204 KB Output is correct
17 Correct 5 ms 736 KB Output is correct
18 Correct 8 ms 968 KB Output is correct
19 Correct 8 ms 972 KB Output is correct
20 Correct 8 ms 892 KB Output is correct
21 Correct 8 ms 1084 KB Output is correct
22 Correct 8 ms 972 KB Output is correct
23 Correct 8 ms 972 KB Output is correct
24 Correct 8 ms 972 KB Output is correct
25 Correct 8 ms 924 KB Output is correct
26 Correct 10 ms 996 KB Output is correct
27 Correct 8 ms 984 KB Output is correct
28 Correct 8 ms 968 KB Output is correct
29 Correct 8 ms 972 KB Output is correct
30 Correct 8 ms 968 KB Output is correct
# 결과 실행 시간 메모리 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 2 ms 460 KB Output is correct
5 Correct 7 ms 976 KB Output is correct
6 Correct 7 ms 976 KB Output is correct
7 Correct 10 ms 976 KB Output is correct
8 Correct 9 ms 976 KB Output is correct
9 Correct 7 ms 976 KB Output is correct
10 Correct 7 ms 976 KB Output is correct
11 Correct 7 ms 1108 KB Output is correct
12 Correct 7 ms 1048 KB Output is correct
13 Correct 7 ms 1112 KB Output is correct
14 Correct 8 ms 1048 KB Output is correct
15 Correct 9 ms 1124 KB Output is correct