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

Pytorch---使用Pytorch实现LinkNet进行语义分割
![[1200. Différence absolue minimale]](/img/fa/4ffbedd8f24c75a20d3eaeaf0430ae.png)
[1200. Différence absolue minimale]

字节测试工程师十年经验直击UI 自动化测试痛点

测试员的算法面试题-找众数

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

看腾讯大老如何做接口自动化测试

WinCC7.5 SP1如何通过交叉索引来寻找变量及其位置?

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

What are the functional modules of RFID warehouse management system solution

How does win11 search for wireless displays? Win11 method of finding wireless display device
随机推荐
Introduction to pressure measurement of JMeter
Quelques suggestions pour la conception de l'interface
福昕PDF编辑器v10.1.8绿色版
D3.js+Three.js数据可视化3d地球js特效
How does wincc7.5 SP1 find variables and their positions through cross indexing?
See how Tencent does interface automation testing
Go language notes (4) go common management commands
async await 在map中使用
[1200. Minimum absolute difference]
冰河的海报封面
JS closure
PS vertical English and digital text how to change direction (vertical display)
Flet tutorial 04 basic introduction to filledtonalbutton (tutorial includes source code)
Actual combat simulation │ JWT login authentication
【optimtool.unconstrain】无约束优化工具箱
杰理之AD 系列 MIDI 功能说明【篇】
[Shenbo introduction] VI How to contact your favorite doctoral tutor
多模输入事件分发机制详解
[solution] paddlepaddle 2 X call static graph mode
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??