公式

D - 魔物の一掃 / Sweeping Out Monsters 解説 by admin

gpt-6-astra-medium

Overview

We check all combinations of scrolls to use and find the minimum magic power consumed among the combinations that can defeat all monsters. For damage calculation of each combination, we use the Imos method, which can process range additions efficiently.

Analysis

All combinations of scrolls can be exhaustively searched

Since each scroll has \(2\) choices—either “use” or “do not use”—there are \(2^M\) combinations in total.

Since \(M \leq 15\), there are at most \(2^{15}=32768\) combinations. Therefore, we can check all combinations.

In addition, the order in which the scrolls are used does not affect the total damage or the consumed magic power, so there is no need to consider the order of use.

Directly adding damage is inefficient

If we add damage directly to all target monsters for the chosen scrolls in each combination, the time complexity will be \(O(2^M MN)\).

In the worst case, this requires around \(10^8\) operations, which risks exceeding the time limit. Therefore, we exploit the fact that a scroll’s effect is a “uniform addition to a contiguous range”.

Combining range additions with the Imos method

In the Imos method, instead of updating the entire range, we only record the boundaries where the damage increases or decreases.

Using \(0\)-based indexing, the target of scroll \(j\) is the half-open interval \([L_j-1,R_j)\). We perform the following updates on the difference array diff:

  • Add \(C_j\) to diff[L_j - 1].
  • Subtract \(C_j\) from diff[R_j].

After that, by taking the prefix sums from left to right, we obtain the total damage dealt to each monster.

For example, to deal \(3\) damage to monsters \(2\) through \(4\), using \(0\)-based indexing, we set diff[1] += 3 and diff[4] -= 3. If \(N=5\), the damage obtained from the prefix sums is \([0,3,3,3,0]\).

This allows us to evaluate a single combination in \(O(M+N)\) time.

Algorithm

  1. Initialize the answer answer with a sufficiently large value.
  2. Iterate through all integers mask such that \(0 \le \text{mask} < 2^M\).
    • If the \(j\)-th bit from the lowest bit of mask is \(1\), we consider scroll \(j+1\) to be used.
  3. For each mask, perform the following steps:
    1. Initialize the difference array diff and the consumed magic power cost to \(0\).
    2. For each scroll used, add its consumed magic power to cost, and update both ends in the difference array.
    3. If cost >= answer, this combination cannot improve the current best value, so skip the evaluation for this combination.
    4. Compute the prefix sums of diff to find the total damage dealt to each monster.
    5. If the total damage dealt to all monsters is greater than or equal to their initial HP, update answer with cost.
  4. If the answer was never updated, output \(-1\); otherwise, output answer.

Correctness

Whether each scroll is used can be represented by exactly one bit string. Therefore, by checking all mask, we can exhaustively inspect every combination of scrolls without omission.

Furthermore, the prefix sums from the Imos method correctly represent the total damage dealt to each monster by the chosen scrolls. Having all these values be greater than or equal to the initial HP is a necessary and sufficient condition to defeat all monsters.

Thus, by maintaining the minimum consumed magic power among the combinations that satisfy the condition, we obtain the desired answer. Skipping combinations with a consumed magic power greater than or equal to the current best value does not affect the answer.

Complexity

  • Time complexity: \(O(2^M(M+N))\)
  • Space complexity: \(O(N+M)\)

For each combination, it takes \(O(M)\) time to check the scrolls, and \(O(N)\) time to initialize the difference array and compute the prefix sums. The difference array is reused for each combination.

Implementation Notes

  • Use long long for totals
    The total damage and consumed magic power can be up to \(15 \times 10^9\), which does not fit in a \(32\)-bit integer.

  • Be careful with range indices
    In the code, only L_j is decremented by \(1\) after reading the input. This allows the original closed interval \([L_j,R_j]\) to be treated as the \(0\)-based half-open interval \([L_j-1,R_j)\).

  • Make the difference array of size \(N+1\)
    Even when \(R_j=N\), updating diff[N] to mark the end of the interval is necessary.

  • Early exit if an undefeated monster is found
    If a monster with insufficient damage is found while checking the prefix sums, the goal cannot be achieved with that combination, so there is no need to check the remaining monsters.

    Source Code

#include <bits/stdc++.h>
using namespace std;

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    int N, M;
    cin >> N >> M;

    vector<long long> A(N);
    for (auto& a : A) cin >> a;

    struct Scroll {
        int l, r;
        long long c, w;
    };
    vector<Scroll> scrolls(M);
    for (auto& s : scrolls) {
        cin >> s.l >> s.r >> s.c >> s.w;
        --s.l;
    }

    long long answer = LLONG_MAX;
    vector<long long> diff(N + 1);

    for (int mask = 0; mask < (1 << M); ++mask) {
        fill(diff.begin(), diff.end(), 0LL);
        long long cost = 0;

        for (int j = 0; j < M; ++j) {
            if ((mask >> j) & 1) {
                const auto& s = scrolls[j];
                cost += s.w;
                diff[s.l] += s.c;
                diff[s.r] -= s.c;
            }
        }

        if (cost >= answer) continue;

        long long damage = 0;
        bool possible = true;
        for (int i = 0; i < N; ++i) {
            damage += diff[i];
            if (damage < A[i]) {
                possible = false;
                break;
            }
        }

        if (possible) answer = cost;
    }

    cout << (answer == LLONG_MAX ? -1 : answer) << '\n';
    return 0;
}

This editorial was generated by gpt-6-astra-medium.