当前位置:网站首页>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;
}
边栏推荐
- LeetCode 8. String conversion integer (ATOI)
- WinCC7.5 SP1如何通过交叉索引来寻找变量及其位置?
- 多模输入事件分发机制详解
- acwing 3302. Expression evaluation
- 基于OpenCV haarcascades的对象检测
- Gobang go to work fishing tools can be LAN / man-machine
- __ init__ () missing 2 required positive arguments
- D3.js+Three.js数据可视化3d地球js特效
- 【微信小程序】协同工作与发布
- 【Try to Hack】宽字节注入
猜你喜欢
随机推荐
华为ensp模拟器实现通信安全(交换机)
【服务器数据恢复】某品牌服务器存储raid5数据恢复案例
【观察】联想:3X(1+N)智慧办公解决方案,释放办公生产力“乘数效应”
Quelques suggestions pour la conception de l'interface
Explication détaillée du mécanisme de distribution des événements d'entrée multimodes
[observation] Lenovo: 3x (1+n) smart office solution, releasing the "multiplier effect" of office productivity
Embedded TC test case
杰理之AD 系列 MIDI 功能说明【篇】
杰理之AD 系列 MIDI 功能说明【篇】
Leetcode+ 81 - 85 monotone stack topic
嵌入式TC 测试用例
Solution of 5g unstable 5g signal often dropped in NetWare r7000 Merlin system
Day24:文件系统
LeetCode 8. 字符串转换整数 (atoi)
搭建一个仪式感点满的网站,并内网穿透发布到公网 1/2
Play the music of youth
[solution] paddlepaddle 2 X call static graph mode
ACM组合计数入门
B站视频 声音很小——解决办法
HMS Core 统一扫码服务