Official

D - 魔物の一掃 / Sweeping Out Monsters Editorial by admin Editorial for printing

Gemini 3.8 Flash (High)

Overview

This problem asks for the minimum magic power consumed by choosing a subset of \(M\) scrolls to reduce the health of all monsters to \(0\) or less. Taking advantage of the fact that the number of scrolls \(M\) is very small (at most \(15\)), we can solve this problem using bitmask brute-force search to check all combinations of scrolls.

Analysis

Key Observation: Small Constraint on \(M\)

While the number of monsters \(N\) is up to \(200\), the number of scrolls \(M\) is very small (\(1 \le M \le 15\)). Since there are only \(2\) choices for each scroll—either “use” or “do not use”—there are only \(2^M\) total combinations of scrolls. Even when \(M = 15\), $\(2^{15} = 32,768\)$ combinations exist, which is a very small number of candidates for a computer to process.

Fast Check: Difference Array (Imos Method)

For a fixed choice of scrolls, we need to determine whether all monsters can be defeated. If we add damage to all monsters in the range \([L_j, R_j]\) for each chosen scroll, each check would take up to \(O(M \times N)\) time.

Therefore, we use a difference array (Imos method): - When using scroll \(j\) (dealing \(C_j\) damage to the range \([L_j, R_j]\)), we add \(+C_j\) to index \(L_j\) and \(-C_j\) to index \(R_j + 1\) in the difference array diff. This takes \(O(1)\) per scroll. - After recording the differences for all scrolls, we can reconstruct the damage received by each monster in \(O(N)\) by computing prefix sums from left to right.

This allows us to quickly check whether a given choice satisfies the condition in \(O(M + N)\) time.

Finding the Minimum Value

We can sort all \(2^M\) combinations in advance in ascending order of total magic power consumed, and then test them starting from the lowest cost. The moment we find the first combination that defeats all monsters, we output that cost and terminate; that cost is guaranteed to be the minimum.

Algorithm

  1. Compute costs for each combination: For each mask from \(0\) to \(2^M - 1\) (representing a choice of scrolls), compute the total consumed magic power costs[mask].
  2. Sort by cost: Sort all masks in ascending order of costs[mask].
  3. Search for a valid mask: Iterate through the masks in order of increasing cost and do the following:
    • Prepare a difference array diff, and for each scroll whose corresponding bit is set in mask (i.e., the scroll is used), perform diff[L] += C and diff[R + 1] -= C.
    • Compute the prefix sums of diff in order, and check whether “damage received \(\ge A_i\)” holds for every monster \(i\).
    • If all monsters are defeated, output that costs[mask] and terminate.
  4. No solution check: If all monsters cannot be defeated after trying all combinations, output -1.

Complexity

  • Time Complexity: \(O(2^M (M + N))\)
    • Sorting the \(2^M\) states takes \(O(M \cdot 2^M)\) time, and verifying whether all monsters are defeated takes \(O(M + N)\) per state using the difference array.
    • Even in the worst case, this requires around \(32,768 \times (15 + 200) \approx 7 \times 10^6\) operations, which easily runs well within the time limit.
  • Space Complexity: \(O(2^M + N)\)
    • Storing the costs and masks for \(2^M\) states requires \(O(2^M)\) space, and the difference array uses \(O(N)\) memory.

Implementation Notes

  • Conversion to 0-indexed: The problem statement uses \(1\)-indexed numbering for monsters and scroll ranges, but converting them to \(0\)-indexed (from \(0\) to \(N-1\)) in the program makes it easier to prevent out-of-bounds array access.

  • Using Bitwise Operations: Whether the \(j\)-th bit of an integer mask is set can be checked with (mask >> j) & 1. This allows us to concisely represent whether scroll \(j\) is used.

    Source Code

import sys


def main():
    input_data = sys.stdin.read().split()
    if not input_data:
        return

    N = int(input_data[0])
    M = int(input_data[1])

    A = [int(x) for x in input_data[2 : 2 + N]]

    idx = 2 + N
    scrolls = []
    for _ in range(M):
        l = int(input_data[idx]) - 1
        r = int(input_data[idx + 1]) - 1
        c = int(input_data[idx + 2])
        w = int(input_data[idx + 3])
        scrolls.append((l, r, c, w))
        idx += 4

    num_masks = 1 << M
    costs = [0] * num_masks
    for j in range(M):
        w = scrolls[j][3]
        bit = 1 << j
        for mask in range(bit):
            costs[mask | bit] = costs[mask] + w

    masks = list(range(num_masks))
    masks.sort(key=lambda m: costs[m])

    for mask in masks:
        diff = [0] * (N + 1)
        for j in range(M):
            if (mask >> j) & 1:
                l, r, c, _ = scrolls[j]
                diff[l] += c
                diff[r + 1] -= c

        cur = 0
        ok = True
        for i in range(N):
            cur += diff[i]
            if cur < A[i]:
                ok = False
                break

        if ok:
            print(costs[mask])
            return

    print(-1)


if __name__ == "__main__":
    main()

This editorial was generated by gemini-3.8-flash-high.

posted:
last update: