题解:P15580 [USACO26FEB] Blast Damage P
这是官方题解的 AI 中文翻译
我们将解法分为三个部分:
- 找到操作次数的一个下界。
- 通过提供一个使用该次数操作且最多运行
N 次的解法,证明该下界是可达的。 - 推导
f(N) ,并说明如何修改上述解法以获得满分。
第一部分:下界
设
让我们从所有
这给出了
第二部分:一种构造方法
让我们为第一部分中每一个非零的
关键在于,对于满足
该断言可通过分类讨论证明。对于每个索引
- 如果某个索引标记为 S,则其后续索引上不会标记任何字母。
- 由上述观察可知,不存在两个相邻索引同时标记为 S。
- 一个索引仅当其相邻索引标记为 S 时才可能是敏感的。
- 如果索引
i 和i+3 均标记为 S,则a_{i+2} = 0 ,因为i+1 处无 R 且i+2 处无 S,因此i+1 和i+2 不能同时为敏感索引。
由于类似推理,还存在其他可行的运行顺序。例如,从最大索引向最小索引执行操作,或根据需要交换某些不相交相邻对的顺序。
附注:若存在一个给定
第三部分:一个小的修改
我们断言,当
当
#include <bits/stdc++.h>
using namespace std;
template <class T> using V = vector<T>;
#define all(x) begin(x), end(x)
using ll = long long;
int M;
pair<ll, vector<pair<int, ll>>> get_ops(const vector<ll> &v) {
int N = size(v);
// get op count
vector<int> a(N);
for (int i = 0; i < N; ++i) { // satisfy i-th type 2 constraint by increasing a_{i+1} and a_i
ll remaining = v.at(i) - a.at(i);
if (i > 0) remaining -= a.at(i - 1);
remaining = max(remaining, 0LL);
if (i + 1 < N) {
a.at(i + 1) = min(remaining, v.at(i + 1));
remaining -= a.at(i + 1);
}
a.at(i) += remaining;
}
ll op_count = accumulate(begin(a), end(a), 0LL);
// construction
auto sensitive = [&](int i) {
assert(0 <= i && i < N);
return ((i == 0 ? 0 : a.at(i - 1)) + a.at(i) +
(i + 1 == N ? 0 : a.at(i + 1)) >
v.at(i)) &&
a.at(i);
};
vector<pair<int, ll>> runs;
for (int i = 0; i < N; ++i)
if (sensitive(i)) {
if (i) assert(!sensitive(i - 1));
// sanity check: no two consecutive sensitive indices
runs.push_back({i, a.at(i)});
}
for (int i = 0; i < N; ++i)
if (!sensitive(i) && a.at(i)) { runs.push_back({i, a.at(i)}); }
return {op_count, runs};
}
void solve() {
int N;
cin >> N;
vector<ll> v(N);
for (auto &t : v) cin >> t;
auto ans = get_ops(v);
bool rev = false;
if (N % 2 == 0 && size(ans.second) == N) { // for M=2
rev = true;
reverse(all(v));
ans = get_ops(v);
assert(size(ans.second) < N);
}
const auto &[op_count, runs] = ans;
cout << op_count << "\n";
if (M) {
cout << size(runs) << "\n";
for (auto [i, r] : runs)
cout << (rev ? N - i : i + 1) << " " << r << "\n";
}
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int T;
cin >> T >> M;
while (T--) solve();
}
翻译由 Qwen3-235B-A22B 完成