[CF1257F]Make Them Similar

题目

传送门

题解

考虑先将每一位上单独异或一个 \(1\) 所有数会使 \(a[i]\) 发生什么变化,可以这样预处理出来:

inline void Getcnt(){
    rep(i,0,MAXK){
        rep(j,1,n)if((a[j]^(1<<i))<a[j])cnt[i][j]=-1;
        else cnt[i][j]=1;
    }
}

并且,我们可以得到 \(a[i]\)\(1\) 的个数,这样:

inline void Getbitcnt(){
    rep(i,1,n){
        rep(j,0,MAXK)if((a[i]>>j)&1)
            ++bitcnt[i];
    }
}

而我们的目标就是,尝试将一些 \(cnt[i]\) 影响在 \(a\) 数组上,最后使得每一个 \(a[j]\) 都一样

如果我们直接暴力,时间复杂度无疑是 \(\mathcal O(2^{30}n)\) 会超时,想到一个优化——折半搜索

尝试以 \([0,14]\)\([15,29]\) 分开组合 \(cnt[i]\),最后我们枚举答案中所有数的 \(1\) 的个数为 \(k\),再枚举第一个暴力部分的组合,在第二个部分中用 map 或者其他数据结构找到是否存在这样的组合即可

可以有一个小优化,省掉 \(30\) 的常数:

由于我们最后的数的 \(1\) 个数相同,那么我们可以维护差分数组,那么最后的差分数组一定全为 \(0\),然后用相同方法去找即可

时间复杂度 \(\mathcal O(2^{15}\times 2+2^{15}\times 15)\)

代码

#include<cstdio>
#include<cstring>
#include<map>
using namespace std;

#define rep(i,__l,__r) for(signed i=(__l),i##_end_=(__r);i<=i##_end_;++i)
#define fep(i,__l,__r) for(signed i=(__l),i##_end_=(__r);i>=i##_end_;--i)
#define erep(i,u) for(signed i=tail[u],v=e[i].to;i;i=e[i].nxt,v=e[i].to)
#define writc(a,b) fwrit(a),putchar(b)
#define mp(a,b) make_pair(a,b)
#define ft first
#define sd second
typedef long long LL;
// typedef pair<int,int> pii;
typedef unsigned long long ull;
typedef unsigned uint;
#define Endl putchar('\n')
// #define int long long
// #define int unsigned
// #define int unsigned long long

#define cg (c=getchar())
template<class T>inline void read(T& x){
    char c;bool f=0;
    while(cg<'0'||'9'<c)f|=(c=='-');
    for(x=(c^48);'0'<=cg&&c<='9';x=(x<<1)+(x<<3)+(c^48));
    if(f)x=-x;
}
template<class T>inline T read(const T sample){
    T x=0;char c;bool f=0;
    while(cg<'0'||'9'<c)f|=(c=='-');
    for(x=(c^48);'0'<=cg&&c<='9';x=(x<<1)+(x<<3)+(c^48));
    return f?-x:x;
}
template<class T>void fwrit(const T x){//just short,int and long long
    if(x<0)return (void)(putchar('-'),fwrit(-x));
    if(x>9)fwrit(x/10);
    putchar(x%10^48);
}
template<class T>inline T Max(const T x,const T y){return x>y?x:y;}
template<class T>inline T Min(const T x,const T y){return x<y?x:y;}
template<class T>inline T fab(const T x){return x>0?x:-x;}
inline int gcd(const int a,const int b){return b?gcd(b,a%b):a;}
inline void getInv(int inv[],const int lim,const int MOD){
    inv[0]=inv[1]=1;for(int i=2;i<=lim;++i)inv[i]=1ll*inv[MOD%i]*(MOD-MOD/i)%MOD;
}
inline LL mulMod(const LL a,const LL b,const LL mod){//long long multiplie_mod
    return ((a*b-(LL)((long double)a/mod*b+1e-8)*mod)%mod+mod)%mod;
}

const int MAXN=100;
const int MAXK=29;

int n,a[MAXN+5];
int cnt[MAXK+5][MAXN+5];
int bitcnt[MAXN+5];

inline void Init(){
    n=read(1);
    rep(i,1,n)a[i]=read(1);
}

inline void Getcnt(){
    rep(i,0,MAXK){
        rep(j,1,n)if((a[j]^(1<<i))<a[j])cnt[i][j]=-1;
        else cnt[i][j]=1;
    }
}

struct node{
    int a[MAXN+5];
    node(){memset(a,0,sizeof a);}
    inline bool operator <(const node rhs)const{
        rep(i,1,n)if(a[i]^rhs.a[i])
            return a[i]<rhs.a[i];
        return 0;
    }
}tmp;
map<node,int>Map;
int t[MAXN+5];
node arr[1<<17];
int ax[1<<17],acnt;
inline void putIn(const int x,const int ind){
    // printf("When ind == %d, x == %d\n",ind,x);
    tmp=node();
    rep(i,1,n-1)tmp.a[i]=t[i]-t[i+1];
    // rep(i,1,n-1)writc(tmp.a[i],' ');Endl;
    if(ind==0){
        ++acnt;
        arr[acnt]=tmp,ax[acnt]=x;
    }
    else Map[tmp]=x;
}
void dfs(const int i,const int now,const int lim,const int x){
    if(now>lim)return putIn(x,i);
    dfs(i,now+1,lim,x);
    rep(j,1,n)t[j]+=cnt[now][j];
    dfs(i,now+1,lim,x^(1<<now));
    rep(j,1,n)t[j]-=cnt[now][j];
}

inline void Getbitcnt(){
    rep(i,1,n){
        rep(j,0,MAXK)if((a[i]>>j)&1)
            ++bitcnt[i];
    }
    rep(i,1,n-1)bitcnt[i]-=bitcnt[i+1];
}

signed main(){
    Init();
    Getcnt();
    Getbitcnt();
    dfs(0,0,14,0);
    dfs(1,15,29,0);
    rep(i,1,acnt){
        tmp=node();
        rep(j,1,n-1)tmp.a[j]=-bitcnt[j]-arr[i].a[j];
        if(Map.count(tmp)){
            printf("%d\n",ax[i]|Map[tmp]);
            return 0;
        }
    }printf("-1\n");
	return 0;
}
posted @ 2020-07-20 09:01  Arextre  阅读(107)  评论(0编辑  收藏  举报