D. Blocking Elements
D. Blocking Elements
You are given an array of numbers . Your task is to block some elements of the array in order to minimize its cost. Suppose you block the elements with indices . Then the cost of the array is calculated as the maximum of:
- the sum of the blocked elements, i.e., .
- the maximum sum of the segments into which the array is divided when the blocked elements are removed. That is, the maximum sum of the following () subarrays: [], [], [], [], [] (the sum of numbers in a subarray of the form [] is considered to be ).
For example, if , the original array is [], and you block the elements at positions and , then the cost of the array will be the maximum of the sum of the blocked elements () and the sums of the subarrays (, , ), which is .
You need to output the minimum cost of the array after blocking.
Input
The first line of the input contains a single integer () — the number of queries.
Each test case consists of two lines. The first line contains an integer () — the length of the array . The second line contains elements () — the array .
It is guaranteed that the sum of over all test cases does not exceed .
Output
For each test case, output a single number — the minimum cost of blocking the array.
Example
input
3
6
1 4 5 3 3 2
5
1 2 3 4 5
6
4 1 6 3 10 7
output
7
5
11
Note
The first test case matches with the array from the statement. To obtain a cost of , you need to block the elements at positions and . In this case, the cost of the array is calculated as the maximum of:
- the sum of the blocked elements, which is .
- the maximum sum of the segments into which the array is divided when the blocked elements are removed, i.e., the maximum of , , .
So the cost is .
In the second test case, you can block the elements at positions and .
In the third test case, to obtain the answer , you can block the elements at positions and . There are other ways to get this answer, for example, blocking positions and .
解题思路
显然答案具有二段性,即如果存在一种合法方案使得分割点总和以及每个分割段的和都不超过 ,那么大于 的值也可以作为一个合法的答案,为此需要二分出最小的 。
对于二分值 则需要判断是否存在合法的分割方案。由于选择分割点相当于选择总和不超过 的子序列,所以可以尝试 dp 找到使得每个分割段的和都不超过 时分割点总和的最小值是多少,如果不超过 则说明存在分割方案。
定义 表示将前 个元素进行分割,且分割点总和以及每个分割段的和都不超过 的所有方案中,分割点总和的最小值。根据第 个元素是否为分割点进行状态划分:
- 如果 是分割点,那么状态转移方程为 。
- 如果 不是分割点,那么找到前一个分割点 ,要求满足最后一个分割段的和不超过 ,即 ,其中 表示前缀和。状态转移方程为 。
其中定义 ,。
显然第二个部分需要优化,注意到本质就是在 中找到所有满足 的 ,然后对 取最小值。所以可以用值域线段树来优化,在每个 处存储 并维护最小值,询问就相当于求以 为左端点的后缀的最小值。需要对 和 离散化。
最后如果 则说明存在分割方案。
AC 代码如下,时间复杂度为 :
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
const int N = 2e5 + 10;
const LL INF = 0x3f3f3f3f3f3f3f3f;
int n;
int a[N];
LL s[N];
LL xs[N], sz;
LL f[N];
struct Node {
int l, r;
LL mn;
}tr[N * 4];
int find(LL x) {
int l = 1, r = sz;
while (l < r) {
int mid = l + r >> 1;
if (xs[mid] >= x) r = mid;
else l = mid + 1;
}
return l;
}
void build(int u, int l, int r) {
tr[u] = {l, r, INF};
if (l != r) {
int mid = l + r >> 1;
build(u << 1, l, mid);
build(u << 1 | 1, mid + 1, r);
}
}
void modify(int u, int x, LL c) {
if (tr[u].l == tr[u].r) {
tr[u].mn = min(tr[u].mn, c);
}
else {
int mid = tr[u].l + tr[u].r >> 1;
if (x <= mid) modify(u << 1, x, c);
else modify(u << 1 | 1, x, c);
tr[u].mn = min(tr[u << 1].mn, tr[u << 1 | 1].mn);
}
}
LL query(int u, int l, int r) {
if (tr[u].l >= l && tr[u].r <= r) return tr[u].mn;
int mid = tr[u].l + tr[u].r >> 1;
LL ret = INF;
if (l <= mid) ret = query(u << 1, l, r);
if (r >= mid + 1) ret = min(ret, query(u << 1 | 1, l, r));
return ret;
}
bool check(LL mid) {
sz = 0;
for (int i = 0; i <= n; i++) {
xs[++sz] = s[i];
xs[++sz] = s[i] - mid;
}
sort(xs + 1, xs + sz + 1);
sz = unique(xs + 1, xs + sz + 1) - xs - 1;
build(1, 1, sz);
memset(f, 0x3f, n + 10 << 3);
f[0] = 0;
for (int i = 1; i <= n; i++) {
modify(1, find(s[i - 1]), f[max(0, i - 2)] + a[i - 1]);
f[i] = min(f[i - 1] + a[i], query(1, find(s[i] - mid), sz));
}
return f[n] <= mid;
}
void solve() {
scanf("%d", &n);
for (int i = 1; i <= n; i++) {
scanf("%d", a + i);
s[i] = s[i - 1] + a[i];
}
LL l = 1, r = 1e14;
while (l < r) {
LL mid = l + r >> 1;
if (check(mid)) r = mid;
else l = mid + 1;
}
printf("%lld\n", l);
}
int main() {
int t;
scanf("%d", &t);
while (t--) {
solve();
}
return 0;
}
比赛的时候我是用上面的做法过的,还可以用单调队列来优化。注意到 中的 ,随着 的增加 也会增加,因此 也会增加,具有单调性。因此可以用单调队列来维护 中 的最小值。当枚举到 ,如果对头元素对应的 则弹出。
AC 代码如下,时间复杂度为 :
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
const int N = 2e5 + 10;
int n;
int a[N];
LL s[N];
LL f[N];
int q[N], hh, tt;
bool check(LL mid) {
memset(f, 0x3f, n + 10 << 3);
f[0] = 0;
hh = 0, tt = -1;
for (int i = 1; i <= n; i++) {
// 把f(j-1)+a[j]加入单调队列中
while (hh <= tt && f[max(0, q[tt] - 1)] + a[q[tt]] >= f[max(0, i - 2)] + a[i - 1]) {
tt--;
}
q[++tt] = i - 1; // 队列中存的是下标
while (hh <= tt && s[q[hh]] < s[i] - mid) { // 把队头不满足s[j]>=s[i]-mid的元素删除
hh++;
}
if (hh <= tt) f[i] = min(f[i - 1] + a[i], f[max(0, q[hh] - 1)] + a[q[hh]]);
}
return f[n] <= mid;
}
void solve() {
scanf("%d", &n);
for (int i = 1; i <= n; i++) {
scanf("%d", a + i);
s[i] = s[i - 1] + a[i];
}
LL l = 1, r = 1e14;
while (l < r) {
LL mid = l + r >> 1;
if (check(mid)) r = mid;
else l = mid + 1;
}
printf("%lld\n", l);
}
int main() {
int t;
scanf("%d", &t);
while (t--) {
solve();
}
return 0;
}
参考资料
Codeforces Round #922 (Div. 2) Editorial:https://codeforces.com/blog/entry/125300%E3%80%81
本文来自博客园,作者:onlyblues,转载请注明原文链接:https://www.cnblogs.com/onlyblues/p/17999932
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 单线程的Redis速度为什么快?
· 展开说说关于C#中ORM框架的用法!
· Pantheons:用 TypeScript 打造主流大模型对话的一站式集成库
· SQL Server 2025 AI相关能力初探
· 为什么 退出登录 或 修改密码 无法使 token 失效
2023-01-31 C. Remove the Bracket