当前位置:网站首页>2021 CCPC 哈尔滨 I. Power and Zero(二进制 + 思维)
2021 CCPC 哈尔滨 I. Power and Zero(二进制 + 思维)
2022-07-04 20:13:00 【GHOSTANDBREAD】
借鉴于:I. Power and Zero(二进制,思维)_Looy_cai的博客-CSDN博客
思路:
将一串十进制的数转为二进制,然后各个数位相加,这时候从高到低可能不是递增的,所以要进行高位到低位的转换。直到从高到低都是不递减的状态。这时候最后一位,也就是数值最大的那一位,就是答案。
注意:
将一串数转为二进制时,每个数的二进制长度可能不同,所以要记录最大长度作为二进制数组的长度。
从高到低转换时,转换一遍是不能保证达到不递减的状态的,因为bit[1]和bit[2]转换后,bit[2]和bit[3]又转换后,bit[2]变小了,bit[1]又比bit[2]大了,所以要不断循环直到二进制数组达到从高到低都是不递减的状态。
代码:
#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;
}
边栏推荐
- Vue cleans up the keepalive cache scheme in a timely manner
- vim异步问题
- ApplicationContext 与 BeanFactory 区别(MS)
- 网件r7000梅林系统虚拟内存创建失败,提示USB磁盘读写速度不满足要求解决办法,有需要创建虚拟内存吗??
- Jmeter 之压测入门
- LeetCode 7. Integer inversion
- [Shenbo introduction] VI How to contact your favorite doctoral tutor
- 实战模拟│JWT 登录认证
- heatmap.js图片热点热力图插件
- Explication détaillée du mécanisme de distribution des événements d'entrée multimodes
猜你喜欢
How does win11 search for wireless displays? Win11 method of finding wireless display device
heatmap.js图片热点热力图插件
HWiNFO硬件检测工具v7.26绿色版
分析伦敦银走势图的技巧
Alibaba testers use UI automated testing to achieve element positioning
See how Tencent does interface automation testing
Fleet tutorial 08 introduction to AppBar toolbar Basics (tutorial includes source code)
【服务器数据恢复】某品牌服务器存储raid5数据恢复案例
福昕PDF编辑器v10.1.8绿色版
hash 表的概念及应用
随机推荐
【解决方案】PaddlePaddle 2.x调用静态图模式
6月“墨力原创作者计划”获奖名单公布!邀您共话国产数据库
Flet tutorial 07 basic introduction to popupmenubutton (tutorial includes source code)
Go language notes (2) some simple applications of go
B站视频 声音很小——解决办法
伦敦银走势图分析的新方法
Hwinfo hardware detection tool v7.26 green version
Play the music of youth
Advantages of semantic tags and block level inline elements
Flet tutorial 06 basic introduction to textbutton (tutorial includes source code)
Four traversal methods of binary tree, as well as the creation of binary tree from middle order to post order, pre order to middle order, pre order to post order, and sequence [specially created for t
Advantages of RFID warehouse management system solution
uniapp 富文本编辑器使用
插入排序,选择排序,冒泡排序
torch.tensor和torch.Tensor的区别
js 3D爆炸碎片图片切换js特效
面对同样复杂的测试任务为什么大老很快能梳理解决方案,阿里十年测试工程师道出其中的技巧
What should I do if my computer sharing printer refuses access
华为模拟器ensp常用命令
五子棋 上班摸鱼工具 可局域网/人机