codeforces 868F Yet Another Minimization Problem(DP)

 题目链接:https://codeforces.com/contest/868/problem/F

You are given an array of n integers a1... an. The cost of a subsegment is the number of unordered pairs of distinct indices within the subsegment that contain equal elements. Split the given array into k non-intersecting non-empty subsegments so that the sum of their costs is minimum possible. Each element should be present in exactly one subsegment.

Input

The first line contains two integers n and k (2 ≤ n ≤ 105, 2 ≤ k ≤ min (n, 20))  — the length of the array and the number of segments you need to split the array into.

The next line contains n integers a1, a2, ..., an (1 ≤ ai ≤ n) — the elements of the array.

Output

Print single integer: the minimum possible total cost of resulting subsegments.

 

参考思路:https://www.luogu.com.cn/article/v5dzg8s3

题目大意:给一个数组a,要求分成k份,每份的代价 cost = count{ a[i] = a[j] and i < j }。要求最小化k份的代价总和。

 

思路:

朴素的DP思路为,令 dp[step][i] 为前 i 个 a,分成 step 份的最小化代价

那么有 dp[step][i] = min{ dp[step - 1][j - 1] + cost(j, i), j <= i }

时间复杂度为 O(k * n^2)

 

那么怎么优化呢

如图:

image

假设 dp[step][i] 的最佳决策点为 u,对于所有 v < u

有 A + cost(u+1,i) ≤ B + cost(v+1,i)

 

显然,每一份长度越长,代价越高

有 cost(u+1,i+1) ≤ cost(v+1,i+1)

 

联合两条式子,有 A + cost(u+1,i+1) ≤ B + cost(v+1,i+1)

既对于任意 v ,代价都不会比 u 低

所以我们计算位置 i 的时候,根本不用考虑小于 u 的所有决策点

这里可以得出一个结论,我们每一次计算 i,它的最佳决策点 best_pos[i],都会大于等于 best_post[i - 1]

所以说 决策是单调的

 

那么这又有什么用呢

考虑分治的办法

对于区间 [1, n],我们先暴力计算出中点 mid 的值 dp[step][mid],其最佳决策点为 best_pos

那么对于 [1, mid - 1] 的部分,他们的最佳决策点都会小于等于 best_pos;对于 [mid +1, n] 的部分,他们的最佳决策点都会大于等于 best_pos

所以递归的时候,左边只会枚举 [1, best_pos];右边只会枚举 [best_pos, n]

然后对于区间 [l, r],最佳决策点范围为 [low, high],我们先暴力计算出中点 mid 的值 dp[step][mid],其最佳决策点为 best_pos

那么对于 [l, mid - 1] 的部分,他们的最佳决策点都会小于等于 best_pos;对于 [mid +1, r] 的部分,他们的最佳决策点都会大于等于 best_pos

所以递归的时候,左边只会枚举 [low, best_pos];右边只会枚举 [best_pos, high]

所以分治的时候,每次递归,搜索范围会不平均地被分配给左右两个分治,每一层的搜索范围加起来是O(n)的

image

故分治的时间复杂度为 O( knlogn * time_of_calculate_cost )

剩下的问题就是怎么计算 cost 了

 

计算代价 cost 可以考虑尺取发,维护区间 [l, r] 的 cost 值,和每个 a[i] 出现的次数 count[a[i]]

那么当 r 往右移动一位的之后,cost 会增加 a[r+1] 在区间 [l, r] 的出现次数 count[a[r+1]]

左移、l 移动同理

我们就可以 O(1) 地移动 l、r

 

但是我们在上面分治计算的时候,cost 区间一直在移动,这个时间复杂度怎么算呢?

分治的时候每次都是先算完当前区间,再算左子区间,再算右子区间

在左子区间计算的时候,cost维护的区域从父区间来,区间最多移动 O( l - r) 次

在右子区间计算的时候,cost维护的区域从左子区间来,区间还是最多移动 O (l - r) 次

故每次分治,均摊复杂度与分治遍历的位置数相当,为 O( nlogn )

image

 

代码(156 ms):

#include <cstdio>
#include <limits>
#include <algorithm>
#include <memory>
#include <cstring>

typedef long long LL;

constexpr int MAXN = 100005;
constexpr int MAXK = 25;

int a[MAXN];
LL dp[MAXK][MAXN];
int n, k;

int count[MAXN];
int cur_l, cur_r;
LL sum;

LL calc(int target_l, int target_r) {
    while (target_l < cur_l) sum += count[a[--cur_l]]++;
    while (cur_l < target_l) sum -= --count[a[cur_l++]];

    while (target_r < cur_r) sum -= --count[a[cur_r--]];
    while (cur_r < target_r) sum += count[a[++cur_r]]++;

    return sum;
}

void solve(int step, int l, int r, int low, int high) {
    int mid = (l + r) / 2, bast_pos = 0;
    LL res = 0x7f7f7f7f7f7f7f7f;
    for (int i = low; i < std::min(high, mid + 1); ++i) {
        LL v = i > 0 ? dp[step - 1][i - 1] : 0;
        v += calc(i, mid);
        if (res > v) {
            res = v;
            bast_pos = i;
        }
    }
    dp[step][mid] = res;
    if (l < mid)
        solve(step, l, mid, low, bast_pos + 1);
    if (mid + 1 < r)
        solve(step, mid + 1, r, bast_pos, high);
}

int main() {
    scanf("%d%d", &n, &k);
    for (int i = 0; i < n; ++i) {
        scanf("%d", &a[i]);
    }
    memset(dp[0], 0x7f, sizeof(dp[0][0]) * n);

    cur_l = cur_r = 0;
    count[a[0]] = 1;

    for (int i = 1; i <= k; ++i) {
        solve(i, 0, n, 0, n);
    }

    printf("%lld\n", dp[k][n - 1]);
}
View Code

 

posted @ 2026-10-27 23:03  Oyking  阅读(10)  评论(0)    收藏  举报