Submission #120693

# Submission time Handle Problem Language Result Execution time Memory
120693 2019-06-25T08:26:16 Z 윤교준(#2960) Minerals (JOI19_minerals) C++14
90 / 100
108 ms 4592 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;
			}
			vector<int> Bleft;
			for(int j = i; j < N; j++) Bleft.eb(B[j]);
			if(sz(BL) == M) {
				Delta += K - sz(BR);
				solve(AL, BL);
				Delta -= K - sz(BR);
				wellsolve(AR, BR, Bleft);
				return;
			} else {
				Delta += M;
				solve(AR, BR);
				Delta -= M;
				wellsolve(AL, BL, Bleft);
				return;
			}
		}
	}
}

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;
                             ^
# Verdict Execution time Memory Grader output
1 Correct 2 ms 256 KB Output is correct
2 Correct 2 ms 384 KB Output is correct
3 Correct 2 ms 256 KB Output is correct
4 Correct 2 ms 256 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 512 KB Output is correct
2 Correct 5 ms 512 KB Output is correct
3 Correct 8 ms 640 KB Output is correct
4 Correct 16 ms 1152 KB Output is correct
5 Correct 31 ms 1728 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 256 KB Output is correct
2 Correct 2 ms 384 KB Output is correct
3 Correct 2 ms 256 KB Output is correct
4 Correct 2 ms 256 KB Output is correct
5 Correct 3 ms 512 KB Output is correct
6 Correct 5 ms 512 KB Output is correct
7 Correct 8 ms 640 KB Output is correct
8 Correct 16 ms 1152 KB Output is correct
9 Correct 31 ms 1728 KB Output is correct
10 Correct 3 ms 384 KB Output is correct
11 Correct 21 ms 1408 KB Output is correct
12 Correct 31 ms 1840 KB Output is correct
13 Correct 31 ms 1796 KB Output is correct
14 Correct 31 ms 1712 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 256 KB Output is correct
2 Correct 2 ms 384 KB Output is correct
3 Correct 2 ms 256 KB Output is correct
4 Correct 2 ms 256 KB Output is correct
5 Correct 3 ms 512 KB Output is correct
6 Correct 5 ms 512 KB Output is correct
7 Correct 8 ms 640 KB Output is correct
8 Correct 16 ms 1152 KB Output is correct
9 Correct 31 ms 1728 KB Output is correct
10 Correct 3 ms 384 KB Output is correct
11 Correct 21 ms 1408 KB Output is correct
12 Correct 31 ms 1840 KB Output is correct
13 Correct 31 ms 1796 KB Output is correct
14 Correct 31 ms 1712 KB Output is correct
15 Correct 96 ms 4124 KB Output is correct
16 Correct 90 ms 4296 KB Output is correct
17 Correct 94 ms 4336 KB Output is correct
18 Correct 87 ms 3996 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 256 KB Output is correct
2 Correct 2 ms 384 KB Output is correct
3 Correct 2 ms 256 KB Output is correct
4 Correct 2 ms 256 KB Output is correct
5 Correct 3 ms 512 KB Output is correct
6 Correct 5 ms 512 KB Output is correct
7 Correct 8 ms 640 KB Output is correct
8 Correct 16 ms 1152 KB Output is correct
9 Correct 31 ms 1728 KB Output is correct
10 Correct 3 ms 384 KB Output is correct
11 Correct 21 ms 1408 KB Output is correct
12 Correct 31 ms 1840 KB Output is correct
13 Correct 31 ms 1796 KB Output is correct
14 Correct 31 ms 1712 KB Output is correct
15 Correct 96 ms 4124 KB Output is correct
16 Correct 90 ms 4296 KB Output is correct
17 Correct 94 ms 4336 KB Output is correct
18 Correct 87 ms 3996 KB Output is correct
19 Correct 90 ms 4208 KB Output is correct
20 Correct 92 ms 4332 KB Output is correct
21 Correct 93 ms 4336 KB Output is correct
22 Correct 94 ms 4032 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 256 KB Output is correct
2 Correct 2 ms 384 KB Output is correct
3 Correct 2 ms 256 KB Output is correct
4 Correct 2 ms 256 KB Output is correct
5 Correct 3 ms 512 KB Output is correct
6 Correct 5 ms 512 KB Output is correct
7 Correct 8 ms 640 KB Output is correct
8 Correct 16 ms 1152 KB Output is correct
9 Correct 31 ms 1728 KB Output is correct
10 Correct 3 ms 384 KB Output is correct
11 Correct 21 ms 1408 KB Output is correct
12 Correct 31 ms 1840 KB Output is correct
13 Correct 31 ms 1796 KB Output is correct
14 Correct 31 ms 1712 KB Output is correct
15 Correct 96 ms 4124 KB Output is correct
16 Correct 90 ms 4296 KB Output is correct
17 Correct 94 ms 4336 KB Output is correct
18 Correct 87 ms 3996 KB Output is correct
19 Correct 90 ms 4208 KB Output is correct
20 Correct 92 ms 4332 KB Output is correct
21 Correct 93 ms 4336 KB Output is correct
22 Correct 94 ms 4032 KB Output is correct
23 Correct 95 ms 4356 KB Output is correct
24 Correct 96 ms 4468 KB Output is correct
25 Correct 92 ms 4380 KB Output is correct
26 Correct 96 ms 4256 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 256 KB Output is correct
2 Correct 2 ms 384 KB Output is correct
3 Correct 2 ms 256 KB Output is correct
4 Correct 2 ms 256 KB Output is correct
5 Correct 3 ms 512 KB Output is correct
6 Correct 5 ms 512 KB Output is correct
7 Correct 8 ms 640 KB Output is correct
8 Correct 16 ms 1152 KB Output is correct
9 Correct 31 ms 1728 KB Output is correct
10 Correct 3 ms 384 KB Output is correct
11 Correct 21 ms 1408 KB Output is correct
12 Correct 31 ms 1840 KB Output is correct
13 Correct 31 ms 1796 KB Output is correct
14 Correct 31 ms 1712 KB Output is correct
15 Correct 96 ms 4124 KB Output is correct
16 Correct 90 ms 4296 KB Output is correct
17 Correct 94 ms 4336 KB Output is correct
18 Correct 87 ms 3996 KB Output is correct
19 Correct 90 ms 4208 KB Output is correct
20 Correct 92 ms 4332 KB Output is correct
21 Correct 93 ms 4336 KB Output is correct
22 Correct 94 ms 4032 KB Output is correct
23 Correct 95 ms 4356 KB Output is correct
24 Correct 96 ms 4468 KB Output is correct
25 Correct 92 ms 4380 KB Output is correct
26 Correct 96 ms 4256 KB Output is correct
27 Correct 96 ms 4344 KB Output is correct
28 Correct 97 ms 4592 KB Output is correct
29 Correct 93 ms 4332 KB Output is correct
30 Correct 94 ms 4336 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 256 KB Output is correct
2 Correct 2 ms 384 KB Output is correct
3 Correct 2 ms 256 KB Output is correct
4 Correct 2 ms 256 KB Output is correct
5 Correct 3 ms 512 KB Output is correct
6 Correct 5 ms 512 KB Output is correct
7 Correct 8 ms 640 KB Output is correct
8 Correct 16 ms 1152 KB Output is correct
9 Correct 31 ms 1728 KB Output is correct
10 Correct 3 ms 384 KB Output is correct
11 Correct 21 ms 1408 KB Output is correct
12 Correct 31 ms 1840 KB Output is correct
13 Correct 31 ms 1796 KB Output is correct
14 Correct 31 ms 1712 KB Output is correct
15 Correct 96 ms 4124 KB Output is correct
16 Correct 90 ms 4296 KB Output is correct
17 Correct 94 ms 4336 KB Output is correct
18 Correct 87 ms 3996 KB Output is correct
19 Correct 90 ms 4208 KB Output is correct
20 Correct 92 ms 4332 KB Output is correct
21 Correct 93 ms 4336 KB Output is correct
22 Correct 94 ms 4032 KB Output is correct
23 Correct 95 ms 4356 KB Output is correct
24 Correct 96 ms 4468 KB Output is correct
25 Correct 92 ms 4380 KB Output is correct
26 Correct 96 ms 4256 KB Output is correct
27 Correct 96 ms 4344 KB Output is correct
28 Correct 97 ms 4592 KB Output is correct
29 Correct 93 ms 4332 KB Output is correct
30 Correct 94 ms 4336 KB Output is correct
31 Correct 108 ms 4588 KB Output is correct
32 Correct 105 ms 4508 KB Output is correct
33 Correct 101 ms 4508 KB Output is correct
34 Correct 99 ms 4456 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 256 KB Output is correct
2 Correct 2 ms 384 KB Output is correct
3 Correct 2 ms 256 KB Output is correct
4 Correct 2 ms 256 KB Output is correct
5 Correct 3 ms 512 KB Output is correct
6 Correct 5 ms 512 KB Output is correct
7 Correct 8 ms 640 KB Output is correct
8 Correct 16 ms 1152 KB Output is correct
9 Correct 31 ms 1728 KB Output is correct
10 Correct 3 ms 384 KB Output is correct
11 Correct 21 ms 1408 KB Output is correct
12 Correct 31 ms 1840 KB Output is correct
13 Correct 31 ms 1796 KB Output is correct
14 Correct 31 ms 1712 KB Output is correct
15 Correct 96 ms 4124 KB Output is correct
16 Correct 90 ms 4296 KB Output is correct
17 Correct 94 ms 4336 KB Output is correct
18 Correct 87 ms 3996 KB Output is correct
19 Correct 90 ms 4208 KB Output is correct
20 Correct 92 ms 4332 KB Output is correct
21 Correct 93 ms 4336 KB Output is correct
22 Correct 94 ms 4032 KB Output is correct
23 Correct 95 ms 4356 KB Output is correct
24 Correct 96 ms 4468 KB Output is correct
25 Correct 92 ms 4380 KB Output is correct
26 Correct 96 ms 4256 KB Output is correct
27 Correct 96 ms 4344 KB Output is correct
28 Correct 97 ms 4592 KB Output is correct
29 Correct 93 ms 4332 KB Output is correct
30 Correct 94 ms 4336 KB Output is correct
31 Correct 108 ms 4588 KB Output is correct
32 Correct 105 ms 4508 KB Output is correct
33 Correct 101 ms 4508 KB Output is correct
34 Correct 99 ms 4456 KB Output is correct
35 Incorrect 100 ms 4512 KB Wrong Answer [2]
36 Halted 0 ms 0 KB -