好写的严格单 做法 /hec。
hint 1
首先 Wilco 没有什么主观性,而 Tom 是可以被操作的。
可以把问题反过来看:计算 Tom 最多能拿走多少颗糖果。
hint 2
分析 Tom 的具体路径:Tom 拿走一颗糖后要把它送到一个空地,且每次只能拿 颗。
那么我们不妨将 Tom 拆成若干个“任务”:从某个商店拿一颗糖果,送到某个空地,然后继续去下一家要偷的商店。
hint 3
分析一下取糖果的顺序:由于 Wilco 是从左到右走的,所以肯定是从左到右的取糖果。
hint 4
考虑分析一下每个任务的代价(即所需要走的路径长度),设商店 到最近的空地的距离为 ,那么似乎取一次糖果的代价为 。但是这真的对吗?
hint 5
事实上,对于商店 取得最后一颗糖果,Tom 并不需要回到商店 ,而是可以去到一个右边空地后,直接走向下一个要取糖果的商店。
设商店 右边的空地为 ,商店 和空地之间最左的商店为 。
如果先送到一个空地,然后再走到右边的商店,我们的代价为 。
而直接走过去,不添加糖果的代价为 。
由于 hint 4 中的策略并没有计算走到下一个商店的代价,我们可以得出这个策略的代价为 。
故我们最后一颗糖果的代价为 。
hint 6
有了这个性质后,我们可以发现每个商店的糖果实际上可以分成两种:
- 颗“最后糖果”:代价为 。
- 颗“普通糖果”:代价为 。
问题变成:从左到右选择尽量多的物品,使得 Tom 来得及在 Wilco 到达对应位置前开始拿这颗糖果。
hint 7
当处理到 时,设前面取糖果花费的代价为 (注意没有加上路径长度)。
那么只有 时,Tom 才可以取糖果。这种问题是不是感觉很熟悉?
hint 8
考虑反悔贪心,用一个大根堆存储之前取得所有糖果。
对于每个可选糖果,直接选;否则若堆顶代价大于当前代价,则弹出堆顶,然后看看能不能再塞一些糖果。
有个小细节,反悔贪心的时候要先贪“普通糖果”,再贪“最后糖果”(不能反过来),不然就不符合上面的结论。
时间复杂度?
预处理部分中,计算 、、、 都只需要对序列做常数次线性扫描,因此复杂度为 。
主循环中需要证明堆操作的总次数是线性的。
注意代码并没有逐颗处理糖果,而是将同一家商店中代价相同的“普通糖果”合并成一组,用(代价,数量)的形式放入堆中。
对于每个商店,至多会向堆中加入:
- 一组普通糖果;
- 一颗最后糖果。
因此如果不考虑反悔时拆分出的剩余部分,堆中新增元素数为 。
接下来考虑当 时的反悔过程。
每次循环要么:
-
当前还能直接选择糖果,即 。 此时程序会一次性加入尽可能多的当前糖果,而不是一颗一颗加入。并且若当前商店的普通糖果还没有处理完,加入后一定有 。 因此这种操作每执行一次,都会使偷到的糖果数增加至少 。
-
当前不能直接选择糖果,需要从堆中弹出一个代价最大的旧糖果组进行替换。 如果整组都被替换掉,那么这个堆元素以后不会再出现。 如果只替换了其中一部分,程序会把剩余部分重新放回堆中;但此时根据 的计算方式,替换后一定重新满足 ,所以在下一次反悔之前,必然又要先发生一次直接加入糖果的操作。
偷到的糖果总数是 的:每个商店至多有一颗“最后糖果”,共 颗;而普通糖果的代价至少为 ,在任意位置 开始拿糖果前都有 ,因此能同时保留的普通糖果数量也是 。
所以直接加入糖果的次数为 ,反悔替换造成的堆弹出、重新插入次数也为 。
堆的大小始终为 ,每次堆操作复杂度为 ,因此主循环总复杂度为 。
加上线性预处理,总时间复杂度为 。
空间上,数组 以及优先队列都只存储 个元素,因此空间复杂度为 。
#include <bits/stdc++.h>
using namespace std;
const int N = 500000;
const int inf = 0x3f3f3f3f;
struct cmp {
bool operator() (pair<int, int> u, pair<int, int> v) {
return u.first < v.first;
};
};
int n, a[N + 10];
int d[N + 10], s[N + 10], nxt[N + 10];
int main() {
ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
cin >> n;
for (int i = 1; i <= n; ++i) cin >> a[i];
long long sum = 0;
for (int i = 1; i <= n; ++i) {
if (a[i] > 0) sum += a[i];
}
int lst = 0;
for (int i = 1; i <= n; ++i) {
if (!lst) d[i] = inf;
else d[i] = i - lst;
if (a[i] == -1) lst = i;
}
lst = 0;
for (int i = n; i >= 1; --i) {
if (lst) d[i] = min(d[i], lst - i);
if (a[i] == -1) lst = i;
}
lst = 0;
for (int i = n; i >= 1; --i) {
if (a[i] == -1) lst = 0;
nxt[i] = lst;
if (a[i] > 0) lst = i;
}
lst = 0;
for (int i = n; i >= 1; --i) {
s[i] = lst;
if (a[i] == -1) lst = i;
}
priority_queue<pair<int, int>, vector<pair<int, int>>, cmp> heap;
int tot = 0, ans = 0;
for (int i = 1; i <= n; ++i) {
if (a[i] <= 0) continue;
int val;
--a[i], val = 2 * d[i]; int add_cnt = 0;
while (a[i] > 0) {
if (tot <= i - 1) {
int cnt = min(a[i], (i - 1 - tot) / val + 1);
ans += cnt, tot += val * cnt, add_cnt += cnt, a[i] -= cnt;
continue;
}
if (heap.empty() || heap.top().first <= val) break;
pair<int, int> tmp = heap.top(); heap.pop();
int diff = tmp.first - val;
int cnt = min({a[i], tmp.second, (tot - i + diff) / diff});
tot -= diff * cnt, add_cnt += cnt, a[i] -= cnt;
if (tmp.second > cnt) heap.push({tmp.first, tmp.second - cnt});
}
if (add_cnt) heap.push({val, add_cnt});
if (!nxt[i]) val = 0;
else if (!s[i]) val = 2 * d[i];
else val = min(2 * d[i], 2 * (s[i] - nxt[i]));
if (tot <= i - 1) {
++ans, tot += val;
heap.push({val, 1});
} else {
if (!heap.empty()) {
pair<int, int> tmp = heap.top(); heap.pop();
if (tmp.first > val) {
tot += val - tmp.first;
heap.push({val, 1});
if (tmp.second > 1) heap.push({tmp.first, tmp.second - 1});
} else heap.push(tmp);
}
}
}
cout << sum - ans;
return 0;
}