当前位置:网站首页>2021 CCPC Harbin I. power and zero (binary + thinking)
2021 CCPC Harbin I. power and zero (binary + thinking)
2022-07-04 21:21:00 【GHOSTANDBREAD】
In view of :I. Power and Zero( Binary system , thinking )_Looy_cai The blog of -CSDN Blog
Ideas :
Convert a string of decimal numbers into binary , Then add the digits , At this time, it may not be increasing from high to low , So we need to convert from high to low . Until from high to low, there is no decreasing state . At this time, the last one , That is, the one with the largest number , That's the answer. .
Be careful :
When converting a string of numbers to binary , The binary length of each number may be different , So record the maximum length as the length of the binary array .
When switching from high to low , There is no guarantee that the state of non decreasing can be achieved by switching once , because bit[1] and bit[2] After the transformation ,bit[2] and bit[3] After conversion ,bit[2] It's getting smaller ,bit[1] And than bit[2] big , Therefore, it is necessary to keep cycling until the binary array reaches the state of no decrement from high to low .
Code :
#include<iostream>
#include<cstring>
#include<vector>
#include<string>
#include<algorithm>
using namespace std;
int t, n;
int bit[50];
int main() {
scanf("%d", &t);
while(t --) {
scanf("%d", &n);
int x;
int len = -1;
memset(bit, 0, sizeof bit);
for(int i = 0; i < n; i ++) {
scanf("%d", &x);
int ind = 0;
while(x) {
bit[++ ind] += x % 2;
x /= 2;
}
len = max(len, ind);
}
bool ok = true;
while(ok) {
ok = false;
for(int i = len; i >= 2; i --) {
if(bit[i] > bit[i - 1]) {
ok = true;
int avg = (bit[i] * 2 + bit[i - 1]) / 3;
bit[i - 1] += (bit[i] - avg) * 2;
bit[i] = avg;
}
}
}
printf("%d\n", bit[1]);
}
return 0;
}
边栏推荐
猜你喜欢
随机推荐
c语言函数形参自增自减情况分析
[solution] paddlepaddle 2 X call static graph mode
The video sound of station B is very low - solution
Detailed explanation of multi-mode input event distribution mechanism
记一次重复造轮子(Obsidian 插件设置说明汉化)
LeetCode 7. Integer inversion
搭建一个仪式感点满的网站,并内网穿透发布到公网 1/2
Hands on deep learning (III) -- convolutional neural network CNN
IIC (STM32)
【微服务|SCG】Predicate的使用
【观察】联想:3X(1+N)智慧办公解决方案,释放办公生产力“乘数效应”
【服务器数据恢复】某品牌服务器存储raid5数据恢复案例
RFID仓库管理系统解决方案有哪些功能模块
【1200. 最小绝对差】
Go notes (1) go language introduction and characteristics
杰理之AD 系列 MIDI 功能说明【篇】
Procurement in software development
uniapp 富文本编辑器使用
阿里云国际版CDN的优势
Ten years' experience of byte test engineer directly hits the pain point of UI automation test