309C - Memory for Arrays - CodeForces Solution


binary search bitmasks greedy *1900

Please click on ads to support us..

C++ Code:

#include <bits/stdc++.h>
using namespace std; 
const long long maxSz = 1e6 + 5;

long long n, m;
long long mem[maxSz];
long long oddCnt[35];
long long ohmy[35];
long long orig[35];
long long powe[35];

void computePow() {
    powe[0] = 1;
    for(long long i = 1; i < 35; i++) {
        powe[i] = powe[i - 1] * 2;
    }
}

long long nums[maxSz];

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    computePow();
    cin >> n >> m; 
    for(long long i = 0; i < n; i++) {
        cin >> mem[i]; 
    }
    for(long long i = 0; i < m; i++) {
        long long x; cin >> x;
        nums[i] = powe[x];
        ohmy[x]++;
        orig[x]++;
    }
    sort(nums, nums + m);

    for(long long i = 0; i < 35; i++) {
        long long cnt = 0;
        for(long long j = 0; j < n; j++) {
            cnt += mem[j] % 2 == 1;
            mem[j] /= 2;
        }
        oddCnt[i] = cnt;
    }

    long long i = 0; 
    long long ptr = 0;
    while(i < m && ptr < 35) {
        long long currCap = powe[ptr];
        long long s = 0; // maybe A[i] not sure 
        while(i < m && oddCnt[ptr] > 0 && nums[i] <= currCap) {
            // cout << "Bundle: ";
            while(i < m && s + nums[i] <= currCap) {
                s += nums[i]; 
                // cout << nums[i] << " ";
                i++;
            }
            // cout << '\n';
            oddCnt[ptr]--; 
            s = 0;
        }
        ptr++;
    }
    cout << i << '\n';



}


Comments

Submit
0 Comments
More Questions

1328A - Divisibility Problem
339A - Helpful Maths
4A - Watermelon
476A - Dreamoon and Stairs
1409A - Yet Another Two Integers Problem
977A - Wrong Subtraction
263A - Beautiful Matrix
180C - Letter
151A - Soft Drinking
1352A - Sum of Round Numbers
281A - Word Capitalization
1646A - Square Counting
266A - Stones on the Table
61A - Ultra-Fast Mathematician
148A - Insomnia cure
1650A - Deletions of Two Adjacent Letters
1512A - Spy Detected
282A - Bit++
69A - Young Physicist
1651A - Playoff
734A - Anton and Danik
1300B - Assigning to Classes
1647A - Madoka and Math Dad
710A - King Moves
1131A - Sea Battle
118A - String Task
236A - Boy or Girl
271A - Beautiful Year
520B - Two Buttons
231A - Team