D - 魔物の一掃 / Sweeping Out Monsters Editorial by admin
Gemini 3.8 Flash (High)Overview
This problem asks us to defeat all \(N\) monsters by choosing a subset of \(M\) available scrolls that deal range damage, minimizing the total consumed magic power. Focusing on the constraint that the number of scrolls \(M\) is very small (at most \(15\)), we can solve this problem using bitmask brute force.
Analysis
- A greedy approach (such as “using scrolls in order of magic efficiency”) does not yield the correct solution because the optimal combination depends on how the ranges overlap.
- Looking at the constraints, the number of monsters \(N\) is up to \(200\), but the number of scrolls \(M\) is at most \(15\), which is very small.
- For each scroll, there are \(2\) choices: whether to “use” it or “not use” it. Thus, there are \(2^M\) possible combinations of scrolls in total.
- When \(M \le 15\), there are only \(2^{15} = 32{,}768\) combinations. Because the total number of combinations is so small, we can exhaustively check all possible choices (brute-force search).
- For each choice, we check the following:
- Calculate the sum of the magic consumption \(W_j\) of the chosen scrolls.
- Add damage \(C_j\) to the effective range \([L_j, R_j]\) of each chosen scroll to find the total damage received by each monster.
- For all monsters \(i\), check if the total damage received is greater than or equal to their initial health \(A_i\).
- If the condition is satisfied, update the minimum magic consumption.
The time complexity to check a single combination is at most \(M \times N = 15 \times 200 = 3{,}000\) operations even if we naively add damage across the intervals. In total, this requires at most \(32{,}768 \times 3{,}000 \approx 10^8\) simple addition operations, which easily runs well within the time limit.
Algorithm
We use bitmask brute force.
- Any choice of a subset of the \(M\) scrolls can be put into a one-to-one correspondence with the binary representation (bit string) of an integer from \(0\) to \(2^M - 1\).
- For example, when \(M = 3\), the integer 5 (binary 101) represents the state: “use the \(0\)-th and \(2\)-nd scrolls, and do not use the \(1\)-st scroll”.
- We loop an integer mask from \(0\) to \((1 \ll M) - 1\), and simulate using the scrolls corresponding to the bits that are set (1) in mask.
Complexity
- Time Complexity: \(O(2^M \cdot M \cdot N)\)
- There are \(2^M\) possible choices.
- For each choice, we apply at most \(M\) scrolls, and each application adds damage to an interval of length at most \(N\), which takes \(O(M \cdot N)\).
- In the worst case, this requires \(2^{15} \times 15 \times 200 \approx 9.8 \times 10^7\) operations, which executes quickly in languages such as C++ (taking around a few milliseconds to a few tens of milliseconds).
- Space Complexity: \(O(N + M)\)
- This corresponds to the size of the arrays storing monster healths, scroll information, and the accumulated damage to each monster, which easily fits in memory.
Implementation Details
Overflow Prevention:
- The values of \(A_i, W_j, C_j\) can reach up to \(10^9\). Since the total magic power consumed and the total damage dealt can exceed the maximum limit of a 32-bit signed integer (approximately \(2 \times 10^9\)), be sure to use a 64-bit integer type (
long longin C++) for variables storing these values.
- The values of \(A_i, W_j, C_j\) can reach up to \(10^9\). Since the total magic power consumed and the total damage dealt can exceed the maximum limit of a 32-bit signed integer (approximately \(2 \times 10^9\)), be sure to use a 64-bit integer type (
Converting to 0-indexed:
- While indices in the problem statement are \(1\)-indexed (\(1\) to \(N\)), standardizing to \(0\)-indexed (\(0\) to \(N-1\)) in the program makes array access smoother. It is recommended to subtract \(1\) from \(L_j\) and \(R_j\) of each scroll when reading the input.
Handling Unachievable Cases:
Initialize the variable
ansthat tracks the minimum cost to-1, so that if the condition is never satisfied,-1will be output as is.Source Code
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
struct Scroll {
int l, r;
long long c, w;
};
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int n, m;
if (!(cin >> n >> m)) return 0;
vector<long long> a(n);
for (int i = 0; i < n; ++i) {
cin >> a[i];
}
vector<Scroll> scrolls(m);
for (int i = 0; i < m; ++i) {
cin >> scrolls[i].l >> scrolls[i].r >> scrolls[i].c >> scrolls[i].w;
--scrolls[i].l;
--scrolls[i].r;
}
long long ans = -1;
for (int mask = 0; mask < (1 << m); ++mask) {
long long current_cost = 0;
vector<long long> damage(n, 0);
for (int i = 0; i < m; ++i) {
if ((mask >> i) & 1) {
current_cost += scrolls[i].w;
for (int j = scrolls[i].l; j <= scrolls[i].r; ++j) {
damage[j] += scrolls[i].c;
}
}
}
bool ok = true;
for (int i = 0; i < n; ++i) {
if (damage[i] < a[i]) {
ok = false;
break;
}
}
if (ok) {
if (ans == -1 || current_cost < ans) {
ans = current_cost;
}
}
}
cout << ans << "\n";
return 0;
}
This editorial was generated by gemini-3.8-flash-high.