QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#801132#9806. Growing Treeucup-team5008WA 19ms3644kbC++232.3kb2024-12-06 18:32:282024-12-06 18:32:30

Judging History

你现在查看的是最新测评结果

  • [2024-12-06 18:32:30]
  • 评测
  • 测评结果:WA
  • 用时:19ms
  • 内存:3644kb
  • [2024-12-06 18:32:28]
  • 提交

answer

#include<bits/stdc++.h>
#define rep2(i, s, n) for(ll i = ll(s); i < ll(n); i++)
#define rrep2(i, n, t) for(ll i = ll(n)-1; i >= ll(t); i--)
#define rep(i, n) rep2(i, 0, n)
#define rrep(i, n) rrep2(i, n, 0)
#define eb emplace_back
#define SZ(a) ll(a.size())
#define all(a) a.begin(),a.end()
using namespace std;
using ll = long long;
using P = pair<ll, ll>;
using vl = vector<ll>;
using vvl = vector<vl>;
using vp = vector<P>;
using vvp = vector<vp>;
bool chmin(auto &a, auto b) { return a > b ? a = b, true : false; }
bool chmax(auto &a, auto b) { return a < b ? a = b, true : false; }
const ll inf = LLONG_MAX / 4;

#define ln "\n"

void solve() {
	ll n; cin >> n;
	vl dist(1<<(n+1));
	vl a(1<<(n+1));
	rep2(i,2,1<<(n+1)) cin >> a[i];
	vl dep(1<<(n+1));
	rep2(i,1,(1<<(n+1))){
		ll now=i;
		while(now > 1){
			dist[i] += a[now];
			dep[i]++;
			now >>= 1;
		}
	}
	{
		vl data;
		rep2(i,1,(1<<(n+1))) data.eb(dist[i]);
		sort(all(data));
		data.erase(unique(all(data)),data.end());
		rep2(i,1,(1ll<<(n+1))){
			dist[i] = lower_bound(all(data), dist[i])-data.begin();
		}
	}
	vector<bool> remain(1<<(n+1),true);
	vl used;
	auto findNext = [&](){
		vl last((1<<(n+1)), -1);
		ll res = 0;
		rep2(i,(1<<n),(1<<(n+1))){
			ll now = i;
			bool flag = true;
			while(now > 1){
				if(!remain[now]) flag = false;
				now >>= 1;
			}
			if(!flag) continue;
			if(last[dist[i]] != -1){
				ll u = i, v = last[dist[i]];
				while(u != v){
					u >>= 1;
					v >>= 1;
				}
				chmax(res, u);
			}
			last[dist[i]] = i;
		}
		return res;
	};
	ll ans = inf;
	auto check = [&](){
		//for(auto el: used) cout << el << " ";
		//cout << endl;
		rep(i,SZ(used)){
			if(used[SZ(used)-i-1] > i) return false;
		}
		return true;
	};
	auto dfs = [&](auto& dfs){
		//for(auto el: used) cout << el << " ";
		//cout << endl;
		ll nex = findNext();
		if(nex == 0){
			if(check()) chmin(ans, SZ(used));
			return;
		}
		if(SZ(used) == n) return;
		used.eb(dep[nex]);
		remain[nex*2] = false;
		dfs(dfs);
		remain[nex*2] = true;
		remain[nex*2+1] = false;
		dfs(dfs);
		remain[nex*2+1] = true;
		used.pop_back();
		return;
	};
	dfs(dfs);
	if(ans == inf) ans = -1;
	cout << ans << ln;
}

int main() {
	cin.tie(0)->sync_with_stdio(0);
	ll t; cin >> t;
	while(t--) solve();
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 0ms
memory: 3644kb

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: 19ms
memory: 3644kb

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:

-1
0
-1
-1
0
1
-1
0
0
-1
0
0
0
-1
2
1
0
-1
0
2
0
-1
0
-1
0
0
-1
-1
-1
-1
-1
4
-1
0
-1
2
7
-1
-1
-1
2
2
5
0
0
3
7
-1
7
0
-1
-1
-1
0
0
0
-1
1
-1
-1
0
0
1
-1
-1
0
1
-1
0
-1
0
0
-1
-1
-1
0
-1
0
0
0
-1
-1
-1
1
7
0
0
0
0
1
0
-1
4
4

result:

wrong answer 1st numbers differ - expected: '2', found: '-1'