zoukankan      html  css  js  c++  java
  • CF1416C XOR Inverse(字典树+贪心)

    link

    
    #include<bits/stdc++.h>
    using namespace std;
    typedef long long ll;
    typedef unsigned long long ull;
    typedef pair<ll, ll>PLL;
    typedef pair<int, int>PII;
    typedef pair<double, double>PDD;
    #define I_int ll
    inline ll read()
    {
        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 * 10 + ch - '0';
            ch = getchar();
        }
        return x * f;
    }
    #define read read()
    #define closeSync ios::sync_with_stdio(0);cin.tie(0);cout.tie(0)
    #define multiCase int T;cin>>T;for(int t=1;t<=T;t++)
    #define rep(i,a,b) for(int i=(a);i<=(b);i++)
    #define repp(i,a,b) for(int i=(a);i<(b);i++)
    #define per(i,a,b) for(int i=(a);i>=(b);i--)
    #define perr(i,a,b) for(int i=(a);i>(b);i--)
    ll ksm(ll a, ll b, ll p)
    {
        ll res = 1;
        while(b)
        {
            if(b & 1)res = res * a % p;
            a = a * a % p;
            b >>= 1;
        }
        return res;
    }
    
    const int inf = 0x3f3f3f3f;
    #define PI acos(-1)
    const int maxn=3e5+100;
    const double eps=1e-7;
    const ll mod=987654321;
    
    ll n;
    int tr[maxn*40][2],idx;
    ll dep[40][2];
    vector<int>node[maxn*40];
    
    int gnew(){
    	idx++;
    	tr[idx][0]=tr[idx][1]=0;
    	node[idx].clear();
    	return idx;
    }
    
    void insert(ll x,int cur){
    	int p=0;
    	for(int i=30;i>=0;i--){
    		int now=(x>>i)&1;
    		if(!tr[p][now]) tr[p][now]=gnew();
    		p=tr[p][now];
    		node[p].push_back(cur);
    		
    	}
    }
    
    void dfs(int pos,int d){
    	if(d==-1) return ;
    	int l=tr[pos][0],r=tr[pos][1];
    	ll res=0;
    	int sizl=node[l].size(),sizr=node[r].size();
    	for(int i=0,j=0;i<sizl;i++){
    		while(j<sizr&&node[l][i]>node[r][j]) j++;
    		res+=j;
    	}
    	dep[d][0]+=res;
    	dep[d][1]+=1ll*sizl*sizr-res;
    	if(l) dfs(l,d-1);
    	if(r) dfs(r,d-1);
    }
    
    int main(){
    	idx=-1;
    	gnew();
    	n=read;
    	rep(i,1,n){
    		ll x=read;
    		insert(x,i);
    	}
    	dfs(0,30);
    	ll res=0,x=0;
    	rep(i,0,30){
    		if(dep[i][0]<=dep[i][1]){
    			res+=dep[i][0];///this is 0
    		}
    		else{
    			res+=dep[i][1];
    			x=x|(1<<i);
    		}
    	}
    	cout<<res<<" "<<x<<endl;
    	
    	
    	
    	
    	
    	
    	return 0;
    }
    
    
    
    
    
    
    
    
    
     
    
  • 相关阅读:
    系统测试的策略
    POJ1611(并查集)
    POJ2752(KMP)
    POJ3176(DP)
    HDU2579(BFS)
    HDOJ1175(BFS)
    HDOJ1242(BFS)
    HDOJ1180(BFS)
    HDOJ1372(BFS)
    HDOJ2717(BFS)
  • 原文地址:https://www.cnblogs.com/OvOq/p/15038842.html
Copyright © 2011-2022 走看看