当前位置:网站首页>Cf:h. maximum and [bit operation practice + K operations + maximum and]
Cf:h. maximum and [bit operation practice + K operations + maximum and]
2022-07-06 00:44:00 【White speed Dragon King's review】

analysis
and Just one 0 This position is still 0
So we count 31 individual bit Including 1 The number of
And then greed , From the highest position , As long as there is k Within, we will add ans in ( Add 2 ** i)
This is the bit operation expanded into dict, Then come down greedily from the highest position
Just get a high position , Just make money
So start from the top
ac code
import sys
input = sys.stdin.readline
for _ in range(int(input())):
n, k = list(map(int, input().split()))
a = list(map(int, input().split()))
bitDict = {
}
for i in range(31):
bitDict[i] = 0
for aa in a:
#print(bin(aa)[2:].zfill(31))
bin_str = (bin(aa)[2:].zfill(31))[::-1]
for i in range(31):
if bin_str[i] == '1':
bitDict[i] += 1
ans = 0
# greedy
for i in range(30, -1, -1):
# normal add 1
if k >= n - bitDict[i]:
ans += 2 ** i
k -= n - bitDict[i]
print(ans)
summary
An operation + and + greedy
边栏推荐
- NLP text processing: lemma [English] [put the deformation of various types of words into one form] [wet- > go; are- > be]
- Codeforces round 804 (Div. 2) [competition record]
- Classical concurrency problem: the dining problem of philosophers
- Single source shortest path exercise (I)
- [groovy] XML serialization (use markupbuilder to generate XML data | create sub tags under tag closures | use markupbuilderhelper to add XML comments)
- Opencv classic 100 questions
- How to make your own robot
- For a deadline, the IT fellow graduated from Tsinghua suddenly died on the toilet
- Room cannot create an SQLite connection to verify the queries
- Leetcode 450 deleting nodes in a binary search tree
猜你喜欢

MIT博士论文 | 使用神经符号学习的鲁棒可靠智能系统

Cve-2017-11882 reappearance

Opencv classic 100 questions

Folding and sinking sand -- weekly record of ETF

Spark SQL空值Null,NaN判断和处理

Free chat robot API

Data analysis thinking analysis methods and business knowledge -- analysis methods (II)

常用API类及异常体系

uniapp开发,打包成H5部署到服务器

MySQL存储引擎
随机推荐
MySQL存储引擎
Uniapp development, packaged as H5 and deployed to the server
[groovy] JSON serialization (jsonbuilder builder | generates JSON string with root node name | generates JSON string without root node name)
Free chat robot API
【线上小工具】开发过程中会用到的线上小工具合集
curlpost-php
小程序容器可以发挥的价值
An understanding of & array names
几百行代码实现一个 JSON 解析器
Spark SQL null value, Nan judgment and processing
Analysis of the combination of small program technology advantages and industrial Internet
NLP text processing: lemma [English] [put the deformation of various types of words into one form] [wet- > go; are- > be]
State mode design procedure: Heroes in the game can rest, defend, attack normally and attack skills according to different physical strength values.
A preliminary study of geojson
Spark-SQL UDF函数
Location based mobile terminal network video exploration app system documents + foreign language translation and original text + guidance records (8 weeks) + PPT + review + project source code
LeetCode 1598. Folder operation log collector
电机的简介
STM32 key chattering elimination - entry state machine thinking
Starting from 1.5, build a micro Service Framework - call chain tracking traceid