QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#795969#9806. Growing Treeucup-team191#WA 1ms4184kbC++233.7kb2024-12-01 05:36:552024-12-01 05:36:55

Judging History

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

  • [2024-12-01 05:36:55]
  • 评测
  • 测评结果:WA
  • 用时:1ms
  • 内存:4184kb
  • [2024-12-01 05:36:55]
  • 提交

answer

#include <cstdio>
#include <vector>
#include <algorithm>

#define PB push_back

using namespace std;

typedef vector < int > vi;

const int OFF = (1 << 10) + 1;
const int N = 50;

int n, T[2 * OFF], pos[2 * OFF], list[2 * OFF];
vector < int > v[2 * OFF];

bool inter(vi &A, vi &B) {
	int i = 0, j = 0;
	while(i < (int)A.size() && j < (int)B.size()) {
		if(A[i] == B[j]) return true;
		else if(A[i] < B[j]) i++;
		else j++;
	}
	return false;
}

int ban[N], cur_sub[2 * OFF], list_ind[2 * OFF];
int rezo[2 * OFF], off;
vector < int > makni;
int kolko_rezo, ans;

void brute(int ind) {
	if(ind == (int)makni.size()) {
		ans = min(ans, kolko_rezo);
		return;
	}
	int x = makni[ind];
	//printf("ind = %d x = %d kolko_rezo = %d\n", ind, x, kolko_rezo);
	int lijevo = cur_sub[2 * x];
	int desno = cur_sub[2 * x + 1];
	bool dobar = true;
	//printf("L = %d R = %d\n", lijevo, desno);
	for(;lijevo; lijevo -= lijevo & -lijevo) {
		if(ban[__lg(lijevo & -lijevo)] & desno) {
			dobar = false; break;
		}
	}
	//printf("dobar = %d ban %d %d\n", dobar, ban[0], ban[1]);
	if(dobar) {
		brute(ind + 1);
	} else if(kolko_rezo < n - __lg(x)){
		int tata = x;
		while(tata > 1 && !rezo[tata]) tata >>= 1;
		kolko_rezo++;
		rezo[2 * x] = 1;
		for(int y = x;y >= tata;y >>= 1)
			cur_sub[y] ^= cur_sub[2 * x];
		brute(ind + 1);
		rezo[2 * x] = 0;
		rezo[2 * x + 1] = 1;
		for(int y = x;y >= tata;y >>= 1)
			cur_sub[y] ^= cur_sub[2 * x] ^ cur_sub[2 * x + 1];
		brute(ind + 1);
		for(int y = x;y >= tata;y >>= 1)
			cur_sub[y] ^= cur_sub[2 * x + 1];
		rezo[2 * x + 1] = 0;
		kolko_rezo--;
	}
}

void ocisti() {
	
	
	for(int i = 0;i <= 2 * off;i++) {
		v[i].clear(); 
		cur_sub[i] = 0; list[i] = 0; pos[i] = 0;
		list_ind[i] = 0; rezo[i] = 0; T[i] = 0;
	}
	for(int i = 0;i < N;i++) {
		ban[i] = 0;
	}
	
	makni.clear();
	
}

void solve() {
	scanf("%d", &n);
	off = (1 << n);
	T[1] = 0;
	for(int i = 2;i < 2 * off;i++) {
		scanf("%d", T + i);
	}
	for(int i = 2;i < off;i++) T[2 * i] += T[i], T[2 * i + 1] += T[i];
	for(int i = off;i < 2 * off;i++) {
		v[i].PB(T[i]);
	}
	for(int i = off - 1;i >= 1;i--) {
		for(int x : v[2 * i]) v[i].PB(x);
		for(int x : v[2 * i + 1]) v[i].PB(x);
		sort(v[i].begin(), v[i].end());
		v[i].erase(unique(v[i].begin(), v[i].end()), v[i].end());
		v[i].shrink_to_fit();
		pos[i] = (int)v[i].size() < (1 << (n - __lg(i)));
	}
	//printf("kuku %d\n", (int)v[1].size());
	if(!pos[1]) {
		printf("0\n");
		ocisti();
		return;
	}
	vector < int > svi;
	int list_cnt = 0;
	for(int i = 1;i < 2 * off;i++) {
		//printf("pos[ %d ] = %d\n", i, pos[i]);
		if(i == 1 || pos[i >> 1]) {
			if(!pos[i]) {
				//printf("list %d %d\n", i, list_cnt);
				list[i] = 1, list_ind[i] = list_cnt++;
			}
			else makni.PB(i);
			svi.PB(i);
		}
	}
	reverse(makni.begin(), makni.end());
	//printf("list_cnt = %d\n", list_cnt);
	if((int)svi.size() > 4 * n) {
		printf("-1\n");
		ocisti();
		return;
	}
	int m = (int)svi.size();
	for(int i = 0;i < m;i++) {
		if(!list[svi[i]]) continue;
		for(int j = i + 1;j < m;j++) {
			if(!list[svi[j]]) continue;
			if(inter(v[svi[i]], v[svi[j]])) {
				//printf("banana %d %d\n", list_ind[svi[i]], list_ind[svi[j]]);
				ban[list_ind[svi[i]]] |= (1 << list_ind[svi[j]]);
				ban[list_ind[svi[j]]] |= (1 << list_ind[svi[i]]);
			}
		}
	}
	
	
	for(int i = 2 * off - 1;i >= 1;i--) {
		if(list[i]) {
			cur_sub[i] = 1 << list_ind[i];
		} else if(pos[i]) {
			cur_sub[i] = cur_sub[2 * i] | cur_sub[2 * i + 1];
		}
	}
	
	ans = 100;
	kolko_rezo = 0;
	brute(0);
	
	if(ans == 100) {
		printf("-1\n");
	} else {
		printf("%d\n", ans);
	}
	
	ocisti();
}

int main() {
	int T; scanf("%d", &T);
	for(;T--;) {
		solve();
	}
	return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 1ms
memory: 4120kb

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: 0ms
memory: 4184kb

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

result:

wrong answer 85th numbers differ - expected: '7', found: '-1'