답안 #577694

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
577694 2022-06-15T08:09:40 Z GioChkhaidze Poklon (COCI17_poklon7) C++14
96 / 120
311 ms 238592 KB
// Source: https://usaco.guide/general/io

#include <bits/stdc++.h>
#define f first
#define s second
using namespace std;

const int N = 1e6 + 6;

int n, lf[N], rf[N], c[N];

pair < int , pair < int , int > > dfs(int x) {
	if (x > n) {
		int sz = (int)log2(c[x]) + 1;
		return {c[x], {sz, sz}};
	}
	pair < int ,  pair < int , int > > Lf = dfs(lf[x]);
	pair < int ,  pair < int , int > > Rf = dfs(rf[x]);
	++Lf.s.f, ++Rf.s.f;
	if (Lf.s.f > Rf.s.f) return Lf;
	if (Lf.s.f < Rf.s.f) return Rf;
	if (Lf.s.s < Rf.s.s) {
		if (Lf.f * (1 << (Rf.s.s - Lf.s.s)) > Rf.f) 
			return Lf;
		return Rf;
	}
	if (Lf.s.s > Rf.s.s) {
		if (Rf.f * (1 << (Lf.s.s - Rf.s.s)) > Lf.f) 
			return Rf;
		return Lf;
	}
	if (Lf.f > Rf.f) return Lf;
	return Rf;
}

int main() {
	ios::sync_with_stdio(false);
	cin.tie(NULL), cout.tie(NULL);
	cin >> n;
	int ts = n;
	for (int i = 1; i <= n; ++i) {
		cin >> lf[i] >> rf[i];
		if (lf[i] < 0) {
			++ts;
			c[ts] = -lf[i];
			lf[i] = ts;
		}
		if (rf[i] < 0) {
			++ts;
			c[ts] = -rf[i];
			rf[i] = ts;
		}
	}
	pair < int ,  pair < int , int > > ans = dfs(1);
	for (int i = ans.s.s - 1; i >= 0; --i) {
		cout << ((ans.f >> i) & 1);
	}
	for (int i = 0; i < ans.s.f - ans.s.s; ++i) {
		cout << 0;
	} 
}
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 340 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 0 ms 340 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 0 ms 340 KB Output is correct
7 Correct 0 ms 340 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 3 ms 980 KB Output is correct
12 Correct 4 ms 852 KB Output is correct
13 Correct 15 ms 3612 KB Output is correct
14 Correct 26 ms 7232 KB Output is correct
15 Correct 23 ms 1544 KB Output is correct
16 Correct 89 ms 18008 KB Output is correct
17 Runtime error 162 ms 14676 KB Execution killed with signal 11
18 Runtime error 158 ms 14740 KB Execution killed with signal 11
19 Runtime error 209 ms 16224 KB Execution killed with signal 11
20 Runtime error 311 ms 238592 KB Execution killed with signal 11