题解:P7384 「EZEC-6」分组
zhongzhong517 · · 题解
题意
题目说的很清楚了,就是将
思路
由于要让结果最小,所以对于每个二进制位,在所有组的按位或结果中最多出现 63 - __builtin_clzll(a[i]))。
但是,最高位不同不代表所有位不同,要使所有位不同,就得上大招:并查集。枚举每一位,枚举每一组,求出第
但是,测样例
代码
#include <bits/stdc++.h>
#define int long long
using namespace std;
int n, tot, maxn = -10, a[10000005], s[105], fa[105];
#define getchar()(p1==p2&&(p2=(p1=buf)+fread(buf,1,1<<21,stdin),p1==p2)?EOF:*p1++)
char buf[1<<21],*p1=buf,*p2=buf;
template <typename T>
inline void read(T& r) {
r=0;bool w=0; char ch=getchar();
while(ch<'0'||ch>'9') w=ch=='-'?1:0,ch=getchar();
while(ch>='0'&&ch<='9') r=r*10+(ch^48), ch=getchar();
r=w?-r:r;
}
inline int find(int x){
while(fa[x] != x){
fa[x] = fa[fa[x]];
x = fa[x];
}
return x;
}
void merge(int x, int y){
int fx = find(x), fy = find(y);
fa[fx] = fy;
}
int log_2(int x){
return 63 - __builtin_clzll(x);
}
signed main(){
read(n);
for(int i = 1; i <= n; i++) read(a[i]);
for(int i = 1; i <= n; i++){
if(a[i] == 0) continue;
int lg = log_2(a[i]);
s[lg] |= a[i];
}
for(int i = 0; i <= 60; i++) fa[i] = i;
for(int i = 0; i <= 60; i++){
int lst = -1;
for(int j = 60; j >= 0; j--){
if((s[j] >> i) & 1){
if(lst != -1) merge(lst, j);
lst = j;
}
}
}
int cnt = 0;
for(int i = 0; i <= 60; i++) if(s[i] != 0 && fa[i] == i) cnt++;
for(int i = 1; i <= n; i++){
if(a[i] == 0) cnt++;
}
cout << cnt;
return 0;
}