当前位置:网站首页>cf:H. Maximal AND【位运算练习 + k次操作 + 最大And】
cf:H. Maximal AND【位运算练习 + k次操作 + 最大And】
2022-07-06 00:38:00 【白速龙王的回眸】
分析
and就是只要有一个0这个位置还是0
所以我们统计31个bit的含1的个数
然后贪心,从最高位看起,只要有在k以内的我们就加到ans中(加2 ** i)
这个就是位运算展开成dict,然后从最高位贪心下来
只要得到某一个高位,就是赚了
所以从高位开始贪心
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)
总结
位运算 + and + 贪心
边栏推荐
- State mode design procedure: Heroes in the game can rest, defend, attack normally and attack skills according to different physical strength values.
- NLP text processing: lemma [English] [put the deformation of various types of words into one form] [wet- > go; are- > be]
- How to make your own robot
- Calculate sha256 value of data or file based on crypto++
- Power query data format conversion, Split Merge extraction, delete duplicates, delete errors, transpose and reverse, perspective and reverse perspective
- Spark DF adds a column
- notepad++正则表达式替换字符串
- Spark SQL空值Null,NaN判断和处理
- The value of applet containers
- 多线程与高并发(8)—— 从CountDownLatch总结AQS共享锁(三周年打卡)
猜你喜欢
Calculate sha256 value of data or file based on crypto++
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
esxi的安装和使用
MCU通过UART实现OTA在线升级流程
Analysis of the combination of small program technology advantages and industrial Internet
State mode design procedure: Heroes in the game can rest, defend, attack normally and attack skills according to different physical strength values.
Uniapp development, packaged as H5 and deployed to the server
Atcoder beginer contest 258 [competition record]
Keepalive component cache does not take effect
Basic introduction and source code analysis of webrtc threads
随机推荐
PHP determines whether an array contains the value of another array
MySQL storage engine
[groovy] JSON string deserialization (use jsonslurper to deserialize JSON strings | construct related classes according to the map set)
The relationship between FPGA internal hardware structure and code
LeetCode 6004. Get operands of 0
notepad++正則錶達式替換字符串
《强化学习周刊》第52期:Depth-CUPRL、DistSPECTRL & Double Deep Q-Network
【线上小工具】开发过程中会用到的线上小工具合集
Global and Chinese market of water heater expansion tank 2022-2028: Research Report on technology, participants, trends, market size and share
Calculate sha256 value of data or file based on crypto++
esxi的安装和使用
Spark获取DataFrame中列的方式--col,$,column,apply
Basic introduction and source code analysis of webrtc threads
MySQL存储引擎
建立时间和保持时间的模型分析
The third season of ape table school is about to launch, opening a new vision for developers under the wave of going to sea
Meta AI西雅图研究负责人Luke Zettlemoyer | 万亿参数后,大模型会持续增长吗?
Ffmpeg captures RTSP images for image analysis
Intranet Security Learning (V) -- domain horizontal: SPN & RDP & Cobalt strike
Codeforces gr19 D (think more about why the first-hand value range is 100, JLS yyds)