QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#810705 | #9806. Growing Tree | 0x3f | WA | 0ms | 3628kb | C++14 | 1.4kb | 2024-12-12 09:22:13 | 2024-12-12 09:22:14 |
Judging History
answer
#include <bits/stdc++.h>
#define ll long long
#define lson(u) (u<<1)
#define rson(u) ((u<<1)|1)
#pragma GCC optimize(4)
using namespace std;
const int N=2100;
int n,sz,ans,d[N];
vector<int> g[N];
void mergge(vector<int> &a,vector<int> &b,vector<int> &S) {
S.clear();
int lena=a.size(),lenb=b.size();
int i=0,j=0;
while(i<lena&&j<lenb) {
if(a[i]==b[j]) {
S.clear();
return ;
}
if(a[i]<b[j]) S.push_back(a[i++]);
if(a[i]>b[j]) S.push_back(b[j++]);
}
while(i<lena) S.push_back(a[i++]);
while(j<lenb) S.push_back(b[j++]);
return ;
}
void dfs(int p,int d) {
if(p==0) {ans=d;return ;}
mergge(g[lson(p)],g[rson(p)],g[p]);
if(g[p].empty()) {
if(++d==ans||d+__lg(p)>n) return ;
g[p]=g[lson(p)];
dfs(p-1,d);
if(d<ans) {
g[p]=g[rson(p)];
dfs(p-1,d);
}
} else dfs(p-1,d);
return ;
}
void solve() {
cin>>n;
sz=(1<<n);
for(int i=2;i<(sz<<1);i++) {
cin>>d[i];
d[i]+=d[i>>1];
if(i>=sz) {
g[i].clear();
g[i].push_back(d[i]);
}
}
ans=0x3f3f3f3f;
dfs(sz,0);
if(ans>n) cout<<"-1\n";
else cout<<ans<<"\n";
return ;
}
signed main() {
ios::sync_with_stdio(false);
cin.tie(0);cout.tie(0);
int tc;cin>>tc;
while(tc--) solve();
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 0
Wrong Answer
time: 0ms
memory: 3628kb
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 -1 -1
result:
wrong answer 1st numbers differ - expected: '1', found: '-1'