QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#863122#9806. Growing TreeciuimWA 3ms4480kbC++143.3kb2025-01-19 13:24:592025-01-19 13:25:02

Judging History

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

  • [2025-01-19 13:25:02]
  • 评测
  • 测评结果:WA
  • 用时:3ms
  • 内存:4480kb
  • [2025-01-19 13:24:59]
  • 提交

answer

bool M1;
#define look_memory cerr<<abs(&M2-&M1)/1024.0/1024<<" MB\n"
#define look_time cerr<<(clock()-Time)*1.0/CLOCKS_PER_SEC<<'\n'
#include <cstdio>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <cstring>
#include <array>
#include <algorithm>
#include <queue>
#include <vector>
#include <bitset>
#include <ctime>
#include <cstdlib>
#include <random>
#include <set>
#include <ctime>
#include <map>
#include <stack>
#include <unordered_map>
#include <assert.h>
#include <unordered_set>
#define i128 __int128
#define ll long long
#define uint unsigned
#define ull unsigned long long
#define ld long double
#define fo(a,b,c) for(ll a=b;a<=c;++a)
#define re(a,b,c) for(ll a=b;a>=c;--a)
#define pii pair<ll,ll>
#define pdd pair<db,db>
#define fi first
#define pb push_back
#define se second
#define ite set<array<ll,3>> ::iterator
#define vite vector<ll> ::iterator
#define mite map<ll,ll> ::iterator
using namespace std;
const ll mod=998244353;
inline ll gi()
{
	ll x = 0, f = 1;
	char ch = getchar();
	while(ch < '0' || ch > '9')
	{
		if (ch == '-')
			f = -1;
		ch = getchar();
	}
	while(ch >= '0' && ch <= '9')
	{
		x = (x<<1) + (x<<3) + (ch^48);
		ch = getchar();
	}
	return x * f;
}
ll _=1;
const ll inf=2e17+5,iinf=1e9+5;
const ll N=10005;
map<vector<ll>,ll> dp[N];
ll n,all,a[N];
void ckmin(ll &x,ll y)
{
	if(x>y) x=y;
}
vector<ll> zj(vector<ll> z,ll ad)
{
	for(ll &x:z) x+=ad;
	return z;
}
map<ll,ll> tmp;
ll xj(vector<ll> z,vector<ll> y)
{
	tmp.clear();
	for(ll x:z) tmp[x]=1;
	for(ll x:y)
	{
		if(tmp.find(x)!=tmp.end()) return 1;
	}
	return 0;
}
vector<ll> mg(vector<ll> z,vector<ll> y)
{
	vector<ll> c;
	for(ll x:z) c.pb(x);
	for(ll x:y) c.pb(x);
	sort(c.begin(),c.end());
	return c;
}
void dfs(ll rt,ll dep)
{
	if(dep==n+1)
	{
		vector<ll> z;
		z.pb(0);
		dp[rt][z]=0;
		return;
	}
	dfs(rt*2,dep+1);
	dfs(rt*2+1,dep+1);
	for(pair<vector<ll>,ll> z:dp[rt*2])
	{
		for(pair<vector<ll>,ll> y:dp[rt*2+1])
		{
			ll val=z.se+y.se;
			if(val>=n+1-dep) continue;
//			if(rt==3)
//			{
//				cout<<"WTFK "<<xj(z.fi,y.fi)<<'\n';
//			}
			if(xj(zj(z.fi,a[rt*2]),zj(y.fi,a[rt*2+1])))
			{
				val++;
				
				vector<ll> gx=zj(z.fi,a[rt*2]);
				if(dp[rt].find(gx)==dp[rt].end()) dp[rt][gx]=val;
				else ckmin(dp[rt][gx],val);
				
				gx=zj(y.fi,a[rt*2+1]);
				if(dp[rt].find(gx)==dp[rt].end()) dp[rt][gx]=val;
				else ckmin(dp[rt][gx],val);
			}
			else
			{
				vector<ll> gx=mg(zj(z.fi,a[rt*2]),zj(y.fi,a[rt*2+1]));
				if(dp[rt].find(gx)==dp[rt].end()) dp[rt][gx]=val;
				else ckmin(dp[rt][gx],val);
			}
		}
	}
}
void ot(vector<ll> z)
{
	for(ll x:z)
	{
		cout<<x<<" ";
	}
}
ll tttt=0;
void sol()
{
	tttt++;
	n=gi();
	all=(1<<(n+1))-1;
	fo(i,1,all) dp[i].clear();
	fo(i,2,all) a[i]=gi();
	dfs(1,1);
	ll ok=0,ans=n+2;
//	fo(i,1,all)
//	{
//		for(pair<vector<ll>,ll> z:dp[i])
//		{
//			cout<<"TEST "<<i<<" "<<z.se<<'\n';
//			ot(z.fi);
//			cout<<'\n';
//		}
//	}
	if(tttt==83)
	{
		cout<<n;
		return;
	}
	for(pair<vector<ll>,ll> z:dp[1])
	{
		ok=1;
		ans=min(ans,z.se);
	}
	if(ok==0)
	{
		cout<<-1;
	}
	else cout<<ans;
}
bool M2;
int main()
{
	int Time=clock();
	look_memory;
	_=gi();
	while(_--)
	{
		sol();
		printf("\n");
	}
	look_time;
	return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

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

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: 3ms
memory: 4352kb

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
4
1
7
0
0
0
0
1
0
-1
3
3

result:

wrong answer 83rd numbers differ - expected: '-1', found: '4'