QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#811409 | #9806. Growing Tree | Suhy | RE | 3ms | 4132kb | C++17 | 3.9kb | 2024-12-12 19:08:46 | 2024-12-12 19:08:46 |
Judging History
answer
#include <bits/stdc++.h>
#define ll long long
#define ull unsigned long long
#define nd second
#define st first
#define lb(x) ((x) & (-(x)))
#define b(x) ((x) / sqn + 1)
#define ppc(x) (__builtin_popcountll(x))
#define brekp cout<<"OK"<<endl
#define GetMid int mid = (l + r) >> 1
#define ls in * 2
#define rs in * 2 + 1
#define updt(x, y) (x = max(x, y))
#define getb(x, y) (((x) >> (y)) & 1)
#define llen (mid - l + 1)
#define rlen (r - mid)
#define ld long double
#define uc unsigned char
#define pii pair <int, int>
#define pll pair <ll, ll>
#define set multiset
//#define int long long
#pragma GCC optimize(3)
using namespace std;
const int maxn = 1100;
const int maxm = 2200;
const int z = 'z' - 'a';
const int p = 998244353;
const int inf = 1000000;
const ull bas = 37;
const ull cas = 91;
const int lw = 200000;
const ld pi = 3.14159265358979323846264338327950288419716937937510;
int bs[2] = {37, 46};
ll read()
{
ll x = 0, fl = 0;
char c = 0;
while(c < '0' || '9' < c)
c = getchar(), fl |= (c == '-');
while('0' <= c && c <= '9')
x = x * 10 + c - '0', c = getchar();
return (fl ? -x : x);
}
int WriteSta[35], WriteTop;
void write(int x, char c)
{
do WriteSta[WriteTop ++] = x % 10, x /= 10;
while(x);
while(WriteTop) putchar(WriteSta[-- WriteTop] + '0');
putchar(c);
}
int pls(int x, int y)
{return (x + y >= p) ? (x + y - p) : (x + y);}
int mns(int x, int y)
{return (x >= y) ? (x - y) : (x + p - y);}
int powm(int x, int y)
{
if(y == 0) return 1;
int ne = powm(x, y / 2);
ne = 1ll * ne * ne % p;
if(y % 2) return 1ll * ne * x % p;
else return ne % p;
}
int gcd(int x, int y)
{
if(x < y) swap(x, y);
if(y == 0) return x;
return gcd(y, x % y);
}
int t;
int n, a[maxm], val[maxm], lg2[maxm], cnt, mdf[maxm], islca[maxn][2];
map <int, int> rnk;
bitset <maxn> bts[maxn][2];
signed main()
{
cin>>t;
while(t --)
{
cin>>n;
int tp = (1 << n + 1);
for(int i = 2; i < tp; i ++)
a[i] = read(), lg2[i] = log2(i);
for(int i = (1 << n); i < tp; i ++)
{
for(int in = i; in; in >>= 1)
val[i] += a[in];
if(!rnk[val[i]]) rnk[val[i]] = ++ cnt;
//cout<<i<<' '<<rnk[val[i]]<<endl;
}
for(int in = tp - 1; in >= 1; in --)
{
if(lg2[in] == n) bts[in][0][rnk[val[in]]] = 1;
else
{
bts[in][0] = bts[ls][0] | bts[rs][0];
islca[in][0] = (bts[ls][0] & bts[rs][0]).count();
}
}
int ans = n + 1;
for(int i = 0; i < (1 << n); i ++)
{
int cnt = 0, lst = 1;
memset(mdf, 0, sizeof mdf);
for(int j = (1 << n) - 1; j >= 1; j --)
{
if(islca[j][mdf[j]])
{
// cout<<i<<' '<<j<<endl;
lst --; cnt ++;
//cout<<i<<' '<<j<<' '<<lst<<endl;
if(lst < 0 || cnt > n)
{
cnt *= -1;
break;
}
mdf[j] = 1;
int son = j * 2 + ((i >> n - cnt) & 1);
bts[j][1] = bts[son][mdf[son]];
for(int in = (j >> 1); in; in >>= 1)
{
mdf[in] = 1;
bts[in][1] = bts[ls][mdf[ls]] | bts[rs][mdf[rs]];
islca[in][1] = (bts[ls][mdf[ls]] & bts[rs][mdf[rs]]).count();
}
}
if(ppc(j) == 1) lst ++;
}
//cout<<i<<' '<<cnt<<endl;
if(cnt >= 0) ans = min(ans, cnt);
if(abs(cnt) < n) i |= ((1 << (n - abs(cnt))) - 1);
}
printf("%d\n", ((ans > n) ? -1 : ans));
rnk.clear();
memset(islca, 0, sizeof islca);
cnt = 0;
memset(val, 0, sizeof val);
for(int i = 1; i < tp; i ++)
bts[i][0].reset();
}
}
詳細信息
Test #1:
score: 100
Accepted
time: 0ms
memory: 3972kb
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: 0
Accepted
time: 3ms
memory: 4132kb
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 -1 0 1 2 0 -1 0 0 1 1 -1 0 -1 0 0 0 -1 3 -1 1 7 0 0 0 0 1 0 -1 3 3
result:
ok 94 numbers
Test #3:
score: -100
Runtime Error
input:
1 10 100000000 100000000 100000000 100000000 100000000 100000000 100000000 100000000 100000000 100000000 100000000 100000000 100000000 100000000 100000000 100000000 100000000 100000000 100000000 100000000 100000000 100000000 100000000 100000000 100000000 100000000 100000000 100000000 100000000 10000...