답안 #335017

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
335017 2020-12-10T19:42:42 Z Plurm 도서관 (JOI18_library) C++11
19 / 100
2000 ms 492 KB
#include <bits/stdc++.h>
#include "library.h"
using namespace std;

int p[1024];
int fn(int x){
	if(p[x] == -1) return x;
	else return p[x] = fn(p[x]);
}
bool un(int x, int y){
	x = fn(x);
	y = fn(y);
	if(x == y) return false;
	p[x] = y;
	return true;
}
int queryRange(int L, int R, int N, int sp = -1){
	vector<int> M(N);
	set<int> s;
	for(int i = L; i <= R; i++){
		s.insert(fn(i));
	}
	if(sp != -1) s.insert(fn(sp));
	for(int i = 0; i < N; i++){
		if(s.count(fn(i))) M[i] = 1;
		else M[i] = 0;
	}
	return Query(M);
}
int countRange(int L, int R, int sp = -1){
	set<int> s;
	for(int i = L; i <= R; i++){
		s.insert(fn(i));
	}
	if(sp != -1) s.insert(fn(sp));
	return s.size();
}
int LeftTurn(int L, int N){
	// L -> x duplicates
	// L -> x-1 does not duplicate
	if(queryRange(L, N-1, N, L) == countRange(L, N-1, L)) return -1;
	int lo = L;
	int hi = N-1;
	int mid;
	while(lo < hi){
		mid = (lo + hi)/2;
		if(queryRange(L, mid, N, L) == countRange(L, mid, L)) lo = mid+1;
		else hi = mid;
	}
	return lo;
}
int RightTurn(int R, int N){
	// x <- R duplicates
	// x+1 <- R does not duplicate
	if(queryRange(0, R, N, R) == countRange(0, R, R)) return -1;
	int lo = 0;
	int hi = R;
	int mid;
	while(lo < hi){
		mid = (lo + hi + 1)/2;
		if(queryRange(mid, R, N, R) == countRange(mid, R, R)) hi = mid-1;
		else lo = mid;
	}
	return lo;
}
vector<int> g[1024];
void addEdge(int u, int v){
	g[u].push_back(v);
	g[v].push_back(u);
}
int findLeaf(int u, int p){
	for(int v : g[u]){
		if(v == p) continue;
		return findLeaf(v, u);
	}
	return u;
}
pair<int,int> findExtremum(int u){
	if(g[u].size() > 1){
		return make_pair(findLeaf(g[u][0], u), findLeaf(g[u][1], u));
	}else if(g[u].size() == 1){
		return make_pair(u, findLeaf(g[u][0], u));
	}else{
		return make_pair(u, u);
	}
}
bool checkPair(int l, int r, int N){
	vector<int> M(N);
	fill(M.begin(), M.end(), 0);
	M[l] = M[r] = 1;
	return Query(M) == 1;
}
void Solve(int N)
{
	memset(p, -1, sizeof(p));
	vector<int> M(N);
	for(int i = 1; i < N; i++){
		int l = LeftTurn(0, N);
		int r = RightTurn(l, N);
		int l1, l2;
		tie(l1, l2) = findExtremum(l);
		int r1, r2;
		tie(r1, r2) = findExtremum(r);
		if(checkPair(l1, r1, N)){
			l = l1;
			r = r1;
		}else if(checkPair(l1, r2, N)){
			l = l1;
			r = r2;
		}else if(checkPair(l2, r1, N)){
			l = l2;
			r = r1;
		}else{
			l = l2;
			r = r2;
		}
		un(l, r);
		addEdge(l, r);
	}
	int last = -1;
	int leaf = findLeaf(0, 0);
	vector<int> ans;
	for(int i = 0; i < N; i++){
		ans.push_back(leaf+1);
		int nxt = -1;
		for(int v: g[leaf]){
			if(v != last) nxt = v;
		}
		last = leaf;
		leaf = nxt;
	}
	Answer(ans);
}
# 결과 실행 시간 메모리 Grader output
1 Correct 82 ms 304 KB # of queries: 3366
2 Correct 87 ms 364 KB # of queries: 3352
3 Correct 99 ms 364 KB # of queries: 3547
4 Correct 100 ms 492 KB # of queries: 3543
5 Correct 102 ms 492 KB # of queries: 3527
6 Correct 84 ms 364 KB # of queries: 3530
7 Correct 88 ms 364 KB # of queries: 3526
8 Correct 81 ms 492 KB # of queries: 3394
9 Correct 87 ms 364 KB # of queries: 3523
10 Correct 43 ms 364 KB # of queries: 2117
11 Correct 0 ms 364 KB # of queries: 0
12 Correct 1 ms 364 KB # of queries: 5
13 Correct 1 ms 364 KB # of queries: 13
14 Correct 1 ms 364 KB # of queries: 22
15 Correct 3 ms 364 KB # of queries: 145
16 Correct 5 ms 364 KB # of queries: 307
# 결과 실행 시간 메모리 Grader output
1 Correct 82 ms 304 KB # of queries: 3366
2 Correct 87 ms 364 KB # of queries: 3352
3 Correct 99 ms 364 KB # of queries: 3547
4 Correct 100 ms 492 KB # of queries: 3543
5 Correct 102 ms 492 KB # of queries: 3527
6 Correct 84 ms 364 KB # of queries: 3530
7 Correct 88 ms 364 KB # of queries: 3526
8 Correct 81 ms 492 KB # of queries: 3394
9 Correct 87 ms 364 KB # of queries: 3523
10 Correct 43 ms 364 KB # of queries: 2117
11 Correct 0 ms 364 KB # of queries: 0
12 Correct 1 ms 364 KB # of queries: 5
13 Correct 1 ms 364 KB # of queries: 13
14 Correct 1 ms 364 KB # of queries: 22
15 Correct 3 ms 364 KB # of queries: 145
16 Correct 5 ms 364 KB # of queries: 307
17 Execution timed out 2021 ms 492 KB Time limit exceeded
18 Halted 0 ms 0 KB -