当前位置:网站首页>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;
}边栏推荐
猜你喜欢

【服务器数据恢复】某品牌服务器存储raid5数据恢复案例

接口設計時的一些建議

Stealing others' vulnerability reports and selling them into sidelines, and the vulnerability reward platform gives rise to "insiders"

五子棋 上班摸鱼工具 可局域网/人机

杰理之AD 系列 MIDI 功能说明【篇】
![[1200. Minimum absolute difference]](/img/fa/4ffbedd8f24c75a20d3eaeaf0430ae.png)
[1200. Minimum absolute difference]

Detailed explanation of multi-mode input event distribution mechanism

How does wincc7.5 SP1 find variables and their positions through cross indexing?

Billions of citizens' information has been leaked! Is there any "rescue" for data security on the public cloud?

接口设计时的一些建议
随机推荐
redis管道
[solution] paddlepaddle 2 X call static graph mode
Vue cleans up the keepalive cache scheme in a timely manner
Introduction to pressure measurement of JMeter
Test case (TC)
Billions of citizens' information has been leaked! Is there any "rescue" for data security on the public cloud?
PS vertical English and digital text how to change direction (vertical display)
Huawei ENSP simulator enables devices of multiple routers to access each other
[server data recovery] a case of RAID5 data recovery stored in a brand of server
阿里云国际版CDN的优势
吐槽 B 站收费,是怪它没钱么?
Huawei simulator ENSP common commands
杰理之AD 系列 MIDI 功能说明【篇】
go defer的使用说明
Go notes (1) go language introduction and characteristics
【解决方案】PaddlePaddle 2.x调用静态图模式
Explication détaillée du mécanisme de distribution des événements d'entrée multimodes
Pytorch---使用Pytorch实现LinkNet进行语义分割
Idea plug-in
shp数据制作3DTiles白膜