답안 #775207

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
775207 2023-07-06T08:43:37 Z daoquanglinh2007 Xor Sort (eJOI20_xorsort) C++17
65 / 100
7 ms 1092 KB
#include <bits/stdc++.h>
using namespace std;

#define pii pair <int, int>
#define fi first
#define se second
#define mp make_pair
#define getbit(x, i) ((x&(1<<i)) != 0)

const int NM = 1000;

int N, S, a[NM+5];
vector <pii> ans;

void op(int i, int j){
	ans.push_back(mp(i, j));
	a[i] ^= a[j];
}

void solve1(){
	for (int i = N; i >= 1; i--){
		int k = 1;
		for (int j = 2; j <= i; j++)
			if (a[j] > a[k]) k = j;
		int cur = a[i];
		bool ok = 0;
		for (int j = i+1; j <= N; j++){
			cur ^= a[j];
			if (cur < a[i+1] && cur >= a[k]){
				ok = 1;
				for (int ii = j-1; ii >= i; ii--)
					op(ii, ii+1);
				for (int ii = i+1; ii < j; ii++)
					op(ii, ii+1);
				break;
			}
		}
		if (ok) continue;
		cur = a[i];
		for (int j = i-1; j >= 1; j--){
			cur ^= a[j];
			if (cur < a[i+1] && cur >= a[k]){
				ok = 1;
				for (int ii = j+1; ii <= i; ii++)
					op(ii, ii-1);
				for (int ii = i-1; ii > j; ii--)
					op(ii, ii-1);
				break;
			}
		}
		if (ok) continue;
		for (int j = 1; j < i; j++)
			if (a[j] > a[j+1]){
				op(j+1, j);
				op(j, j+1);
				op(j+1, j);
			}
	}
}

void solve2(){
	int tar = N+1;
	for (int i = 19; i >= 0; i--){
		bool ok = 0;
		for (int j = 1; j <= N; j++)
			if (getbit(a[j], i)){
				ok = 1;
				break;
			}
		if (ok == 0) continue;
		tar--;
		for (int j = 1; j < tar; j++)
			if (getbit(a[j], i) && !getbit(a[j+1], i)){
				op(j+1, j);
				op(j, j+1);
			}
			else if (getbit(a[j], i) && getbit(a[j+1], i)){
				op(j, j+1);
			}
	}
}

int main(){
	ios_base::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
	cin >> N >> S;
	for (int i = 1; i <= N; i++) cin >> a[i];
	if (S == 1) solve1();
	else solve2();
	cout << ans.size() << '\n';
	for (int i = 0; i < ans.size(); i++)
		cout << ans[i].fi << ' ' << ans[i].se << '\n';
	return 0;
}

Compilation message

xorsort.cpp: In function 'int main()':
xorsort.cpp:92: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]
   92 |  for (int 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 2 ms 632 KB Output is correct
5 Correct 2 ms 596 KB Output is correct
6 Correct 3 ms 600 KB Output is correct
7 Correct 2 ms 596 KB Output is correct
8 Correct 2 ms 600 KB Output is correct
9 Correct 3 ms 600 KB Output is correct
10 Correct 2 ms 596 KB Output is correct
11 Correct 1 ms 340 KB Output is correct
12 Correct 4 ms 728 KB Output is correct
13 Correct 4 ms 728 KB Output is correct
14 Correct 5 ms 864 KB Output is correct
15 Correct 4 ms 728 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 2 ms 632 KB Output is correct
5 Correct 2 ms 596 KB Output is correct
6 Correct 3 ms 600 KB Output is correct
7 Correct 2 ms 596 KB Output is correct
8 Correct 2 ms 600 KB Output is correct
9 Correct 3 ms 600 KB Output is correct
10 Correct 2 ms 596 KB Output is correct
11 Correct 1 ms 340 KB Output is correct
12 Correct 4 ms 728 KB Output is correct
13 Correct 4 ms 728 KB Output is correct
14 Correct 5 ms 864 KB Output is correct
15 Correct 4 ms 728 KB Output is correct
16 Correct 0 ms 212 KB Output is correct
17 Correct 2 ms 596 KB Output is correct
18 Correct 4 ms 856 KB Output is correct
19 Correct 4 ms 856 KB Output is correct
20 Correct 4 ms 728 KB Output is correct
21 Correct 4 ms 856 KB Output is correct
22 Correct 4 ms 856 KB Output is correct
23 Correct 4 ms 856 KB Output is correct
24 Correct 4 ms 808 KB Output is correct
25 Correct 4 ms 808 KB Output is correct
26 Incorrect 7 ms 1092 KB Integer 50020 violates the range [0, 40000]
27 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 0 ms 340 KB Output is correct
4 Correct 1 ms 468 KB Output is correct
5 Correct 4 ms 856 KB Output is correct
6 Correct 4 ms 856 KB Output is correct
7 Correct 4 ms 856 KB Output is correct
8 Correct 5 ms 856 KB Output is correct
9 Correct 6 ms 856 KB Output is correct
10 Correct 6 ms 900 KB Output is correct
11 Correct 4 ms 900 KB Output is correct
12 Correct 4 ms 856 KB Output is correct
13 Correct 5 ms 856 KB Output is correct
14 Correct 4 ms 856 KB Output is correct
15 Correct 5 ms 984 KB Output is correct