1446C - Xor Tree - CodeForces Solution


binary search bitmasks data structures divide and conquer dp trees *2100

Please click on ads to support us..

C++ Code:

#include <bits/stdc++.h>
using namespace std;
#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;

#define all(x) begin(x),end(x)

template<class T>
    using iset=tree<T,null_type,less<T>,rb_tree_tag,tree_order_statistics_node_update>;

void solve(){
    int n;
    cin>>n;
    vector<long long> a(n);
    for(auto&i:a){
	cin>>i;
    }
    sort(all(a));
    
    auto rec=[&](auto&&rec,int begi,int endi,long long TARGET)
    {
	if(endi-begi<=1)return 0;
	
	int pos=begi-1;
	for(int jump=__lg(endi-1-begi);jump>=0;jump--){
	    int posjump=pos+(1<<jump);
	    if(posjump<endi&&((a[posjump]&TARGET)==0))pos=posjump;
	}
	pos++;

	int removeRight=max(0,endi-pos-1);
	int removeLeft=max(0,pos-begi-1);
	if(TARGET==0){
	    return min(removeLeft,removeRight);
	}
	
	int solLeft=rec(rec,begi,pos,TARGET>>1)+removeRight;
	int solRight=rec(rec,pos,endi,TARGET>>1)+removeLeft;
	return min(solLeft,solRight);
    };

    int sol=rec(rec,0,n,1ll<<32);
    cout<<sol<<'\n';
}

int main(){
    ios::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);

    int numberOfTests=1;
    //cin>>numberOfTests;
    while(numberOfTests--){
	solve();
    }
}


Comments

Submit
0 Comments
More Questions

320A - Magic Numbers
1658A - Marin and Photoshoot
514A - Chewbaсca and Number
382A - Ksenia and Pan Scales
734B - Anton and Digits
1080A - Petya and Origami
1642D - Repetitions Decoding
1440A - Buy the String
1658F - Juju and Binary String
478A - Initial Bet
981A - Antipalindrome
365A - Good Number
1204B - Mislove Has Lost an Array
1409D - Decrease the Sum of Digits
1476E - Pattern Matching
1107A - Digits Sequence Dividing
1348A - Phoenix and Balance
1343B - Balanced Array
1186A - Vus the Cossack and a Contest
1494A - ABC String
1606A - AB Balance
1658C - Shinju and the Lost Permutation
1547C - Pair Programming
550A - Two Substrings
797B - Odd sum
1093A - Dice Rolling
1360B - Honest Coach
1399C - Boats Competition
1609C - Complex Market Analysis
1657E - Star MST