uoj # 370 [UR # 17] Funny Funny fruit trees

topic

Chilean players really low brush does not move uoj

Consider first the structure of a tree apparently lie to you play, Bitwise and this thing is getting smaller and smaller, linked to the bottom of the chain is clearly not inferior to a certain point before hanging below, so we just ask for a arranged such that the minimum enough answers

Set \ (A = \ max (a_i) \) , the best answer can not be found to be inferior to repeatedly take a number \ (\ rm and \) answer, we have a \ (O (nA) \) violence, let \ (dp_i \) indicates the current \ (\ rm and \) and as \ (i \) , this \ (i \) becomes \ (0 \) minimum cost

But there may be the last \ (\ rm and \) and not a \ (0 \) , so we put all \ (a_i \) are common bits \ (k \) are seeking out in the every \ ( a_i \) to eliminate these digits, that is, and \ (k \) XOR bit, and finally the \ (\ rm and \) and we will be as \ (0 \) ; in answer plus \ (n \ times k \) on All right

In this case the transfer is very simple, we enumerate a \ (a_j \) , \ (dp_i = \ min (dp_ {I \ \ RM and \} a_j + I \ \ RM and \ a_j) \) to

Finally, the answer is \ (\ min (dp_ {a_i } + a_i) \)

Notice \ (i \ \ rm and \ a_j \) must be \ (i \) subset, consider enumerate \ (i \) subset \ (J \) , now only need to determine whether there is a \ ( a_k \) satisfies \ (i \ \ rm and \ \ a_k = j \)

Considered from the standpoint of the set, we can put the above that condition is split into \ (J \) is \ (a_k \) subset, and \ (a_k \) is \ (j \ bigoplus i \) in the corpus complement concentrated subset, we use the \ (\ rm fwt \) deal with what you can know whether there is a \ (a_k \) is \ (i \ bigoplus j \) subset of the full set up centralized, but there is no way to determine \ (j \) whether \ (a_k \) subset

But think about it we found no need to judge \ (j \) whether \ (a_k \) subset, just like transfer

Observation transfer type \ (dp_i = \ min (dp_j + J) \) , apparently \ (J \) as small as possible, such as through the presence of \ (a_k \) a \ (i \ bigoplus j \) complement repertoire concentrated subset, but \ (a_k \) is not \ (j \) subset, then there will be a smaller (i \) \ subset is the \ (a_k \) subset of the transfer of certain better

So complicated than \ (O (3 ^ {\ log A}) \)

#include<bits/stdc++.h>
#define re register
#define LL long long
#define min(a,b) ((a)<(b)?(a):(b))
#define max(a,b) ((a)>(b)?(a):(b))
inline int read() {
    char c=getchar();int x=0;while(c<'0'||c>'9') c=getchar();
    while(c>='0'&&c<='9') x=(x<<3)+(x<<1)+c-48,c=getchar();return x;
}
const int maxn=1e5+5;
int n,a[maxn],vis[1<<18+1],k,T,len;
LL dp[1<<18+1],ans;
int main() {
    n=read();
    for(re int i=1;i<=n;i++) a[i]=read(),T=max(T,a[i]);
    len=1;while(len<T) len<<=1;len--;
    for(re int i=1;i<=n;i++) vis[a[i]]=1;
    for(re int i=2;i<=len+1;i<<=1)
        for(re int ln=i>>1,l=0;l<len;l+=i)
            for(re int x=l;x<l+ln;++x) vis[x+ln]|=vis[x];
    k=a[1];for(re int i=2;i<=n;i++) k&=a[i];
    for(re int i=1;i<=n;i++) a[i]^=k;
    memset(dp,20,sizeof(dp));
    ans=dp[0];dp[0]=0;
    for(re int i=1;i<=T;i++) 
        for(re int j=i;j;j=(j-1)&i)
            if(vis[len^j]&&dp[i^j]+(i^j)<dp[i]) dp[i]=dp[i^j]+(i^j);
    for(re int i=1;i<=n;i++) 
        ans=min(ans,dp[a[i]]+a[i]);
    printf("%lld\n",1ll*n*k+ans);
    return 0;
}

Guess you like

Origin www.cnblogs.com/asuldb/p/11388166.html
Recommended