当前位置:网站首页>C - XOR to all (binary topic)
C - XOR to all (binary topic)
2022-07-05 06:16:00 【whitewall_ nine】
https://atcoder.jp/contests/arc135/tasks/arc135_c
#include<iostream>
using namespace std;
#define rep(i,l,r) for(int i=(l);i<=(r);i++)
#define per(i,l,r) for(int i=(l);i>=(r);i--)
#define ll long long
#define pii pair<int, int>
#define mset(s,t) memset(s,t,sizeof(t))
#define mcpy(s,t) memcpy(s,t,sizeof(t))
#define fir first
#define pb push_back
#define sec second
#define sortall(x) sort((x).begin(),(x).end())
inline int read () {
int x = 0, f = 0;
char ch = getchar();
while (!isdigit(ch)) f |= (ch=='-'),ch= getchar();
while (isdigit(ch)) x = x * 10 + ch - '0', ch = getchar();
return f?-x:x;
}
template<typename T> void print(T x) {
if (x < 0) putchar('-'), x = -x;
if (x >= 10) print(x/10);
putchar(x % 10 + '0');
}
const int N = 35;
int cnt[N];
int a[300005];
int n;
void solve() {
cin >> n;ll ans = 0;
for (int i = 1; i <= n; i ++) {
cin >> a[i];
int t= a[i];
ans += t;
for (int j =0; j <= 30; j ++)
cnt[j] += (t >> j & 1);
}
for (int i = 1; i <= n; i++) {
ll res = 0;
for (int j = 0; j <= 30; j++)
res += ((a[i] >> j & 1) == 1? n - cnt[j] : cnt[j]) * ((ll)1 << j);
ans = max (ans, res);
}
cout << ans << endl;
}
int main () {
int t;
t =1;
while (t --) solve();
return 0;
}
The difficulty lies in how to efficiently count the sum after XOR . We observe binary , For each selected value , We have found , If the binary bit of the current value is 0, Then the binary of the same position of all other numbers is upper 1 Will be preserved , And contribution equals 1 Multiply the number of by the weight of the position . Otherwise, it counts 0 The number of contributions . Pay attention to the synthesis of the initial state . I've been thinking about how to make a sum , Numbers are not considered as binary bits , So we can't find the law . Next time you encounter this problem, you should consider binary representation .
Some properties of XOR
For sequence A, B,B It's from A The result after XOR operation .
a[i]^a[j] = b[i]^b[j];
边栏推荐
- wordpress切换页面,域名变回了IP地址
- 打印机脱机时一种容易被忽略的原因
- One question per day 1447 Simplest fraction
- Binary search template
- 阿里新成员「瓴羊」正式亮相,由阿里副总裁朋新宇带队,集结多个核心部门技术团队
- One question per day 2047 Number of valid words in the sentence
- 【LeetCode】Easy | 20. Valid parentheses
- 1.15 - input and output system
- LeetCode 0107. Sequence traversal of binary tree II - another method
- leetcode-3:无重复字符的最长子串
猜你喜欢

liunx启动redis

MySQL advanced part 2: MySQL architecture

Doing SQL performance optimization is really eye-catching

MySQL advanced part 2: SQL optimization

QQ电脑版取消转义符输入表情

leetcode-6111:螺旋矩阵 IV

7. Processing the input of multidimensional features

Spark中groupByKey() 和 reduceByKey() 和combineByKey()

传统数据库逐渐“难适应”,云原生数据库脱颖而出

Appium foundation - use the first demo of appium
随机推荐
Leetcode-6110: number of incremental paths in the grid graph
1.15 - 输入输出系统
leetcode-31:下一个排列
Leetcode-6108: decrypt messages
WordPress switches the page, and the domain name changes back to the IP address
One question per day 1447 Simplest fraction
One question per day 1765 The highest point in the map
wordpress切换页面,域名变回了IP地址
Leetcode-3: Longest substring without repeated characters
LeetCode 0108.将有序数组转换为二叉搜索树 - 数组中值为根,中值左右分别为左右子树
Leetcode stack related
[leetcode] day95 effective Sudoku & matrix zeroing
打印机脱机时一种容易被忽略的原因
Data visualization chart summary (II)
MySQL advanced part 1: stored procedures and functions
Chapter 6 relational database theory
【Rust 笔记】17-并发(下)
Traversal of leetcode tree
The sum of the unique elements of the daily question
MySQL advanced part 2: MySQL architecture