QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#790371 | #9806. Growing Tree | wangjunrui | WA | 1ms | 3740kb | C++14 | 1.5kb | 2024-11-28 11:15:53 | 2024-11-28 11:15:53 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
constexpr int N = (1 << 13) + 5;
typedef long double ld;
int n, a[N];
vector<int> g[N];
inline bool check(const vector<int> &a0, const vector<int> &a1)
{
for (int i = 0, j = 0; i < (int)a0.size(); ++i)
{
while (j < (int)a1.size() && a0[i] > a1[j])
++j;
if (a0[i] == a1[j])
return true;
}
return false;
}
inline int dfs(int dep, int use)
{
if (dep == 0)
return use;
vector<int> p;
for (int i = (1 << (dep - 1)); i < (1 << dep); ++i)
{
int lc = i << 1, rc = i << 1 | 1;
if (check(g[lc], g[rc]))
p.emplace_back(i);
else
{
g[i].resize(g[lc].size() + g[rc].size());
merge(g[lc].begin(), g[lc].end(), g[rc].begin(), g[rc].end(), g[i].begin());
}
}
use += (int)p.size();
if (use > n - dep + 1)
return INT_MAX;
int ans = INT_MAX;
for (int i = 0; i < (1 << p.size()); ++i)
{
for (int j = 0; j < (int)p.size(); ++j)
g[p[j]] = g[p[j] << 1 | ((i >> j) & 1)];
ans = min(ans, dfs(dep - 1, use));
}
return ans;
}
inline void _main()
{
cin >> n;
for (int i = 2; i < (1 << (n + 1)); ++i)
{
cin >> a[i];
a[i] += a[i >> 1];
}
for (int i = (1 << n); i < (1 << (n + 1)); ++i)
g[i].push_back(a[i]);
int res = dfs(n, 0);
cout << (res > n ? -1 : res) << '\n';
for (int i = 1; i < (1 << (n + 1)); ++i)
{
g[i].clear();
a[i] = 0;
}
}
signed main()
{
ios::sync_with_stdio(false);
cin.tie(0), cout.tie(0);
int test = 1;
cin >> test;
while (test--)
_main();
return 0;
}
詳細信息
Test #1:
score: 100
Accepted
time: 0ms
memory: 3740kb
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: 1ms
memory: 3712kb
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 1 -1 0 0 -1 -1 -1 -1 -1 4 -1 0 3 2 7 -1 -1 -1 1 2 4 0 0 2 -1 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:
wrong answer 23rd numbers differ - expected: '0', found: '1'