1669H - Maximal AND - CodeForces Solution


bitmasks greedy math *1300

Please click on ads to support us..

Python Code:

t = int(input())

while t:
    n, k = map(int, input().split())
    A = list(map(int, input().split()))
    cnt = [0] * 31
    for x in A:
        for i in range(31):
            cnt[i] += (1 - ((x >> i) & 1))
    for i in range(30, -1, -1):
        if k >= cnt[i]:
            k -= cnt[i]
            cnt[i] = 0


    ans = 0
    for i in range(0, 31):
        if cnt[i] == 0:
            ans += (1 << i)
    print(ans)
    t -= 1

C++ Code:

#include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define vbe(v) v.begin(), v.end()
#define int long long
#define fi first
#define sec second
const int mod = 1e9 + 7;
const int N = 1e5 + 7;
#define cy cout << "YES" << endl
#define cn cout << "NO" << endl
#define ch(n) cout << "hello-" << n << endl

void solve()
{
    int n, k;
    cin >> n >> k;
    vector<int> hsh(31, 0);
    int tt;
    vector<int> v;
    for (int i = 0; i < n; i++)
    {
        cin >> tt;
        v.pb(tt);
        for (int ii = 30; ii >= 0; ii--)
        {
            if (((tt >> ii) & 1) == 1)
            {
                hsh[ii]++;
            }
        }
    }
    // for(auto &it:hsh){
    //    cout << it << ' ';
    // }
    // return;
    vector<pair<int, int>> vp;
    for (int i = 0; i < 31; i++)
    {
        vp.pb({i, n - hsh[i]});
    }
    reverse(vbe(vp));
    // for(auto &it:vp){
    //    cout << it.first << ' '<<it.second<<'\n';
    // }
    // return;
    // if(k>n){
    //    for (int i = 0; i < vp.size();i++){
    //       if(k>=vp[i].first && k>n){
    //       k = k - vp[i].first;
    //       val = val + (1 << vp[i].second);
    //       }
    //       else{
    //          break;
    //       }
    //    }
    // }
    vector<int> ans(31, 0);
    for (int i = 0; i < vp.size(); i++)
    {
        if (k >= vp[i].second)
        {
            k = k - vp[i].second;
            ans[vp[i].first]++;
        }
    }
    int val = 0;
    // int ct = 0;
    // for(auto &it:ans){
    //    cout << it<<' '<<ct << '\n';
    //    ct++;
    // }
    // cout << (1 << 31) << '\n';
    // return;
    // cout << ans.size() << '\n';
    for (int i = 0; i < ans.size(); i++)
    {
        if (ans[i])
        {
            val = val + (1 << i);
            // cout << val << '\n';
        }
    }
    // cout << val*2 << '\n';
    cout << val << '\n';
    // cout <<'\n';
    // cout << (1 << 8) << '\n';
}

signed main()
{

    //   #ifndef ONLINE_JUDJE
    //   freopen("input.txt", "r", stdin);
    //   freopen("output.txt", "w", stdout);
    //   #endif

    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    int test = 0;
    cin >> test;
    while (test--)
    {
        solve();
    }
    return 0;
}


Comments

Submit
0 Comments
More Questions

1475E - Advertising Agency
1345B - Card Constructions
1077B - Disturbed People
653A - Bear and Three Balls
794A - Bank Robbery
157A - Game Outcome
3B - Lorry
1392A - Omkar and Password
489A - SwapSort
932A - Palindromic Supersequence
433A - Kitahara Haruki's Gift
672A - Summer Camp
1277A - Happy Birthday Polycarp
577A - Multiplication Table
817C - Really Big Numbers
1355A - Sequence with Digits
977B - Two-gram
993A - Two Squares
1659D - Reverse Sort Sum
1659A - Red Versus Blue
1659B - Bit Flipping
1480B - The Great Hero
1519B - The Cake Is a Lie
1659C - Line Empire
515A - Drazil and Date
1084B - Kvass and the Fair Nut
1101A - Minimum Integer
985D - Sand Fortress
1279A - New Year Garland
1279B - Verse For Santa