当前位置:网站首页>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 + 贪心
边栏推荐
- Leetcode 450 deleting nodes in a binary search tree
- Free chat robot API
- Synchronized and reentrantlock
- STM32按键消抖——入门状态机思维
- MySQL storage engine
- The value of applet containers
- Set data real-time update during MDK debug
- NLP basic task word segmentation third party Library: ICTCLAS [the third party library with the highest accuracy of Chinese word segmentation] [Chinese Academy of Sciences] [charge]
- 看抖音直播Beyond演唱会有感
- Global and Chinese markets for hinged watertight doors 2022-2028: Research Report on technology, participants, trends, market size and share
猜你喜欢
MCU通过UART实现OTA在线升级流程
Spark AQE
XML Configuration File
Extension and application of timestamp
Room cannot create an SQLite connection to verify the queries
Room cannot create an SQLite connection to verify the queries
LeetCode 1189. Maximum number of "balloons"
猿桌派第三季开播在即,打开出海浪潮下的开发者新视野
[groovy] compile time meta programming (compile time method interception | method interception in myasttransformation visit method)
Extracting profile data from profile measurement
随机推荐
Ffmpeg captures RTSP images for image analysis
Introduction of motor
[groovy] compile time meta programming (compile time method interception | method interception in myasttransformation visit method)
小程序技术优势与产业互联网相结合的分析
CTF daily question day44 rot
详细页返回列表保留原来滚动条所在位置
XML配置文件
Global and Chinese markets of universal milling machines 2022-2028: Research Report on technology, participants, trends, market size and share
For a deadline, the IT fellow graduated from Tsinghua suddenly died on the toilet
Leetcode 450 deleting nodes in a binary search tree
Pointer pointer array, array pointer
Leetcode Fibonacci sequence
Power Query数据格式的转换、拆分合并提取、删除重复项、删除错误、转置与反转、透视和逆透视
Spark DF增加一列
PHP determines whether an array contains the value of another array
【文件IO的简单实现】
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)
esxi的安装和使用
Search (DFS and BFS)