D - 魔物の一掃 / Sweeping Out Monsters 解説 by admin
gpt-6-astra-mediumOverview
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
- Initialize the answer
answerwith a sufficiently large value. - Iterate through all integers
masksuch that \(0 \le \text{mask} < 2^M\).- If the \(j\)-th bit from the lowest bit of
maskis \(1\), we consider scroll \(j+1\) to be used.
- If the \(j\)-th bit from the lowest bit of
- For each
mask, perform the following steps:- Initialize the difference array
diffand the consumed magic powercostto \(0\). - For each scroll used, add its consumed magic power to
cost, and update both ends in the difference array. - If
cost >= answer, this combination cannot improve the current best value, so skip the evaluation for this combination. - Compute the prefix sums of
diffto find the total damage dealt to each monster. - If the total damage dealt to all monsters is greater than or equal to their initial HP, update
answerwithcost.
- Initialize the difference array
- 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 longfor 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, onlyL_jis 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\), updatingdiff[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.