Submission #41979922


Source Code Expand

#include<bits/stdc++.h>
#define rep(i,a,b) for(int i=(a);i<=(b);i++)
#define per(i,a,b) for(int i=(a);i>=(b);i--)
#define op(x) ((x&1)?x+1:x-1)
#define odd(x) (x&1)
#define even(x) (!odd(x))
#define lc(x) (x<<1)
#define rc(x) (lc(x)|1)
#define lowbit(x) (x&-x)
#define mp(x,y) make_pair(x,y)
typedef long long ll;
typedef unsigned long long ull;
typedef double db;
using namespace std;
#define int ll

const int MAXN = 1e5+10,MAXM = 6e6+10;

int n,a[MAXN*2];

int ch[MAXM][2],cnt[MAXM],tot;

void ins(int u,int val,int k){
	cnt[u]++;
	if(k==-1)return;

	int v = val>>k&1;
	if(!ch[u][v])ch[u][v] = ++tot;
	ins(ch[u][v],val,k-1);
}

int lim;

int dp(int,int);

int V(int x,int y,int k){
	if(!x && !y)return 0;
	if(cnt[x] > cnt[y])swap(x,y);

	int num = dp(y,k-1);
	return cnt[x] + min(num,(cnt[y]-cnt[x])/2);
}

int g(int x,int y,int k){
	if(!x || !y)return 0;
	if(k==-1){
		return min(cnt[x],cnt[y]);
	}
	if(lim>>k&1){
		return g(ch[x][0],ch[y][1],k-1) + g(ch[x][1],ch[y][0],k-1);
	}else{
		int a = ch[x][0],b = ch[x][1],c = ch[y][0],d = ch[y][1];
		int ret = min(cnt[a],cnt[d]) + min(cnt[b],cnt[c]);
		
		if(cnt[a]>cnt[d] && cnt[c]>cnt[b]){
			int num = g(a,c,k-1);
			num = min(num,min(cnt[a]-cnt[d],cnt[c]-cnt[b]));
			ret += num;
		}
		if(cnt[a]<cnt[d] && cnt[c]<cnt[b]){
			int num = g(b,d,k-1);
			num = min(num,min(cnt[d]-cnt[a],cnt[b]-cnt[c]));
			ret += num;
		}
		
		return ret;
	}
}

int dp(int u,int k){
	if(!u)return 0;

	if(k==-1){
		return cnt[u]/2;
	}

	if(lim>>k&1){ 
		return g(ch[u][0],ch[u][1],k-1);
	}else{ 
		return V(ch[u][0],ch[u][1],k);
	}
}

const int VV = 29;

int chk(int mid){
	lim = mid;

	int ret = dp(1,VV);

	return ret >= (n+1)/2;
}

signed main(){
	cin>>n;

	tot = 1;
	rep(i,1,2*n)cin>>a[i],ins(1,a[i],VV);

	int L = 1,R = (1<<30)-1,ret = 0;

	while(L<=R){
		int mid = (L+R)>>1;

		if(chk(mid)){
			ret = mid; L = mid+1;
		}else{
			R = mid-1;
		}
	}
	cout<<ret<<"\n";

    return 0;
}

Submission Info

Submission Time
Task G - Max of Medians
User Crying
Language C++ (GCC 9.2.1)
Score 625
Code Size 2032 Byte
Status AC
Exec Time 433 ms
Memory 68452 KB

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 625 / 625
Status
AC × 3
AC × 56
Set Name Test Cases
Sample sample00.txt, sample01.txt, sample02.txt
All sample00.txt, sample01.txt, sample02.txt, testcase00.txt, testcase01.txt, testcase02.txt, testcase03.txt, testcase04.txt, testcase05.txt, testcase06.txt, testcase07.txt, testcase08.txt, testcase09.txt, testcase10.txt, testcase11.txt, testcase12.txt, testcase13.txt, testcase14.txt, testcase15.txt, testcase16.txt, testcase17.txt, testcase18.txt, testcase19.txt, testcase20.txt, testcase21.txt, testcase22.txt, testcase23.txt, testcase24.txt, testcase25.txt, testcase26.txt, testcase27.txt, testcase28.txt, testcase29.txt, testcase30.txt, testcase31.txt, testcase32.txt, testcase33.txt, testcase34.txt, testcase35.txt, testcase36.txt, testcase37.txt, testcase38.txt, testcase39.txt, testcase40.txt, testcase41.txt, testcase42.txt, testcase43.txt, testcase44.txt, testcase45.txt, testcase46.txt, testcase47.txt, testcase48.txt, testcase49.txt, testcase50.txt, testcase51.txt, testcase52.txt
Case Name Status Exec Time Memory
sample00.txt AC 3 ms 3544 KB
sample01.txt AC 2 ms 3400 KB
sample02.txt AC 2 ms 3508 KB
testcase00.txt AC 2 ms 3580 KB
testcase01.txt AC 2 ms 3452 KB
testcase02.txt AC 2 ms 3432 KB
testcase03.txt AC 79 ms 4964 KB
testcase04.txt AC 72 ms 5076 KB
testcase05.txt AC 325 ms 68248 KB
testcase06.txt AC 365 ms 68440 KB
testcase07.txt AC 433 ms 68376 KB
testcase08.txt AC 323 ms 68364 KB
testcase09.txt AC 367 ms 68424 KB
testcase10.txt AC 423 ms 68268 KB
testcase11.txt AC 61 ms 5012 KB
testcase12.txt AC 72 ms 5128 KB
testcase13.txt AC 92 ms 28184 KB
testcase14.txt AC 99 ms 30480 KB
testcase15.txt AC 94 ms 29176 KB
testcase16.txt AC 95 ms 30456 KB
testcase17.txt AC 91 ms 28416 KB
testcase18.txt AC 98 ms 30644 KB
testcase19.txt AC 96 ms 29900 KB
testcase20.txt AC 100 ms 30464 KB
testcase21.txt AC 87 ms 28904 KB
testcase22.txt AC 98 ms 30564 KB
testcase23.txt AC 116 ms 30552 KB
testcase24.txt AC 121 ms 30676 KB
testcase25.txt AC 111 ms 28772 KB
testcase26.txt AC 114 ms 30616 KB
testcase27.txt AC 119 ms 30352 KB
testcase28.txt AC 127 ms 35120 KB
testcase29.txt AC 126 ms 32328 KB
testcase30.txt AC 116 ms 33468 KB
testcase31.txt AC 118 ms 32540 KB
testcase32.txt AC 119 ms 31512 KB
testcase33.txt AC 42 ms 4856 KB
testcase34.txt AC 38 ms 5132 KB
testcase35.txt AC 45 ms 5056 KB
testcase36.txt AC 52 ms 5156 KB
testcase37.txt AC 47 ms 4968 KB
testcase38.txt AC 44 ms 5128 KB
testcase39.txt AC 68 ms 6660 KB
testcase40.txt AC 63 ms 6648 KB
testcase41.txt AC 62 ms 6556 KB
testcase42.txt AC 64 ms 6616 KB
testcase43.txt AC 59 ms 6508 KB
testcase44.txt AC 69 ms 6488 KB
testcase45.txt AC 225 ms 66160 KB
testcase46.txt AC 234 ms 68452 KB
testcase47.txt AC 216 ms 64948 KB
testcase48.txt AC 229 ms 68424 KB
testcase49.txt AC 240 ms 64460 KB
testcase50.txt AC 266 ms 68260 KB
testcase51.txt AC 230 ms 62988 KB
testcase52.txt AC 241 ms 68240 KB