当前位置:网站首页>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 + 贪心
边栏推荐
- PHP determines whether an array contains the value of another array
- [groovy] compile time meta programming (AST syntax tree conversion with annotations | define annotations and use groovyasttransformationclass to indicate ast conversion interface | ast conversion inte
- The value of applet containers
- Problems and solutions of converting date into specified string in date class
- Spark获取DataFrame中列的方式--col,$,column,apply
- 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]
- curlpost-php
- Spark-SQL UDF函数
- 小程序容器可以发挥的价值
- 《编程之美》读书笔记
猜你喜欢

多线程与高并发(8)—— 从CountDownLatch总结AQS共享锁(三周年打卡)

How to solve the problems caused by the import process of ecology9.0

如何制作自己的機器人

Problems and solutions of converting date into specified string in date class
![[groovy] XML serialization (use markupbuilder to generate XML data | create sub tags under tag closures | use markupbuilderhelper to add XML comments)](/img/d4/4a33e7f077db4d135c8f38d4af57fa.jpg)
[groovy] XML serialization (use markupbuilder to generate XML data | create sub tags under tag closures | use markupbuilderhelper to add XML comments)

Starting from 1.5, build a micro Service Framework - call chain tracking traceid

Search (DFS and BFS)

notepad++正則錶達式替換字符串

Spark AQE

MySQL存储引擎
随机推荐
[groovy] compile time metaprogramming (compile time method injection | method injection using buildfromspec, buildfromstring, buildfromcode)
notepad++正则表达式替换字符串
Promise
LeetCode 6004. Get operands of 0
[groovy] JSON serialization (convert class objects to JSON strings | convert using jsonbuilder | convert using jsonoutput | format JSON strings for output)
Arduino hexapod robot
Data analysis thinking analysis methods and business knowledge - analysis methods (III)
[Chongqing Guangdong education] reference materials for Zhengzhou Vocational College of finance, taxation and finance to play around the E-era
Browser reflow and redraw
NLP text processing: lemma [English] [put the deformation of various types of words into one form] [wet- > go; are- > be]
Global and Chinese markets of POM plastic gears 2022-2028: Research Report on technology, participants, trends, market size and share
The third season of ape table school is about to launch, opening a new vision for developers under the wave of going to sea
【文件IO的简单实现】
Opencv classic 100 questions
Codeforces round 804 (Div. 2) [competition record]
Classic CTF topic about FTP protocol
Getting started with devkit
[simple implementation of file IO]
Yolov5, pychar, Anaconda environment installation
Spark DF增加一列