当前位置:网站首页>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;
}
边栏推荐
- ApplicationContext 与 BeanFactory 区别(MS)
- LeetCode 8. String conversion integer (ATOI)
- 华为ensp模拟器 实现多个路由器的设备可以相互访问
- GVM use
- 福昕PDF编辑器v10.1.8绿色版
- 多模輸入事件分發機制詳解
- Sword finger offer II 80-100 (continuous update)
- Foxit pdf editor v10.1.8 green version
- How does win11 search for wireless displays? Win11 method of finding wireless display device
- 华为ensp模拟器实现通信安全(交换机)
猜你喜欢
What if the WiFi of win11 system always drops? Solution of WiFi total drop in win11 system
[server data recovery] a case of RAID5 data recovery stored in a brand of server
Day24:文件系统
NetWare r7000 Merlin system virtual memory creation failed, prompting that the USB disk reading and writing speed does not meet the requirements. Solution, is it necessary to create virtual memory??
ApplicationContext 与 BeanFactory 区别(MS)
Understand Alibaba cloud's secret weapon "dragon architecture" in the article "science popularization talent"
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
What should I do if my computer sharing printer refuses access
Golang中UTF编码和字符集
shp数据制作3DTiles白膜
随机推荐
From automation to digital twins, what can Tupo do?
Android原生数据库的基本使用和升级
多模输入事件分发机制详解
LeetCode 8. 字符串转换整数 (atoi)
Sword finger offer II 80-100 (continuous update)
嵌入式TC 测试用例
Go notes (1) go language introduction and characteristics
[server data recovery] a case of RAID5 data recovery stored in a brand of server
伦敦银走势图分析的新方法
冰河的海报封面
hash 表的概念及应用
MySQL - database query - use of aggregate function, aggregate query, grouping query
Embedded TC test case
阿里测试师用UI自动化测试实现元素定位
Leetcode+ 81 - 85 monotone stack topic
【申博攻略】六.如何联系心仪的博导
Explication détaillée du mécanisme de distribution des événements d'entrée multimodes
网件r7000梅林系统5g不稳定 5g信号经常掉线解决方法
acwing 3302. Expression evaluation
Nmap scan