837D - Round Subset - CodeForces Solution


dp math *2100

Please click on ads to support us..

C++ Code:

#include <bits/stdc++.h>
#define MAXN 200
#define MAXPOW (25 * MAXN)

using namespace std;

typedef pair<int, int> ii;

int max_two_pow[MAXN + 2][MAXPOW + 2];
int prev_addition[MAXN + 2][MAXPOW + 2];
int n, k;
vector<bool> mem[MAXN][MAXPOW];
vector<bool> isused[MAXN][MAXPOW];

ii numbers[MAXN + 2];

bool used(int ch, int fpow, int i)
{
    if (mem[ch][fpow][i])
        return isused[ch][fpow][i];

    mem[ch][fpow][i] = true;
    
    bool ans;
    if (prev_addition[ch][fpow] == 0)
        ans = false;
    else if (prev_addition[ch][fpow] == i)
        ans = true;
    else {
        ii to_rm = numbers[prev_addition[ch][fpow]];

        ans = used(ch - 1, fpow - to_rm.first, i);
    }

    return isused[ch][fpow][i] = ans;
}

int main(void)
{
    scanf("%d %d", &n, &k);
    for (int i = 0; i < n; i++) {
        long long int num;
        scanf("%lld", &num);

        int fivepow = 0;
        while (num % 5 == 0) {
            fivepow++;
            num /= 5;
        }

        int twopow = 0;
        while (num % 2 == 0) {
            twopow++;
            num /= 2;
        }

        numbers[i+1] = make_pair(fivepow, twopow);
    }

    for (int i = 0; i < k; i++)
        for (int j = 0; j <= MAXPOW; j++) {
            isused[i][j].resize(n + 1, false);
            mem[i][j].resize(n + 1, false);
        }

    for (int i = 0; i <= k; i++)
        memset(max_two_pow[i], -1, sizeof(max_two_pow[i]));
    max_two_pow[0][0] = 0;

    for (int chosen = 0; chosen < k; chosen++) {
        for (int fpow = 0; fpow <= MAXPOW; fpow++) {
            
            if (max_two_pow[chosen][fpow] == -1) continue;

            for (int i = 1; i <= n; i++) {
                ii cur_select = numbers[i];
                if (max_two_pow[chosen][fpow] + cur_select.second > max_two_pow[chosen + 1][fpow + cur_select.first])
                    if (!used(chosen, fpow, i)) {
                        max_two_pow[chosen + 1][fpow + cur_select.first] = max_two_pow[chosen][fpow] + cur_select.second;
                        prev_addition[chosen + 1][fpow + cur_select.first] = i;
                    }
            }
        }
    }

    int ans = 0;
    for (int i = 0; i <= MAXPOW; i++) 
        ans = max(ans, min(i, max_two_pow[k][i]));

    printf("%d\n", ans);
    return 0;
}


Comments

Submit
0 Comments
More Questions

1562B - Scenes From a Memory
1521A - Nastia and Nearly Good Numbers
208. Implement Trie
1605B - Reverse Sort
1607C - Minimum Extraction
1604B - XOR Specia-LIS-t
1606B - Update Files
1598B - Groups
1602B - Divine Array
1594B - Special Numbers
1614A - Divan and a Store
2085. Count Common Words With One Occurrence
2089. Find Target Indices After Sorting Array
2090. K Radius Subarray Averages
2091. Removing Minimum and Maximum From Array
6. Zigzag Conversion
1612B - Special Permutation
1481. Least Number of Unique Integers after K Removals
1035. Uncrossed Lines
328. Odd Even Linked List
1219. Path with Maximum Gold
1268. Search Suggestions System
841. Keys and Rooms
152. Maximum Product Subarray
337. House Robber III
869. Reordered Power of 2
1593C - Save More Mice
1217. Minimum Cost to Move Chips to The Same Position
347. Top K Frequent Elements
1503. Last Moment Before All Ants Fall Out of a Plank