社区讨论

100分代码,暴力50分,求调

P10471最大异或对 The XOR Largest Pair参与者 2已保存回复 1

讨论操作

快速查看讨论及其快照的属性,并进行相关操作。

当前回复
1 条
当前快照
1 份
快照标识符
@mivjoaio
此快照首次捕获于
2025/12/07 17:53
3 个月前
此快照最后确认于
2025/12/10 19:10
3 个月前
查看原帖
CPP
#include<bits/stdc++.h>
using namespace std;
long long n,a[5000010],b[5000010][2],c=-1e18,d;
void insert(long long e){
    long long x=0;
    for(int i=(1<<30);i;i>>=1){
        long long a=bool(e & i);
        if (!b[x][a]) b[x][a]=d++;
        x=b[x][a];
    }
    return;
}
long long hhy(long long e){
    long long ans=0,x=0;
    for(int i=(1<<30);i;i>>=1){
        long long a=bool(e & i);
        if(b[x][!a]){
            x=b[x][!a];
            ans=(ans<<1)+!a;
        }else{ 
            x=b[x][a];
            ans=(ans<<1)+a;
        }
    }
    return ans;
}
int main(){
    cin>>n;
    for(int i=1;i<=n;++i){
        cin>>a[i];
        insert(a[i]);
        long long e=hhy(a[i]);
        c=max(c,a[i]^e);
    }
    cout<<c<<endl;
    return 0;
}

// #include<bits/stdc++.h>
// using namespace std;
// int n,a[800005],c,hhy=0,hy=0;
// int main() {
// 	freopen("max_xor_pair.in","r",stdin);
// 	freopen("max_xor_pair.out","w",stdout);
// 	cin>>n;
// 	for(int i=1;i<=n;i++) cin >> a[i];
// 	sort(a + 1,a + 1 + n);
// 	for(int i=30;i>=0;i--)
// 	{          
// 	    hy|=(1<<i);
// 	    unordered_set<int> b;
// 	    for(int j=1;j<=n;j++) b.insert(a[j] & hy);
// 	    c=hhy|(1<<i);
// 	    for(auto k=b.begin();k!=b.end();k++)
// 	    {
// 	        if(b.count(*k^c))
//             {
//                 hhy=c;
//                 break;
//             } 
//         }
//     }
// 	cout<<hhy<<endl;
// 	return 0;
// }
//暴力#include<bits/stdc++.h>
//using namespace std;
//long long a,b[100001],hhy=0,c;
//int main()
//{
//	freopen("max_xor_pair.in","r",stdin);
//	freopen("max_xor_pair.out","w",stdout);
//	cin>>a;
//    for(int i=1;i<=a;i++) cin>>b[i];
//    for(int i=1;i<=a;i++)
//    {
////    	cout<<"i="<<i<<" ";
//        for(int j=i+1;j<=a;j++)
//        {
////        	cout<<"j="<<j<<" ";
//        	c=b[i]^b[j];
////        	cout<<"b[i]^b[j]="<<c<<" ";
//            if(c>hhy)hhy=c;
////            cout<<"hhy="<<hhy<<" ";
//		}
//		cout<<endl;
//	}
//    cout<<hhy<<endl; 
//	return 0;
//} 
上面是我写的代码,前两个都能过,哪位大佬能教教暴力怎么写

回复

1 条回复,欢迎继续交流。

正在加载回复...