首页 > 其他分享 >1699H - Maximal And

1699H - Maximal And

时间:2023-07-01 12:46:59浏览次数:38  
标签:Maximal cnt 高位 int 操作数 -- 位数 1699H

思路:

0. 只有所有数这一位是1 &结果才为1
1. 想要得出最大值,高位越大越好 所以从高位开始操作
2. 记录所有数字的每位 为1的cnt[位数]++ 然后那位需要操作的次数为 n-cnt[位数]
3. 优先执行:操作数给高位 如果操作数不够使高位&后结果改变 则给可以被改变的最高位
4. 终止条件: 操作数为0或剩余所有位数均无法改动

#include<bits/stdc++.h>
using namespace std;
int t,n,k,a[200010],cnt[31],ans;
int main(){
    cin>>t;
    while(t--){
        cin>>n>>k;
        ans=0;
        for(int i=0;i<31;i++){
            cnt[i]=0;
        }
        for(int i=0;i<n;i++){
            cin>>a[i];
            for(int j=30;j>=0;j--){
                if(a[i]&(1<<j)){
                    cnt[j]++;
                }
            }
        }
        for(int i=30;i>=0;i--){
            if(n-cnt[i]<= k) {
                k-=n-cnt[i];
                ans+=1<<i;
            }
        }
        cout<<ans<<endl;
    }
    return 0;
} 

 

标签:Maximal,cnt,高位,int,操作数,--,位数,1699H
From: https://www.cnblogs.com/cruz-fy/p/17519115.html

相关文章

  • Educational Codeforces Round 20-C. Maximal GCD
    原题链接C.MaximalGCDtimelimitpertestmemorylimitpertestinputoutputn.Youshouldcreatesuch strictlyincreasing sequenceof k positivenumbers a1, a2, ..., ak,thattheirsumisequalto nGr......
  • Online Continual Learning with Maximally Interfered Retrieval---阅读笔记
    OnlineContinualLearningwithMaximallyInterferedRetrieval---阅读笔记摘要:本文主要提出了一种可控的样本采集策略的重放方法。我们检索受干扰最大的样本,即它们的预测将受到预测参数更新的最大负面影响。1Introduction人工神经网络在完成个体狭窄任务方面的性能已经超......
  • LeetCode: 221. Maximal Square
    LeetCode:221.MaximalSquare题目描述Givena2Dbinarymatrixfilledwith​​0​​​'sand​​1​​​'s,findthelargestsquarecontainingonly​​1​​'s......