QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#809382 | #9806. Growing Tree | MiniLong | WA | 46ms | 3816kb | C++20 | 3.9kb | 2024-12-11 14:43:29 | 2024-12-11 14:43:30 |
Judging History
answer
#include <bits/stdc++.h>
#define _rep(i, x, y) for(int i = x; i <= y; ++i)
#define _req(i, x, y) for(int i = x; i >= y; --i)
#define _rev(i, u) for(int i = head[u]; i; i = e[i].nxt)
#define pb push_back
#define fi first
#define se second
#define mst(f, i) memset(f, i, sizeof f)
using namespace std;
#ifdef ONLINE_JUDGE
#define debug(...) 0
#else
#define debug(...) fprintf(stderr, __VA_ARGS__), fflush(stderr)
#endif
typedef long long ll;
typedef pair<int, int> PII;
namespace fastio{
#ifdef ONLINE_JUDGE
char ibuf[1 << 20],*p1 = ibuf, *p2 = ibuf;
#define get() p1 == p2 && (p2 = (p1 = ibuf) + fread(ibuf, 1, 1 << 20, stdin), p1 == p2) ? EOF : *p1++
#else
#define get() getchar()
#endif
template<typename T> inline void read(T &t){
T x = 0, f = 1;
char c = getchar();
while(!isdigit(c)){
if(c == '-') f = -f;
c = getchar();
}
while(isdigit(c)) x = x * 10 + c - '0', c = getchar();
t = x * f;
}
template<typename T, typename ... Args> inline void read(T &t, Args&... args){
read(t);
read(args...);
}
template<typename T> void write(T t){
if(t < 0) putchar('-'), t = -t;
if(t >= 10) write(t / 10);
putchar(t % 10 + '0');
}
template<typename T, typename ... Args> void write(T t, Args... args){
write(t), putchar(' '), write(args...);
}
template<typename T> void writeln(T t){
write(t);
puts("");
}
template<typename T> void writes(T t){
write(t), putchar(' ');
}
#undef get
};
using namespace fastio;
#define multitest() int T; read(T); _rep(tCase, 1, T)
namespace Calculation{
const ll mod = 998244353;
ll ksm(ll p, ll h){ll base = p % mod, res = 1; while(h){if(h & 1ll) res = res * base % mod; base = base * base % mod, h >>= 1ll;} return res;}
void dec(ll &x, ll y){x = ((x - y) % mod + mod) % mod;}
void add(ll &x, ll y){x = (x + y) % mod;}
void mul(ll &x, ll y){x = x * y % mod;}
ll sub(ll x, ll y){return ((x - y) % mod + mod) % mod;}
ll pls(ll x, ll y){return ((x + y) % mod + mod) % mod;}
ll mult(ll x, ll y){return x * y % mod;}
}
using namespace Calculation;
const int N = (1 << 11) + 5;
ll n, m, w[15], a[N], dis[N], dep[N];
bool vis[N];
int rt;
vector<ll> f[N];
void clr(){
_rep(i, 1, n) a[i] = dis[i] = dep[i] = vis[i] = 0, f[i].clear();
rt = n = m = 0;
}
vector<ll> merge(vector<ll> a, vector<ll> b){
auto lit = a.begin(), rit = b.begin();
vector<ll> c;
while(lit != a.end() || rit != b.end()){
if(rit == b.end() || (lit != a.end() && (*lit) <= (*rit))) c.pb(*lit), lit++;
else c.pb(*rit), rit++;
}
return c;
}
void dfs(int u){
if(dep[u] == m) return f[u] = {dis[u]}, void();
dis[u << 1] = dis[u] + a[u << 1], dis[u << 1 | 1] = dis[u] + a[u << 1 | 1];
if(!vis[u << 1]) dfs(u << 1);
if(!vis[u << 1 | 1]) dfs(u << 1 | 1);
f[u] = merge(f[u << 1], f[u << 1 | 1]);
int len = f[u].size();
bool flag = 0;
_rep(i, 0, len - 2) if(f[u][i] == f[u][i + 1]){flag = 1; break;}
if(flag && (!rt || dep[u] > dep[rt])) rt = u;
}
int ans;
void solve(int u){
if(u > m + 1) return;
rt = 0; _rep(i, 1, n) f[i].clear(), dis[i] = 0;
dfs(1);
if(!rt){
ans = min(ans, u - 1);
return;
}
int t = rt;
if(!w[dep[t]]) return;
w[dep[t]]--;
vis[t << 1] = 1;
solve(u + 1);
vis[t << 1] = 0, vis[t << 1 | 1] = 1;
solve(u + 1);
vis[t << 1 | 1] = 0;
w[dep[t]]++;
}
int main(){
multitest(){
read(m), n = (1 << m + 1) - 1;
_rep(i, 2, n) read(a[i]), dep[i] = dep[i >> 1] + 1;
_rep(i, 0, m) w[i] = m - i;
ans = 0721;
solve(1);
if(ans == 0721) puts("-1");
else writeln(ans);
clr();
}
return 0;
}
详细
Test #1:
score: 100
Accepted
time: 0ms
memory: 3816kb
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
Wrong Answer
time: 46ms
memory: 3704kb
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 ...
output:
2 0 -1 2 0 1 -1 0 0 3 0 0 0 1 2 1 0 2 0 1 0 -1 0 -1 0 0 -1 -1 -1 -1 -1 4 -1 0 3 2 7 -1 -1 -1 1 2 4 0 0 2 7 1 6 0 -1 2 -1 0 0 0 -1 1 -1 -1 0 0 1 1 7 0 1 2 0 -1 0 0 1 1 6 0 -1 0 0 0 -1 3 -1 1 7 0 0 0 0 1 0 -1 3 3
result:
wrong answer 65th numbers differ - expected: '-1', found: '7'