답안 #830778

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
830778 2023-08-19T10:26:53 Z rainboy Toxic Gene (NOI23_toxic) C++17
100 / 100
10 ms 468 KB
/* https://github.com/noisg/noi_2023/blob/master/final/model%20solutions/model_q5_toxic.cpp */
#include "toxic.h"
#include <cstring>
#include <vector>

using namespace std;

typedef vector<bool> vb;
typedef vector<int> vi;

const int N = 300, K = 8, B = (N + K - 1) / K;	/* K = floor(log2(N)) */

int min(int a, int b) { return a < b ? a : b; }

unsigned int X;

int rand_() {
	return (X *= 3) >> 1;
}

int ii[N], ii_[N], jj[N], n_, m; char cc[N + 1], has[B];

void shuffle(int *ii, int n) {
	X = 12345;
	for (int i = 0; i < n; i++) {
		int j = rand_() % (i + 1), tmp;
		tmp = ii[i], ii[i] = ii[j], ii[j] = tmp;
	}
}

void search(int *ii, int n) {
	int lower = 0, upper = n;
	while (upper - lower > 1) {
		int mid = (lower + upper) / 2;
		vi qq;
		for (int h = lower; h < mid; h++)
			qq.push_back(ii[h] + 1);
		int k = min(m, K);
		for (int h = m - k; h < m; h++)
			for (int t = 0; t < 1 << h - m + k; t++)
				qq.push_back(jj[h] + 1);
		int b = query_sample(qq);
		if (b == qq.size()) {
			for (int h = lower; h < mid; h++)
				cc[ii[h]] = 'R';
			lower = mid;
		} else {
			for (int h = mid; h < upper; h++)
				ii_[n_++] = ii[h];
			for (int h = m - k; h < m; h++)
				cc[jj[h]] = (b & 1 << h - m + k) != 0 ? 'S' : 'R';
			m -= k;
			upper = mid;
		}
	}
	cc[ii[lower]] = 'T';
}

void determine_type(int n) {
	int n1 = n;
	memset(cc, '?', n * sizeof *cc);
	for (int i = 0; i < n; i++)
		ii[i] = i;
	shuffle(ii, n);
	for (int g = 0; g * K < n; g++) {
		int l = g * K, r = min((g + 1) * K, n);
		vi qq;
		for (int h = l; h < r; h++)
			for (int t = 0; t < 1 << h - l; t++)
				qq.push_back(ii[h] + 1);
		int b = query_sample(qq);
		if (b == (1 << r - l) - 1) {
			has[g] = 0;
			for (int h = l; h < r; h++)
				jj[m++] = ii[h];
		} else {
			has[g] = 1;
			for (int h = l; h < r; h++)
				if ((b & 1 << h - l) != 0)
					cc[ii[h]] = 'S';
		}
	}
	n_ = 0;
	for (int g = 0; g * K < n; g++)
		if (has[g]) {
			int l = g * K, r = min((g + 1) * K, n), k = 0;
			for (int h = l; h < r; h++)
				if (cc[ii[h]] != 'S')
					ii[l + k++] = ii[h];
			search(ii + l, k);
		}
	memcpy(ii, ii_, (n = n_) * sizeof *ii_);
	while (n > 0) {
		shuffle(ii, n);
		n_ = 0;
		for (int g = 0; g * K < n; g++) {
			int l = g * K, r = min((g + 1) * K, n);
			vi qq;
			for (int h = l; h < r; h++)
				qq.push_back(ii[h] + 1);
			int k = min(m, K);
			for (int h = m - k; h < m; h++)
				for (int t = 0; t < 1 << h - m + k; t++)
					qq.push_back(jj[h] + 1);
			int b = query_sample(qq);
			if (b == qq.size())
				for (int h = l; h < r; h++)
					cc[ii[h]] = 'R';
			else {
				for (int h = m - k; h < m; h++)
					cc[jj[h]] = (b & 1 << h - m + k) != 0 ? 'S' : 'R';
				m -= k;
				search(ii + l, r - l);
			}
		}
		memcpy(ii, ii_, (n = n_) * sizeof *ii_);
	}
	int t = 0;
	while (cc[t] != 'T')
		t++;
	while (m > 0) {
		int k = min(m, K);
		vi qq;
		qq.push_back(t + 1);
		for (int h = m - k; h < m; h++)
			for (int t = 0; t < 1 << h - m + k; t++)
				qq.push_back(jj[h] + 1);
		int b = query_sample(qq);
		for (int h = m - k; h < m; h++)
			cc[jj[h]] = (b & 1 << h - m + k) != 0 ? 'S' : 'R';
		m -= k;
	}
	for (int i = 0; i < n1; i++)
		answer_type(i + 1, cc[i]);
}

Compilation message

toxic.cpp: In function 'void search(int*, int)':
toxic.cpp:40:35: warning: suggest parentheses around '+' inside '<<' [-Wparentheses]
   40 |    for (int t = 0; t < 1 << h - m + k; t++)
      |                             ~~~~~~^~~
toxic.cpp:43:9: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   43 |   if (b == qq.size()) {
      |       ~~^~~~~~~~~~~~
toxic.cpp:51:33: warning: suggest parentheses around '+' inside '<<' [-Wparentheses]
   51 |     cc[jj[h]] = (b & 1 << h - m + k) != 0 ? 'S' : 'R';
      |                           ~~~~~~^~~
toxic.cpp: In function 'void determine_type(int)':
toxic.cpp:69:31: warning: suggest parentheses around '-' inside '<<' [-Wparentheses]
   69 |    for (int t = 0; t < 1 << h - l; t++)
      |                             ~~^~~
toxic.cpp:72:20: warning: suggest parentheses around '-' inside '<<' [-Wparentheses]
   72 |   if (b == (1 << r - l) - 1) {
      |                  ~~^~~
toxic.cpp:79:21: warning: suggest parentheses around '-' inside '<<' [-Wparentheses]
   79 |     if ((b & 1 << h - l) != 0)
      |                   ~~^~~
toxic.cpp:103:36: warning: suggest parentheses around '+' inside '<<' [-Wparentheses]
  103 |     for (int t = 0; t < 1 << h - m + k; t++)
      |                              ~~~~~~^~~
toxic.cpp:106:10: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  106 |    if (b == qq.size())
      |        ~~^~~~~~~~~~~~
toxic.cpp:111:34: warning: suggest parentheses around '+' inside '<<' [-Wparentheses]
  111 |      cc[jj[h]] = (b & 1 << h - m + k) != 0 ? 'S' : 'R';
      |                            ~~~~~~^~~
toxic.cpp:126:35: warning: suggest parentheses around '+' inside '<<' [-Wparentheses]
  126 |    for (int t = 0; t < 1 << h - m + k; t++)
      |                             ~~~~~~^~~
toxic.cpp:130:32: warning: suggest parentheses around '+' inside '<<' [-Wparentheses]
  130 |    cc[jj[h]] = (b & 1 << h - m + k) != 0 ? 'S' : 'R';
      |                          ~~~~~~^~~
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 8 ms 340 KB Output is correct
3 Correct 8 ms 340 KB Output is correct
4 Correct 8 ms 340 KB Output is correct
5 Correct 10 ms 372 KB Output is correct
6 Correct 8 ms 340 KB Output is correct
7 Correct 8 ms 340 KB Output is correct
8 Correct 8 ms 340 KB Output is correct
9 Correct 8 ms 340 KB Output is correct
10 Correct 8 ms 340 KB Output is correct
11 Correct 8 ms 400 KB Output is correct
12 Correct 8 ms 340 KB Output is correct
13 Correct 8 ms 396 KB Output is correct
14 Correct 7 ms 396 KB Output is correct
15 Correct 8 ms 340 KB Output is correct
16 Correct 7 ms 396 KB Output is correct
17 Correct 7 ms 340 KB Output is correct
18 Correct 8 ms 340 KB Output is correct
19 Correct 8 ms 344 KB Output is correct
20 Correct 8 ms 340 KB Output is correct
21 Correct 7 ms 340 KB Output is correct
22 Correct 7 ms 340 KB Output is correct
23 Correct 6 ms 340 KB Output is correct
24 Correct 8 ms 388 KB Output is correct
25 Correct 10 ms 468 KB Output is correct
26 Correct 8 ms 340 KB Output is correct
27 Correct 9 ms 336 KB Output is correct
28 Correct 7 ms 340 KB Output is correct
29 Correct 8 ms 332 KB Output is correct
30 Correct 8 ms 372 KB Output is correct
31 Correct 8 ms 336 KB Output is correct
32 Correct 8 ms 340 KB Output is correct
33 Correct 7 ms 396 KB Output is correct
34 Correct 7 ms 388 KB Output is correct
35 Correct 8 ms 400 KB Output is correct
36 Correct 1 ms 212 KB Output is correct