QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#797273 | #9806. Growing Tree | orz_z | TL | 0ms | 3556kb | C++14 | 4.0kb | 2024-12-02 20:04:08 | 2024-12-02 20:04:12 |
Judging History
answer
//#pragma GCC optimize("Ofast,no-stack-protector")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx")
//#pragma GCC optimize("Ofast,fast-math")
//#pragma GCC target("avx,avx2")
//#pragma GCC optimize(2)
//#pragma GCC optimize(3)
//#pragma GCC optimize("Ofast")
#include <bits/stdc++.h>
using namespace std;
//#define int long long
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef double db;
#define F(i, a, b) for(int i = a; i <= (b); ++i)
#define F2(i, a, b) for(int i = a; i < (b); ++i)
#define dF(i, a, b) for(int i = a; i >= (b); --i)
template<typename T> void debug(string s, T x) {
cerr << "[" << s << "] = [" << x << "]\n";
}
template<typename T, typename... Args> void debug(string s, T x, Args... args) {
for (int i = 0, b = 0; i < (int)s.size(); i++) if (s[i] == '(' || s[i] == '{') b++;
else if (s[i] == ')' || s[i] == '}') b--;
else if (s[i] == ',' && b == 0) {
cerr << "[" << s.substr(0, i) << "] = [" << x << "] | ";
debug(s.substr(s.find_first_not_of(' ', i + 1)), args...);
break;
}
}
#ifdef orzz
#define Debug(...) debug(#__VA_ARGS__, __VA_ARGS__)
#else
#define Debug(...)
#endif
#define pb push_back
#define fi first
#define se second
#define Mry fprintf(stderr, "%.3lf MB\n", (&Med - &Mbe) / 1048576.0)
#define Try cerr << 1e3 * clock() / CLOCKS_PER_SEC << " ms\n";
typedef long long ll;
// namespace Fread {const int SIZE = 1 << 17; char buf[SIZE], *S, *T; inline char getchar() {if (S == T) {T = (S = buf) + fread(buf, 1, SIZE, stdin); if (S == T) return '\n';} return *S++;}}
// namespace Fwrite {const int SIZE = 1 << 17; char buf[SIZE], *S = buf, *T = buf + SIZE; inline void flush() {fwrite(buf, 1, S - buf, stdout), S = buf;} inline void putchar(char c) {*S++ = c;if (S == T) flush();} struct NTR {~NTR() {flush();}} ztr;}
// #ifdef ONLINE_JUDGE
// #define getchar Fread::getchar
// #define putchar Fwrite::putchar
// #endif
inline int ri() {
int x = 0;
bool t = 0;
char c = getchar();
while (c < '0' || c > '9') t |= c == '-', c = getchar();
while (c >= '0' && c <= '9') x = (x << 3) + (x << 1) + (c ^ 48), c = getchar();
return t ? -x : x;
}
inline void wi(int x) {
if (x < 0) {
putchar('-'), x = -x;
}
if (x > 9) wi(x / 10);
putchar(x % 10 + 48);
}
inline void wi(int x, char s) {
wi(x), putchar(s);
}
bool Mbe;
// mt19937 rnd(chrono::steady_clock::now().time_since_epoch().count());
const int mod = 998244353;
const int inf = 0x3f3f3f3f;
const ll infll = 0x3f3f3f3f3f3f3f3f;
const int _ = 2e5 + 5;
int n, a[_], W;
bool fg[_];
int find_edge() {
dF(i, n - 1, 0) {
// [2^i,2^{i+1}-1]
F(j, (1 << i), (1 << (i + 1)) - 1) if(!fg[j << 1] && !fg[j << 1 | 1] ){
unordered_map<int, int> mp;
function<void(int, int)> dfs = [&](int u, int nw) -> void {
Debug("1", j, u, nw);
if(u >= W && u <= W + W - 1) {
mp[nw]++;
return;
}
if(fg[u]) return;
dfs(u << 1, nw + a[u << 1]), dfs(u << 1 | 1, nw + a[u << 1 | 1]);
};
bool flg = 0;
function<void(int, int)> dfs2 = [&](int u, int nw) -> void {
Debug(j, u, nw);
if(u >= W && u <= W + W - 1) {
if(mp.find(nw) != mp.end()) {
flg = 1;
}
return;
}
if(fg[u]) return;
dfs2(u << 1, nw + a[u << 1]), dfs2(u << 1 | 1, nw + a[u << 1 | 1]);
};
dfs(j << 1, a[j << 1]), dfs2(j << 1 | 1, a[j << 1 | 1]);
if(flg) return j;
}
}
return 0;
}
int ans, tk[20];
void dfs(int u) {
int t = find_edge();
Debug(u, t);
if(!t) {
bool flg = 1;
int sm = 0;
dF(j, n, 1) {
sm += tk[j];
flg &= (sm <= n - j + 1);
}
if(flg) ans = min(ans, u - 1);
return;
}
fg[t << 1] = 1;
tk[__lg(t) + 1]++;
dfs(u + 1);
fg[t << 1] = 0;
fg[t << 1 | 1] = 1;
dfs(u + 1);
tk[__lg(t) + 1]--;
fg[t << 1 | 1] = 0;
}
void solve() {
n = ri();
W = (1 << n);
F(i, 2, W + W - 1) a[i] = ri();
ans = inf;
dfs(1);
if(ans == inf) puts("-1");
else cout << ans << '\n';
}
bool Med;
signed main() {
// Mry;
int T = ri();
while(T--) {
solve();
}
// Try;
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 3556kb
input:
3 2 1 2 4 3 2 1 2 1 2 3 3 2 1 2 1 2 3 3 1 1
output:
1 2 -1
result:
ok 3 number(s): "1 2 -1"
Test #2:
score: -100
Time Limit Exceeded
input:
94 5 44 65 38 61 64 94 71 53 65 10 24 36 98 74 11 4 5 46 72 34 9 24 37 32 76 29 48 88 17 14 36 4 22 6 71 53 24 61 89 79 39 57 99 61 27 85 99 46 81 75 90 25 16 13 1 87 55 81 56 78 67 2 3 83 3 74 14 45 17 22 41 62 74 25 1 56 22 7 21 73 83 99 3 91 16 53 8 10 49 29 54 81 45 10 12 68 32 9 30 11 99 85 73 ...