pegasus7d's blog

By pegasus7d, history, 4 weeks ago, In English
  • Vote: I like it
  • -4
  • Vote: I do not like it

»
4 weeks ago, # |
  Vote: I like it 0 Vote: I do not like it

HOW TO OPTIMIZE THIS CODE

// this is code
#include <iostream>
#include <vector>
#include <unordered_set>
#include <algorithm>

using namespace std;

const int MOD = 1e9 + 7;

int calculateMinCost(int left, int right, const unordered_set<int>& special_set, int P, int Q) {
    int L = right - left + 1;
    int X = 0;
    for (int i = left; i <= right; ++i) {
        if (special_set.count(i)) X++;
    }
    if (X == 0) {
        return Q;
    }
    int costMethod1 = (1LL * L * X % MOD) * P % MOD;
    if (L % 2 != 0) {
        return costMethod1;
    }
    int mid = left + (L / 2) - 1;
    int costLeft = calculateMinCost(left, mid, special_set, P, Q);
    int costRight = calculateMinCost(mid + 1, right, special_set, P, Q);
    int costMethod3 = (costLeft + costRight) % MOD;
    return min(costMethod1, costMethod3);
}

int removeIntegers(int N, int P, int Q, vector<int>& special_integers) {
    unordered_set<int> special_set(special_integers.begin(), special_integers.end());
    int result = calculateMinCost(1, (1 << N), special_set, P, Q);
    return result;
}

int main() {
    int N = 2;
    int P = 2;
    int Q = 1;
    vector<int> special_integers = {1, 3};
    cout << removeIntegers(N, P, Q, special_integers) << endl;
    return 0;
}

The rest of the text.

»
4 weeks ago, # |
  Vote: I like it 0 Vote: I do not like it

Auto comment: topic has been updated by pegasus7d (previous revision, new revision, compare).