답안 #534205

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
534205 2022-03-08T02:13:34 Z benson1029 버섯 세기 (IOI20_mushrooms) C++14
100 / 100
10 ms 460 KB
#include "mushrooms.h"
#include <bits/stdc++.h>
using namespace std; 

int N;
int ans[6000]; 
int f[20];
vector< vector<int> > query[20], realQuery[20];
vector<int> temp;
vector<int> knownA, knownB;
int rPtr, lPtr;

int findQueryValue(vector<int> v) {
	vector<int> v2;
	v2.clear();
	for(int i=0; i<v.size(); i++) {
		if(knownA.size()>knownB.size()) v2.push_back(knownA[i]);
		else v2.push_back(knownB[i]);
		v2.push_back(v[i]+lPtr);
	}
	if(knownA.size()>knownB.size()) v2.push_back(knownA[v.size()]);
	else v2.push_back(knownB[v.size()]);
	v2.push_back(rPtr);
	int rv = use_machine(v2);
	if(knownB.size()>=knownA.size()) rv=v2.size()-1-rv;
	if(rv%2==1) {
		ans[rPtr] = 1;
		knownB.push_back(rPtr);
	} else {
		ans[rPtr] = 0;
		knownA.push_back(rPtr);
	}
	rPtr--;
	return rv/2;
}

void solve(int lv, int l, vector<int> qAns) {
	vector<int> A, B;
	A.clear(); B.clear();
	if(lv==0) {
		ans[l] = qAns[0];
		return;
	}
	int ctr = 0;
	int sum = 0;
	for(int i=2; i<qAns.size(); i+=2) {
		// use of qAns[0], qAns[i-1] and qAns[i] to derive
		ans[l+f[lv-1]*2+ctr] = (qAns[i-1] - qAns[i] + qAns[0]) % 2;
		B.push_back((qAns[i-1] - qAns[i] + qAns[0]) / 2);
		A.push_back((qAns[i-1] + qAns[i] - qAns[0]) / 2);
		sum += ans[l+f[lv-1]*2+ctr];
		ctr++;
	}
	B.push_back(qAns[0]);
	A.push_back(qAns[qAns.size()-1] - qAns[0] - sum);
	solve(lv-1, l, A);
	solve(lv-1, l+f[lv-1], B);
}

void initFirstPhase() {
	f[0] = 1;
	for(int i=1; i<=10; i++) {
		f[i] = f[i-1] * 2 + (1<<(i-1)) - 1;
	}
	query[0] = {{0}};
	realQuery[0] = {{0}};
	for(int i=1; i<=6; i++) {
		// find the queries for i from i-1
		temp.clear();
		for(int j=f[i-1]; j<2*f[i-1]; j++) temp.push_back(j);
		query[i].push_back(temp);
		int cnt = 0;
		for(int j=0; j<query[i-1].size()-1; j++) {
			temp.clear();
			for(int k:query[i-1][j]) temp.push_back(k);
			for(int k:query[i-1][j]) temp.push_back(k+f[i-1]);
			temp.push_back(f[i-1]*2+cnt);
			query[i].push_back(temp);
			temp.clear();
			for(int k:query[i-1][j]) temp.push_back(k);
			int ptr = 0;
			for(int k=0; k<f[i-1]; k++) {
				if(ptr<query[i-1][j].size() && query[i-1][j][ptr]==k) ptr++;
				else temp.push_back(k+f[i-1]);
			}
			query[i].push_back(temp);
			cnt++;
		}
		realQuery[i] = query[i];
		temp.clear();
		for(int j=0; j<f[i]; j++) temp.push_back(j);
		query[i].push_back(temp);
		temp.clear();
		for(int j=0; j<f[i-1]; j++) temp.push_back(j);
		for(int j=f[i-1]*2; j<f[i]; j++) temp.push_back(j);
		realQuery[i].push_back(temp);
	}
}

void initQuery() {
	knownA.clear();
	knownB.clear();
	knownA.push_back(0);
	lPtr = 3;
	rPtr = 301;
	ans[1] = 0;
	for(int i=1; i<=301; i++) ans[i] = -1;
	int tmp;
	tmp = use_machine({0, 1});
	if(tmp==1) {
		ans[1] = 1;
		knownB.push_back(1);
	} else {
		ans[1] = 0;
		knownA.push_back(1);
	}
	tmp = use_machine({0, 2});
	if(tmp==1) {
		ans[2] = 1;
		knownB.push_back(2);
	} else {
		ans[2] = 0;
		knownA.push_back(2);
	}
}

void apply(int lv) {
	temp.clear();
	for(int i=0; i<query[lv].size(); i++) {
		temp.push_back(findQueryValue(realQuery[lv][i]));
	}
	if(lv>0) temp[temp.size()-1] += temp[0];
	solve(lv, lPtr, temp);
	for(int i=lPtr; i<lPtr+f[lv]; i++) {
		if(ans[i]==1) {
			knownB.push_back(i);
		} else {
			knownA.push_back(i);
		}
	}
	lPtr += f[lv];
}

void Phase1() {
	initFirstPhase();
	initQuery();
	apply(0);
	apply(1);
	apply(2);
	apply(2);
	apply(4);
	apply(5);
	apply(5);
}

int Phase2(int ptr) {
	int ans = 0;
	while(ptr < N) {
		int rv = 0;
		temp.clear();
		if(knownA.size() > knownB.size()) {
			for(int i=0; i<knownA.size(); i++) {
				if(ptr==N) break;
				temp.push_back(knownA[i]);
				if(ptr<N) {
					temp.push_back(ptr);
					ptr++;
				}
			}
			rv = temp.size()-1 - use_machine(temp);
			ans += rv/2;
			if(rv%2==0) {
				knownB.push_back(ptr-1);
			} else {
				knownA.push_back(ptr-1);
			}
		} else {
			for(int i=0; i<knownB.size(); i++) {
				if(ptr==N) break;
				temp.push_back(knownB[i]);
				if(ptr<N) {
					temp.push_back(ptr);
					ptr++;
				}
			}
			rv = use_machine(temp);
			ans += rv/2;
			if(rv%2==0) {
				knownB.push_back(ptr-1);
			} else {
				knownA.push_back(ptr-1);
			}
		}
	}
	return knownA.size()+ans;
}

int smallSol() {
	knownA.clear();
	knownB.clear();
	knownA.push_back(0);
	return Phase2(1);
}

int count_mushrooms(int n) {
	N = n;
	if(n<=320) return smallSol();
	else {
		Phase1();
		return Phase2(302);
	}
} 

Compilation message

mushrooms.cpp: In function 'int findQueryValue(std::vector<int>)':
mushrooms.cpp:16:16: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   16 |  for(int i=0; i<v.size(); i++) {
      |               ~^~~~~~~~~
mushrooms.cpp: In function 'void solve(int, int, std::vector<int>)':
mushrooms.cpp:46:16: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   46 |  for(int i=2; i<qAns.size(); i+=2) {
      |               ~^~~~~~~~~~~~
mushrooms.cpp: In function 'void initFirstPhase()':
mushrooms.cpp:73:17: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::vector<int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   73 |   for(int j=0; j<query[i-1].size()-1; j++) {
      |                ~^~~~~~~~~~~~~~~~~~~~
mushrooms.cpp:83:11: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   83 |     if(ptr<query[i-1][j].size() && query[i-1][j][ptr]==k) ptr++;
      |        ~~~^~~~~~~~~~~~~~~~~~~~~
mushrooms.cpp: In function 'void apply(int)':
mushrooms.cpp:129:16: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::vector<int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  129 |  for(int i=0; i<query[lv].size(); i++) {
      |               ~^~~~~~~~~~~~~~~~~
mushrooms.cpp: In function 'int Phase2(int)':
mushrooms.cpp:162:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  162 |    for(int i=0; i<knownA.size(); i++) {
      |                 ~^~~~~~~~~~~~~~
mushrooms.cpp:178:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  178 |    for(int i=0; i<knownB.size(); i++) {
      |                 ~^~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 292 KB Output is correct
2 Correct 1 ms 292 KB Output is correct
3 Correct 1 ms 296 KB Output is correct
4 Correct 1 ms 200 KB Output is correct
5 Correct 1 ms 200 KB Output is correct
6 Correct 3 ms 328 KB Output is correct
7 Correct 8 ms 328 KB Output is correct
8 Correct 7 ms 364 KB Output is correct
9 Correct 6 ms 368 KB Output is correct
10 Correct 7 ms 328 KB Output is correct
11 Correct 6 ms 328 KB Output is correct
12 Correct 6 ms 328 KB Output is correct
13 Correct 6 ms 328 KB Output is correct
14 Correct 4 ms 328 KB Output is correct
15 Correct 8 ms 328 KB Output is correct
16 Correct 6 ms 328 KB Output is correct
17 Correct 6 ms 328 KB Output is correct
18 Correct 6 ms 368 KB Output is correct
19 Correct 7 ms 392 KB Output is correct
20 Correct 7 ms 328 KB Output is correct
21 Correct 10 ms 328 KB Output is correct
22 Correct 9 ms 328 KB Output is correct
23 Correct 8 ms 376 KB Output is correct
24 Correct 4 ms 328 KB Output is correct
25 Correct 8 ms 328 KB Output is correct
26 Correct 8 ms 328 KB Output is correct
27 Correct 6 ms 368 KB Output is correct
28 Correct 9 ms 368 KB Output is correct
29 Correct 6 ms 372 KB Output is correct
30 Correct 7 ms 328 KB Output is correct
31 Correct 7 ms 368 KB Output is correct
32 Correct 6 ms 328 KB Output is correct
33 Correct 7 ms 328 KB Output is correct
34 Correct 6 ms 328 KB Output is correct
35 Correct 7 ms 328 KB Output is correct
36 Correct 9 ms 364 KB Output is correct
37 Correct 7 ms 328 KB Output is correct
38 Correct 6 ms 328 KB Output is correct
39 Correct 6 ms 328 KB Output is correct
40 Correct 6 ms 364 KB Output is correct
41 Correct 7 ms 328 KB Output is correct
42 Correct 6 ms 328 KB Output is correct
43 Correct 6 ms 368 KB Output is correct
44 Correct 9 ms 368 KB Output is correct
45 Correct 6 ms 364 KB Output is correct
46 Correct 6 ms 364 KB Output is correct
47 Correct 9 ms 328 KB Output is correct
48 Correct 7 ms 328 KB Output is correct
49 Correct 7 ms 328 KB Output is correct
50 Correct 7 ms 328 KB Output is correct
51 Correct 7 ms 328 KB Output is correct
52 Correct 7 ms 328 KB Output is correct
53 Correct 9 ms 328 KB Output is correct
54 Correct 7 ms 328 KB Output is correct
55 Correct 7 ms 368 KB Output is correct
56 Correct 6 ms 328 KB Output is correct
57 Correct 10 ms 364 KB Output is correct
58 Correct 6 ms 328 KB Output is correct
59 Correct 7 ms 460 KB Output is correct
60 Correct 7 ms 328 KB Output is correct
61 Correct 7 ms 328 KB Output is correct
62 Correct 0 ms 200 KB Output is correct
63 Correct 1 ms 200 KB Output is correct
64 Correct 0 ms 200 KB Output is correct
65 Correct 0 ms 200 KB Output is correct
66 Correct 0 ms 200 KB Output is correct
67 Correct 0 ms 200 KB Output is correct
68 Correct 1 ms 200 KB Output is correct
69 Correct 0 ms 200 KB Output is correct
70 Correct 1 ms 200 KB Output is correct
71 Correct 0 ms 200 KB Output is correct
72 Correct 1 ms 200 KB Output is correct
73 Correct 1 ms 200 KB Output is correct
74 Correct 1 ms 200 KB Output is correct
75 Correct 1 ms 200 KB Output is correct
76 Correct 1 ms 200 KB Output is correct
77 Correct 1 ms 200 KB Output is correct
78 Correct 0 ms 200 KB Output is correct
79 Correct 1 ms 200 KB Output is correct
80 Correct 0 ms 200 KB Output is correct
81 Correct 0 ms 200 KB Output is correct
82 Correct 1 ms 200 KB Output is correct
83 Correct 1 ms 200 KB Output is correct
84 Correct 1 ms 200 KB Output is correct
85 Correct 1 ms 200 KB Output is correct
86 Correct 0 ms 200 KB Output is correct
87 Correct 0 ms 200 KB Output is correct
88 Correct 1 ms 276 KB Output is correct
89 Correct 0 ms 200 KB Output is correct
90 Correct 1 ms 200 KB Output is correct
91 Correct 0 ms 200 KB Output is correct