Submission #335020

# Submission time Handle Problem Language Result Execution time Memory
335020 2020-12-10T19:53:39 Z Plurm Library (JOI18_library) C++11
19 / 100
512 ms 668 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){
	bitset<1024> s;
	vector<int> M(N);
	for(int i = L; i <= R; i++){
		s[fn(i)] = true;
	}
	if(sp != -1) s[fn(sp)] = true;
	for(int i = 0; i < N; i++){
		if(s[fn(i)]) M[i] = 1;
		else M[i] = 0;
	}
	return Query(M);
}
int countRange(int L, int R, int sp = -1){
	bitset<1024> s;
	for(int i = L; i <= R; i++){
		s[fn(i)] = true;
	}
	if(sp != -1) s[fn(sp)] = true;
	return s.count();
}
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 = -1){
	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);
		if(l == -1 || r == -1) while(true);
		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);
	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);
}
# Verdict Execution time Memory Grader output
1 Correct 45 ms 404 KB # of queries: 3366
2 Correct 51 ms 364 KB # of queries: 3352
3 Correct 55 ms 364 KB # of queries: 3547
4 Correct 47 ms 492 KB # of queries: 3543
5 Correct 54 ms 364 KB # of queries: 3527
6 Correct 54 ms 364 KB # of queries: 3530
7 Correct 40 ms 404 KB # of queries: 3526
8 Correct 51 ms 492 KB # of queries: 3394
9 Correct 60 ms 492 KB # of queries: 3523
10 Correct 30 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 4 ms 492 KB # of queries: 307
# Verdict Execution time Memory Grader output
1 Correct 45 ms 404 KB # of queries: 3366
2 Correct 51 ms 364 KB # of queries: 3352
3 Correct 55 ms 364 KB # of queries: 3547
4 Correct 47 ms 492 KB # of queries: 3543
5 Correct 54 ms 364 KB # of queries: 3527
6 Correct 54 ms 364 KB # of queries: 3530
7 Correct 40 ms 404 KB # of queries: 3526
8 Correct 51 ms 492 KB # of queries: 3394
9 Correct 60 ms 492 KB # of queries: 3523
10 Correct 30 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 4 ms 492 KB # of queries: 307
17 Runtime error 512 ms 668 KB Execution killed with signal 13 (could be triggered by violating memory limits)
18 Halted 0 ms 0 KB -