当前位置:网站首页>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 + 贪心
边栏推荐
- Uniapp development, packaged as H5 and deployed to the server
- Classical concurrency problem: the dining problem of philosophers
- Folding and sinking sand -- weekly record of ETF
- Set data real-time update during MDK debug
- 常用API类及异常体系
- State mode design procedure: Heroes in the game can rest, defend, attack normally and attack skills according to different physical strength values.
- Codeforces round 804 (Div. 2) [competition record]
- 新手入门深度学习 | 3-6:优化器optimizers
- How spark gets columns in dataframe --column, $, column, apply
- Global and Chinese markets for hinged watertight doors 2022-2028: Research Report on technology, participants, trends, market size and share
猜你喜欢

Leetcode 450 deleting nodes in a binary search tree

Illustrated network: the principle behind TCP three-time handshake, why can't two-time handshake?

KDD 2022 | EEG AI helps diagnose epilepsy

Spark SQL null value, Nan judgment and processing

AtCoder Beginner Contest 254【VP记录】

About the slmgr command

常用API类及异常体系

MCU通过UART实现OTA在线升级流程

For a deadline, the IT fellow graduated from Tsinghua suddenly died on the toilet

Free chat robot API
随机推荐
Classical concurrency problem: the dining problem of philosophers
How to solve the problems caused by the import process of ecology9.0
[Chongqing Guangdong education] reference materials for Zhengzhou Vocational College of finance, taxation and finance to play around the E-era
Promise
For a deadline, the IT fellow graduated from Tsinghua suddenly died on the toilet
[designmode] Decorator Pattern
Global and Chinese market of valve institutions 2022-2028: Research Report on technology, participants, trends, market size and share
Data analysis thinking analysis methods and business knowledge - analysis methods (III)
常用API类及异常体系
从 1.5 开始搭建一个微服务框架——调用链追踪 traceId
数据分析思维分析方法和业务知识——分析方法(二)
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]
Spark获取DataFrame中列的方式--col,$,column,apply
The value of applet containers
Go learning --- read INI file
Arduino hexapod robot
LeetCode 6006. Take out the least number of magic beans
图解网络:TCP三次握手背后的原理,为啥两次握手不可以?
Global and Chinese markets for pressure and temperature sensors 2022-2028: Research Report on technology, participants, trends, market size and share
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