思路
因为对于两个正整数 a , b a,b a,b
a
⊕
b
≤
a
+
b
a\oplus b\le a+b
a⊕b≤a+b
所以当两个分组的权值求和肯定没有两个分组异或后的和更小。
所以最后把所有的数异或起来是最优的。
时间复杂度: O ( n ) O(n) O(n)
// Problem: P5514 [MtOI2019]永夜的报应
// Contest: Luogu
// URL: https://www.luogu.com.cn/problem/P5514
// Memory Limit: 125 MB
// Time Limit: 1000 ms
// Date: 2021-05-10 15:01:51
// --------by Herio--------
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const int N=1e3+5,M=2e4+5,inf=0x3f3f3f3f,mod=1e9+7;
#define mst(a,b) memset(a,b,sizeof a)
#define PII pair<int,int>
#define fi first
#define se second
#define pb emplace_back
#define SZ(a) (int)a.size()
#define IOS ios::sync_with_stdio(false),cin.tie(0)
void Print(int *a,int n){
for(int i=1;i<n;i++)
printf("%d ",a[i]);
printf("%d\n",a[n]);
}
int main(){
int n;
scanf("%d",&n);
int s=0;
for(int i=0;i<n;i++){
int x;scanf("%d",&x);
s^=x;
}
printf("%d\n",s);
return 0;
}