답안 #120692

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
120692 2019-06-25T08:24:56 Z 윤교준(#2960) Minerals (JOI19_minerals) C++14
90 / 100
106 ms 4468 KB
#include "minerals.h"
#include <bits/stdc++.h>
#define eb emplace_back
#define sz(V) ((int)(V).size())
#define allv(V) ((V).begin()),((V).end())
using namespace std;
typedef pair<int, int> pii;
mt19937 mtrd(time(0));

const int MAXN = 100055;

void solve(vector<int>, vector<int>);
void solve(vector<int>, vector<int>, vector<int>);

bitset<MAXN> chk;

int ask(int c) {
	chk[c] = !chk[c];
	return Query(c);
}

vector<pii> AnsV;
int Delta;

void wellsolve(vector<int> A, vector<int> BL, vector<int> BR) {
	int N = sz(A), M = N >> 1, K = N-M;
	if(sz(BL) > sz(BR)) swap(BL, BR);
	if(BL.empty()) {
		solve(A, BR);
		return;
	}
	if(sz(BL) < abs(M - sz(BL))) {
		for(int v : BL) {
			ask(v);
			BR.eb(v);
		}
		solve(A, BR);
		return;
	} else {
		if(!chk[BL[0]]) swap(BL, BR);
		solve(A, BL, BR);
		return;
	}
}

void solve(vector<int> A, vector<int> B) {
	int N = sz(A), M = N >> 1, K = N-M;
	if(N < 1) return;
	if(1 == N) {
		AnsV.eb(A[0], B[0]);
		if(chk[A[0]] || chk[B[0]]) Delta++;
		return;
	}
	shuffle(allv(A), mtrd);
	shuffle(allv(B), mtrd);
	vector<int> AL, AR, BL, BR;
	if(!chk[A[0]] && !chk[B[0]]) { // off off
		if(2 == N) {
			ask(A[0]);
			int ret = ask(B[0]) - Delta;
			if(1 == ret) {
				AnsV.eb(A[0], B[0]);
				AnsV.eb(A[1], B[1]);
				Delta += 1;
			} else {
				AnsV.eb(A[0], B[1]);
				AnsV.eb(A[1], B[0]);
				Delta += 2;
			}
			return;
		}
		if(3 == N) {
			ask(A[0]);
			int ret = ask(B[0]) - Delta;
			if(1 == ret) {
				AnsV.eb(A[0], B[0]);
				Delta++;
				solve({A[1], A[2]}, {B[1], B[2]});
				return;
			}
			ret = ask(B[1]) - Delta;
			if(2 == ret) {
				AnsV.eb(A[0], B[1]);
				ret = ask(A[1]) - Delta;
				if(2 == ret) {
					AnsV.eb(A[1], B[0]);
					AnsV.eb(A[2], B[2]);
					Delta += 2;
					return;
				}
				AnsV.eb(A[2], B[0]);
				AnsV.eb(A[1], B[2]);
				Delta += 3;
				return;
			}
			AnsV.eb(A[0], B[2]);
			Delta++;
			solve({A[1], A[2]}, {B[0], B[1]});
			return;
		}
		for(int i = 0; i < M; i++) {
			ask(A[i]);
			AL.eb(A[i]);
		}
		for(int i = M; i < N; i++) AR.eb(A[i]);
		{
			int i = 0, h = M;
			for(int v, ret; sz(BL) < M && sz(BR) < K; i++) {
				v = B[i];
				ret = ask(v) - Delta;
				if(h == ret) {
					BL.eb(v);
				} else {
					BR.eb(v);
				}
				h = ret;
			}
			vector<int> Bleft;
			for(int j = i; j < N; j++) Bleft.eb(B[j]);
			if(sz(BL) == M) {
				Delta += sz(BR);
				solve(AL, BL);
				Delta -= sz(BR);
				wellsolve(AR, BR, Bleft);
				return;
			} else {
				Delta += M;
				solve(AR, BR);
				Delta -= M;
				wellsolve(AL, BL, Bleft);
				return;
			}
		}
	} else if(!chk[A[0]] && chk[B[0]]) { // off on
		solve(B, A);
		return;
	} else if(!chk[B[0]]) { // on off
		if(2 == N) {
			ask(A[0]);
			int ret = ask(B[0]) - Delta;
			if(2 == ret) {
				AnsV.eb(A[0], B[0]);
				AnsV.eb(A[1], B[1]);
				Delta += 2;
			} else {
				AnsV.eb(A[0], B[1]);
				AnsV.eb(A[1], B[0]);
				Delta += 1;
			}
			return;
		}
		if(3 == N) {
			ask(A[0]);
			int ret = ask(B[0]) - Delta;
			if(3 == ret) {
				AnsV.eb(A[0], B[0]);
				Delta++;
				solve({A[1], A[2]}, {B[1], B[2]});
				return;
			}
			ret = ask(B[1]) - Delta;
			if(3 == ret) {
				AnsV.eb(A[0], B[1]);
				ret = ask(A[1]) - Delta;
				if(3 == ret) {
					AnsV.eb(A[1], B[0]);
					AnsV.eb(A[2], B[2]);
					Delta += 3;
					return;
				}
				AnsV.eb(A[1], B[2]);
				AnsV.eb(A[2], B[0]);
				Delta += 2;
				return;
			}
			AnsV.eb(A[0], B[2]);
			solve({A[1], A[2]}, {B[0], B[1]});
			return;
		}
		for(int i = 0; i < M; i++) AL.eb(A[i]);
		for(int i = M; i < N; i++) {
			ask(A[i]);
			AR.eb(A[i]);
		}
		{
			int i = 0, h = M;
			for(int v, ret; sz(BL) < M && sz(BR) < K; i++) {
				v = B[i];
				ret = ask(v) - Delta;
				if(h == ret) {
					BL.eb(v);
				} else {
					BR.eb(v);
				}
				h = ret;
			}
			vector<int> Bleft;
			for(int j = i; j < N; j++) Bleft.eb(B[j]);
			if(sz(BL) == M) {
				Delta += sz(BR);
				solve(AL, BL);
				Delta -= sz(BR);
				wellsolve(AR, BR, Bleft);
				return;
			} else {
				Delta += M;
				solve(AR, BR);
				Delta -= M;
				wellsolve(AL, BL, Bleft);
				return;
			}
		}
	} else { // on on
		if(2 == N) {
			ask(A[0]);
			int ret = ask(B[0]) - Delta;
			if(1 == ret) {
				AnsV.eb(A[0], B[0]);
				AnsV.eb(A[1], B[1]);
				Delta += 1;
			} else {
				AnsV.eb(A[0], B[1]);
				AnsV.eb(A[1], B[0]);
				Delta += 2;
			}
			return;
		}
		if(3 == N) {
			ask(A[0]);
			int ret = ask(B[0]) - Delta;
			if(2 == ret) {
				AnsV.eb(A[0], B[0]);
				solve({A[1], A[2]}, {B[1], B[2]});
				return;
			}
			ret = ask(B[1]) - Delta;
			if(2 == ret) {
				AnsV.eb(A[0], B[1]);
				ret = ask(A[1]) - Delta;
				if(1 == ret) {
					AnsV.eb(A[1], B[0]);
					AnsV.eb(A[2], B[2]);
					Delta++;
					return;
				}
				AnsV.eb(A[1], B[2]);
				AnsV.eb(A[2], B[0]);
				Delta += 2;
				return;
			}
			AnsV.eb(A[0], B[2]);
			Delta++;
			solve({A[1], A[2]}, {B[0], B[1]});
			return;
		}
		for(int i = 0; i < M; i++) AL.eb(A[i]);
		for(int i = M; i < N; i++) {
			ask(A[i]);
			AR.eb(A[i]);
		}
		{
			int i = 0, h = N;
			for(int v, ret; sz(BL) < M && sz(BR) < K; i++) {
				v = B[i];
				ret = ask(v) - Delta;
				if(ret == h) {
					BL.eb(v);
				} else {
					BR.eb(v);
				}
				h = ret;
			}
			if(sz(BL) == M) {
				if(sz(BR) < K - sz(BR)) {
					for(int v : BR) ask(v);
				} else {
					for(int j = i; j < N; j++) ask(B[j]);
				}
				for(int j = i; j < N; j++) BR.eb(B[j]);
			} else {
				if(sz(BL) < M - sz(BL)) {
					for(int v : BL) ask(v);
				} else {
					for(int j = i; j < N; j++) ask(B[j]);
				}
				for(int j = i; j < N; j++) BL.eb(B[j]);
			}
		}
		if(chk[BR[0]]) Delta += K;
		solve(AL, BL);
		if(chk[BR[0]]) Delta -= K;
		solve(AR, BR);
	}
}

void solve(vector<int> A, vector<int> Bon, vector<int> Boff) {
	int N = sz(A), M = N >> 1, K = N-M;
	if(Bon.empty()) {
		solve(A, Boff);
		return;
	}
	if(Boff.empty()) {
		solve(A, Bon);
		return;
	}
	if(!chk[Bon[0]]) swap(Bon, Boff);
	if(!chk[A[0]]) { // off
		if(2 == N) {
			int ret = ask(A[0]) - Delta;
			if(1 == ret) {
				AnsV.eb(A[0], Bon[0]);
				AnsV.eb(A[1], Boff[0]);
				Delta++;
				return;
			}
			AnsV.eb(A[0], Boff[0]);
			AnsV.eb(A[1], Bon[0]);
			Delta += 2;
			return;
		}
		for(; sz(Bon) < M;) {
			ask(Boff.back());
			Bon.eb(Boff.back());
			Boff.pop_back();
		}
		for(; M < sz(Bon);) {
			ask(Bon.back());
			Boff.eb(Bon.back());
			Bon.pop_back();
		}
		vector<int> AL, AR;
		{
			int i = 0, h = M;
			for(int v, ret; sz(AL) < M && sz(AR) < K; i++) {
				v = A[i];
				ret = ask(v) - Delta;
				if(h == ret) {
					AL.eb(v);
				} else {
					AR.eb(v);
				}
				h = ret;
			}
			if(sz(AL) == M) {
				Delta += sz(AR);
				solve(AL, Bon);
				Delta -= sz(AR);
				vector<int> Aoff;
				for(int j = i; j < N; j++) Aoff.eb(A[j]);
				wellsolve(Boff, AR, Aoff);
				return;
			} else {
				Delta += M;
				solve(AR, Boff);
				Delta -= M;
				vector<int> Aoff;
				for(int j = i; j < N; j++) Aoff.eb(A[j]);
				wellsolve(Bon, AL, Aoff);
				return;
			}
		}
	} else { // on
		if(2 == N) {
			int ret = ask(A[0]) - Delta;
			if(1 == ret) {
				AnsV.eb(A[0], Boff[0]);
				AnsV.eb(A[1], Bon[0]);
				Delta++;
				return;
			}
			AnsV.eb(A[0], Bon[0]);
			AnsV.eb(A[1], Boff[0]);
			Delta += 2;
			return;
		}
		for(; sz(Bon) < M;) {
			ask(Boff.back());
			Bon.eb(Boff.back());
			Boff.pop_back();
		}
		for(; M < sz(Bon);) {
			ask(Bon.back());
			Boff.eb(Bon.back());
			Bon.pop_back();
		}
		vector<int> AL, AR;
		{
			int i = 0, h = N;
			for(int v, ret; sz(AL) < M && sz(AR) < K; i++) {
				v = A[i];
				ret = ask(v) - Delta;
				if(h == ret) {
					AL.eb(v);
				} else {
					AR.eb(v);
				}
				h = ret;
			}
			if(sz(AL) == M) {
				Delta += K - sz(AR);
				solve(AL, Bon);
				Delta -= K - sz(AR);
				vector<int> Aon;
				for(int j = i; j < N; j++) Aon.eb(A[j]);
				wellsolve(Boff, Aon, AR);
				return;
			} else {
				Delta += M;
				solve(AR, Boff);
				Delta -= M;
				vector<int> Aon;
				for(int j = i; j < N; j++) Aon.eb(A[j]);
				wellsolve(Bon, Aon, AL);
				return;
			}
		}
	}
}


void Solve(int N) {
	{
		vector<int> O, A, B;
		for(int i = 1; i <= N*2; i++) O.eb(i);
		shuffle(allv(O), mtrd);

		int oi = 0, h = 0;
		for(int i, ret; h < N && oi < N*2; oi++) {
			i = O[oi];
			ret = ask(i);
			if(ret == h) {
				B.eb(i);
			} else {
				A.eb(i);
			}
			h = ret;
		}
		if(N+N - oi < oi - N) {
			for(int i = oi; i < N*2; i++) ask(O[i]);
		} else {
			for(int v : B) ask(v);
		}
		for(int i = oi; i < N*2; i++) B.eb(O[i]);

		solve(A, B);
	}

	for(auto &v : AnsV) Answer(v.first, v.second);
}

Compilation message

minerals.cpp: In function 'void wellsolve(std::vector<int>, std::vector<int>, std::vector<int>)':
minerals.cpp:26:29: warning: unused variable 'K' [-Wunused-variable]
  int N = sz(A), M = N >> 1, K = N-M;
                             ^
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 256 KB Output is correct
2 Correct 2 ms 256 KB Output is correct
3 Correct 2 ms 256 KB Output is correct
4 Correct 2 ms 256 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 384 KB Output is correct
2 Correct 5 ms 512 KB Output is correct
3 Correct 9 ms 640 KB Output is correct
4 Correct 16 ms 1024 KB Output is correct
5 Correct 33 ms 1664 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 256 KB Output is correct
2 Correct 2 ms 256 KB Output is correct
3 Correct 2 ms 256 KB Output is correct
4 Correct 2 ms 256 KB Output is correct
5 Correct 4 ms 384 KB Output is correct
6 Correct 5 ms 512 KB Output is correct
7 Correct 9 ms 640 KB Output is correct
8 Correct 16 ms 1024 KB Output is correct
9 Correct 33 ms 1664 KB Output is correct
10 Correct 3 ms 384 KB Output is correct
11 Correct 22 ms 1408 KB Output is correct
12 Correct 33 ms 1664 KB Output is correct
13 Correct 33 ms 1664 KB Output is correct
14 Correct 31 ms 1636 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 256 KB Output is correct
2 Correct 2 ms 256 KB Output is correct
3 Correct 2 ms 256 KB Output is correct
4 Correct 2 ms 256 KB Output is correct
5 Correct 4 ms 384 KB Output is correct
6 Correct 5 ms 512 KB Output is correct
7 Correct 9 ms 640 KB Output is correct
8 Correct 16 ms 1024 KB Output is correct
9 Correct 33 ms 1664 KB Output is correct
10 Correct 3 ms 384 KB Output is correct
11 Correct 22 ms 1408 KB Output is correct
12 Correct 33 ms 1664 KB Output is correct
13 Correct 33 ms 1664 KB Output is correct
14 Correct 31 ms 1636 KB Output is correct
15 Correct 98 ms 4256 KB Output is correct
16 Correct 99 ms 4172 KB Output is correct
17 Correct 93 ms 4128 KB Output is correct
18 Correct 90 ms 4060 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 256 KB Output is correct
2 Correct 2 ms 256 KB Output is correct
3 Correct 2 ms 256 KB Output is correct
4 Correct 2 ms 256 KB Output is correct
5 Correct 4 ms 384 KB Output is correct
6 Correct 5 ms 512 KB Output is correct
7 Correct 9 ms 640 KB Output is correct
8 Correct 16 ms 1024 KB Output is correct
9 Correct 33 ms 1664 KB Output is correct
10 Correct 3 ms 384 KB Output is correct
11 Correct 22 ms 1408 KB Output is correct
12 Correct 33 ms 1664 KB Output is correct
13 Correct 33 ms 1664 KB Output is correct
14 Correct 31 ms 1636 KB Output is correct
15 Correct 98 ms 4256 KB Output is correct
16 Correct 99 ms 4172 KB Output is correct
17 Correct 93 ms 4128 KB Output is correct
18 Correct 90 ms 4060 KB Output is correct
19 Correct 91 ms 4208 KB Output is correct
20 Correct 93 ms 4336 KB Output is correct
21 Correct 98 ms 4128 KB Output is correct
22 Correct 92 ms 3972 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 256 KB Output is correct
2 Correct 2 ms 256 KB Output is correct
3 Correct 2 ms 256 KB Output is correct
4 Correct 2 ms 256 KB Output is correct
5 Correct 4 ms 384 KB Output is correct
6 Correct 5 ms 512 KB Output is correct
7 Correct 9 ms 640 KB Output is correct
8 Correct 16 ms 1024 KB Output is correct
9 Correct 33 ms 1664 KB Output is correct
10 Correct 3 ms 384 KB Output is correct
11 Correct 22 ms 1408 KB Output is correct
12 Correct 33 ms 1664 KB Output is correct
13 Correct 33 ms 1664 KB Output is correct
14 Correct 31 ms 1636 KB Output is correct
15 Correct 98 ms 4256 KB Output is correct
16 Correct 99 ms 4172 KB Output is correct
17 Correct 93 ms 4128 KB Output is correct
18 Correct 90 ms 4060 KB Output is correct
19 Correct 91 ms 4208 KB Output is correct
20 Correct 93 ms 4336 KB Output is correct
21 Correct 98 ms 4128 KB Output is correct
22 Correct 92 ms 3972 KB Output is correct
23 Correct 105 ms 4284 KB Output is correct
24 Correct 98 ms 4272 KB Output is correct
25 Correct 103 ms 4264 KB Output is correct
26 Correct 101 ms 4080 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 256 KB Output is correct
2 Correct 2 ms 256 KB Output is correct
3 Correct 2 ms 256 KB Output is correct
4 Correct 2 ms 256 KB Output is correct
5 Correct 4 ms 384 KB Output is correct
6 Correct 5 ms 512 KB Output is correct
7 Correct 9 ms 640 KB Output is correct
8 Correct 16 ms 1024 KB Output is correct
9 Correct 33 ms 1664 KB Output is correct
10 Correct 3 ms 384 KB Output is correct
11 Correct 22 ms 1408 KB Output is correct
12 Correct 33 ms 1664 KB Output is correct
13 Correct 33 ms 1664 KB Output is correct
14 Correct 31 ms 1636 KB Output is correct
15 Correct 98 ms 4256 KB Output is correct
16 Correct 99 ms 4172 KB Output is correct
17 Correct 93 ms 4128 KB Output is correct
18 Correct 90 ms 4060 KB Output is correct
19 Correct 91 ms 4208 KB Output is correct
20 Correct 93 ms 4336 KB Output is correct
21 Correct 98 ms 4128 KB Output is correct
22 Correct 92 ms 3972 KB Output is correct
23 Correct 105 ms 4284 KB Output is correct
24 Correct 98 ms 4272 KB Output is correct
25 Correct 103 ms 4264 KB Output is correct
26 Correct 101 ms 4080 KB Output is correct
27 Correct 103 ms 4464 KB Output is correct
28 Correct 106 ms 4384 KB Output is correct
29 Correct 98 ms 4388 KB Output is correct
30 Correct 97 ms 4208 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 256 KB Output is correct
2 Correct 2 ms 256 KB Output is correct
3 Correct 2 ms 256 KB Output is correct
4 Correct 2 ms 256 KB Output is correct
5 Correct 4 ms 384 KB Output is correct
6 Correct 5 ms 512 KB Output is correct
7 Correct 9 ms 640 KB Output is correct
8 Correct 16 ms 1024 KB Output is correct
9 Correct 33 ms 1664 KB Output is correct
10 Correct 3 ms 384 KB Output is correct
11 Correct 22 ms 1408 KB Output is correct
12 Correct 33 ms 1664 KB Output is correct
13 Correct 33 ms 1664 KB Output is correct
14 Correct 31 ms 1636 KB Output is correct
15 Correct 98 ms 4256 KB Output is correct
16 Correct 99 ms 4172 KB Output is correct
17 Correct 93 ms 4128 KB Output is correct
18 Correct 90 ms 4060 KB Output is correct
19 Correct 91 ms 4208 KB Output is correct
20 Correct 93 ms 4336 KB Output is correct
21 Correct 98 ms 4128 KB Output is correct
22 Correct 92 ms 3972 KB Output is correct
23 Correct 105 ms 4284 KB Output is correct
24 Correct 98 ms 4272 KB Output is correct
25 Correct 103 ms 4264 KB Output is correct
26 Correct 101 ms 4080 KB Output is correct
27 Correct 103 ms 4464 KB Output is correct
28 Correct 106 ms 4384 KB Output is correct
29 Correct 98 ms 4388 KB Output is correct
30 Correct 97 ms 4208 KB Output is correct
31 Correct 101 ms 4468 KB Output is correct
32 Correct 99 ms 4312 KB Output is correct
33 Correct 101 ms 4360 KB Output is correct
34 Correct 96 ms 4372 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 256 KB Output is correct
2 Correct 2 ms 256 KB Output is correct
3 Correct 2 ms 256 KB Output is correct
4 Correct 2 ms 256 KB Output is correct
5 Correct 4 ms 384 KB Output is correct
6 Correct 5 ms 512 KB Output is correct
7 Correct 9 ms 640 KB Output is correct
8 Correct 16 ms 1024 KB Output is correct
9 Correct 33 ms 1664 KB Output is correct
10 Correct 3 ms 384 KB Output is correct
11 Correct 22 ms 1408 KB Output is correct
12 Correct 33 ms 1664 KB Output is correct
13 Correct 33 ms 1664 KB Output is correct
14 Correct 31 ms 1636 KB Output is correct
15 Correct 98 ms 4256 KB Output is correct
16 Correct 99 ms 4172 KB Output is correct
17 Correct 93 ms 4128 KB Output is correct
18 Correct 90 ms 4060 KB Output is correct
19 Correct 91 ms 4208 KB Output is correct
20 Correct 93 ms 4336 KB Output is correct
21 Correct 98 ms 4128 KB Output is correct
22 Correct 92 ms 3972 KB Output is correct
23 Correct 105 ms 4284 KB Output is correct
24 Correct 98 ms 4272 KB Output is correct
25 Correct 103 ms 4264 KB Output is correct
26 Correct 101 ms 4080 KB Output is correct
27 Correct 103 ms 4464 KB Output is correct
28 Correct 106 ms 4384 KB Output is correct
29 Correct 98 ms 4388 KB Output is correct
30 Correct 97 ms 4208 KB Output is correct
31 Correct 101 ms 4468 KB Output is correct
32 Correct 99 ms 4312 KB Output is correct
33 Correct 101 ms 4360 KB Output is correct
34 Correct 96 ms 4372 KB Output is correct
35 Incorrect 105 ms 4464 KB Wrong Answer [2]
36 Halted 0 ms 0 KB -