답안 #58196

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
58196 2018-07-17T06:26:39 Z 김세빈(#1689) 도서관 (JOI18_library) C++11
100 / 100
324 ms 700 KB
#include "library.h"

#include <bits/stdc++.h>

using namespace std;

vector <vector <int>> G;
int g;

void Solve(int N)
{
	int i, j, cnt, q;
	vector <int> ask(N);
	
	for(i=0;i<N;i++){
//		printf("i : %d\n", i + 1);
		
		for(j=0;j<=i;j++) ask[j] = 1;
		q = Query(ask);
		for(j=0;j<=i;j++) ask[j] = 0;
		
		if(q == g + 1){
			// new group
//			printf("new group\n");
			vector <int> v = {i};
			G.push_back(v);
			
			g ++;
		}
		else if(g == q){
			// endpoint
//			printf("endpoint\n");
			int k, b;
			k = 0;
			
			ask[i] = 1;
			for(b=0;(1<<b)<g;b++){
				cnt = 0;
				for(j=0;j<g;j++) if(j & (1 << b)){
					for(auto &t: G[j]) ask[t] = 1;
					cnt ++;
				}
				
				q = Query(ask);
				if(q == cnt) k |= 1 << b;
				
				for(j=0;j<g;j++) if(j & (1 << b)){
					for(auto &t: G[j]) ask[t] = 0;
				}
			}
			ask[i] = 0;
			
			for(auto &t: G[k]) ask[t] = 1;
			ask[G[k].back()] = 0;
			ask[i] = 1;
			
			q = Query(ask);
			if(q == 1) reverse(G[k].begin(), G[k].end());
			G[k].push_back(i);
			
			for(auto &t: G[k]) ask[t] = 0;
			ask[i] = 0;
		}
		else{
			// merge two groups
//			printf("merge two groups\n");
			int k1, k2, t1, t2, b, f, f1, f2, nf;
			k1 = k2 = f1 = f2 = nf = 0; f = -1;
			
			ask[i] = 1;
			for(b=0;(1<<b)<g;b++){
				cnt = 0;
				for(j=0;j<g;j++) if(j & (1 << b)){
					for(auto &t: G[j]) ask[t] = 1;
					cnt ++;
				}
				
				q = Query(ask);
				if(q == cnt - 1) k1 |= 1 << b, k2 |= 1 << b;
				else if(q == cnt){
					if(f == -1) k1 |= 1 << b, f = 0;
					else f |= 1 << b;
				}
				else nf |= 1 << b;
				
				for(j=0;j<g;j++) if(j & (1 << b)){
					for(auto &t: G[j]) ask[t] = 0;
				}
			}
			ask[i] = 0;
			
//			printf("%d %d %d\n", k1, k2, f);
			
			if(f != -1){
				ask[i] = 1;
				for(b=0;(1<<b)<g;b++) if(f & (1 << b)){
					cnt = 0;
					for(j=0;j<g;j++) if((j & k1) == k1 && (j & nf) == 0 && (j & (1 << b))){
						t1 = j; t2 = k2 | ((~j) & f);
//						printf("%d %d\n", t1, t2);
						if(t2 >= g) continue;
						for(auto &t: G[t1]) ask[t] = 1;
						for(auto &t: G[t2]) ask[t] = 1;
						cnt += 2;
					}
					
					q = Query(ask);
					if(q == cnt - 1) f1 |= 1 << b;
					else f2 |= 1 << b;
					
					for(j=0;j<g;j++) if((j & k1) == k1 && (j & nf) == 0 && (j & (1 << b))){
						t1 = j; t2 = k2 | ((~j) & f);
						if(t2 >= g) continue;
						for(auto &t: G[t1]) ask[t] = 0;
						for(auto &t: G[t2]) ask[t] = 0;
					}
				}
				ask[i] = 0;
			}
			
			k1 |= f1; k2 |= f2;
			if(k1 >= g || k2 >= g) while(1);
			
			for(auto &t: G[k1]) ask[t] = 1;
			ask[G[k1].back()] = 0;
			ask[i] = 1;
			
			q = Query(ask);
			if(q == 1) reverse(G[k1].begin(), G[k1].end());
			
			for(auto &t: G[k1]) ask[t] = 0;
			ask[i] = 0;
			
			for(auto &t: G[k2]) ask[t] = 1;
			ask[G[k2].back()] = 0;
			ask[i] = 1;
			
			q = Query(ask);
			if(q == 2) reverse(G[k2].begin(), G[k2].end());
			
			for(auto &t: G[k2]) ask[t] = 0;
			ask[i] = 0;
			
//			printf("%d %d\n", k1, k2);
			
			G[k1].push_back(i);
			for(auto &t: G[k2]) G[k1].push_back(t);
			G[k2].clear(); swap(G[k2], G.back());
			G.pop_back();
			
			g --;
		}
/*		
		for(auto &t1: G){
			for(auto &t2: t1) printf("%d ", t2 + 1);
			printf("\n");
		}
		
		printf("\n");
*/	}
	
	for(auto &t : G[0]) t ++;
	
	Answer(G[0]);
}
# 결과 실행 시간 메모리 Grader output
1 Correct 16 ms 296 KB Output is correct
2 Correct 37 ms 376 KB Output is correct
3 Correct 18 ms 412 KB Output is correct
4 Correct 33 ms 416 KB Output is correct
5 Correct 17 ms 416 KB Output is correct
6 Correct 23 ms 548 KB Output is correct
7 Correct 27 ms 548 KB Output is correct
8 Correct 34 ms 548 KB Output is correct
9 Correct 21 ms 548 KB Output is correct
10 Correct 13 ms 548 KB Output is correct
11 Correct 2 ms 548 KB Output is correct
12 Correct 2 ms 548 KB Output is correct
13 Correct 3 ms 548 KB Output is correct
14 Correct 2 ms 548 KB Output is correct
15 Correct 4 ms 548 KB Output is correct
16 Correct 6 ms 548 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 16 ms 296 KB Output is correct
2 Correct 37 ms 376 KB Output is correct
3 Correct 18 ms 412 KB Output is correct
4 Correct 33 ms 416 KB Output is correct
5 Correct 17 ms 416 KB Output is correct
6 Correct 23 ms 548 KB Output is correct
7 Correct 27 ms 548 KB Output is correct
8 Correct 34 ms 548 KB Output is correct
9 Correct 21 ms 548 KB Output is correct
10 Correct 13 ms 548 KB Output is correct
11 Correct 2 ms 548 KB Output is correct
12 Correct 2 ms 548 KB Output is correct
13 Correct 3 ms 548 KB Output is correct
14 Correct 2 ms 548 KB Output is correct
15 Correct 4 ms 548 KB Output is correct
16 Correct 6 ms 548 KB Output is correct
17 Correct 300 ms 592 KB Output is correct
18 Correct 324 ms 592 KB Output is correct
19 Correct 247 ms 592 KB Output is correct
20 Correct 303 ms 592 KB Output is correct
21 Correct 215 ms 592 KB Output is correct
22 Correct 295 ms 592 KB Output is correct
23 Correct 298 ms 592 KB Output is correct
24 Correct 70 ms 592 KB Output is correct
25 Correct 239 ms 592 KB Output is correct
26 Correct 268 ms 592 KB Output is correct
27 Correct 105 ms 700 KB Output is correct
28 Correct 68 ms 700 KB Output is correct
29 Correct 71 ms 700 KB Output is correct
30 Correct 47 ms 700 KB Output is correct