Codeforces Round #438 by Sberbank and Barcelona Bootcamp (Div. 1 + Div. 2 combined) F. Yet Another Minimization Problem

$f[i][k]$ 表示前 $i$ 个分成 $k$ 段,且最后一段以 $i$ 结尾的最小值

容易写出转移方程 $f[i][k] = \min \{f[j][k - 1] + calc(j+1,i)\}$

因为具有决策单调性(打表 or 证明(不会)),就可以一种分治算法来优化

具体实现就是 $solve(l,r,L,R)$ 表示要求出 $(l,r)$ 之间的 dp 值,而决策点能取的区间为 $[L,R]$

先暴力求出 $mid=\dfrac{l+r}{2}$ 的决策点 $pos$,再调用 $solve(l,mid-1,L,pos)$,$solve(mid+1,r,pos,R)$ 即可。

$calc(i,j)$ 就用类似于莫队的写法来求,均摊之后最后移动 $O(\log n)$ 次。

复杂度为 $O(kn\log n)$

#include <bits/stdc++.h>
using ll = long long;

const int N = 1e5 + 7;
const ll inf = 0x3f3f3f3f3f3f3f3f;
ll dp[2][N], sum;
int cnt[N], n, k, l = 1, r, cur, last, a[N];

void del(int x) {
    sum -= --cnt[a[x]];
}

void add(int x) {
    sum += cnt[a[x]]++;
}

void calc(int x, int y) {
    while (l < x) del(l++);
    while (l > x) add(--l);
    while (r > y) del(r--);
    while (r < y) add(++r);
}

void solve(int l, int r, int L, int R) {
    if (l > r) return;
    int mid = l + r >> 1;
    dp[cur][mid] = inf;
    int pos = 0;
    for (int i = L; i <= std::min(mid, R); i++) {
        calc(i, mid);
        if (dp[cur][mid] > dp[last][i - 1] + sum)
            dp[cur][mid] = dp[last][i - 1] + sum, pos = i;
    }
    solve(l, mid - 1, L, pos); solve(mid + 1, r, pos, R);
}

int main() {
    scanf("%d%d", &n, &k);
    for (int i = 1; i <= n; i++)
        scanf("%d", a + i);
    memset(dp, 0x3f, sizeof(dp));
    dp[0][0] = 0;
    last = 0; cur = 1;
    while (k--) {
        solve(1, n, 1, n);
        std::swap(cur, last);
    }
    printf("%lld\n", dp[last][n]);
    return 0;
}

原文地址:https://www.cnblogs.com/Mrzdtz220/p/12260266.html

时间: 2024-08-03 04:14:27

Codeforces Round #438 by Sberbank and Barcelona Bootcamp (Div. 1 + Div. 2 combined) F. Yet Another Minimization Problem的相关文章

D. Huge Strings Codeforces Round #438 by Sberbank and Barcelona Bootcamp (Div. 1 + Div. 2 combined)

http://codeforces.com/contest/868/problem/D 优化:两个串合并 原有状态+ 第一个串的尾部&第二个串的头部的状态 串变为第一个串的头部&第二个串的尾部 注意: 头尾不能重复 如串A合并串A 这就意味着串的头尾不能有重合, 详见代码 #include <cstdio> #include <cstdlib> #include <cmath> #include <cstring> #include <

[Codeforces Round #438][Codeforces 868D. Huge Strings]

题目链接:868D - Huge Strings 题目大意:有\(n\)个字符串,\(m\)次操作,每次操作把两个字符串拼在一起,并询问这个新串的价值.定义一个新串的价值\(k\)为:最大的\(k\),使得这个新串包含所有长度为\(k\)的01串(这样的字符串有\(2^k\)个) 题解:首先来证明对于任何的串,这个\(k\)的值不会超过9 若\(k=10\),由所有字符串的长度总和不超过100,因此在初始的\(n\)个串里,互不相同的长度为\(k\)的子串不超过100个.又由于每次连接最多能产生

Educational Codeforces Round 21 G. Anthem of Berland(dp+kmp)

题目链接:Educational Codeforces Round 21 G. Anthem of Berland 题意: 给你两个字符串,第一个字符串包含问号,问号可以变成任意字符串. 问你第一个字符串最多包含多少个第二个字符串. 题解: 考虑dp[i][j],表示当前考虑到第一个串的第i位,已经匹配到第二个字符串的第j位. 这样的话复杂度为26*n*m*O(fail). fail可以用kmp进行预处理,将26个字母全部处理出来,这样复杂度就变成了26*n*m. 状态转移看代码(就是一个kmp

Codeforces Round #428 (Div. 2)

Codeforces Round #428 (Div. 2) A    看懂题目意思就知道做了 #include<bits/stdc++.h> using namespace std; #pragma comment(linker, "/STACK:102400000,102400000") #define rep(i,a,b) for (int i=a; i<=b; ++i) #define per(i,b,a) for (int i=b; i>=a; --i

Codeforces Round #424 (Div. 2) D. Office Keys(dp)

题目链接:Codeforces Round #424 (Div. 2) D. Office Keys 题意: 在一条轴上有n个人,和m个钥匙,门在s位置. 现在每个人走单位距离需要单位时间. 每个钥匙只能被一个人拿. 求全部的人拿到钥匙并且走到门的最短时间. 题解: 显然没有交叉的情况,因为如果交叉的话可能不是最优解. 然后考虑dp[i][j]表示第i个人拿了第j把钥匙,然后 dp[i][j]=max(val(i,j),min(dp[i-1][i-1~j]))   val(i,j)表示第i个人拿

Codeforces Round #424 (Div. 2) C. Jury Marks(乱搞)

题目链接:Codeforces Round #424 (Div. 2) C. Jury Marks 题意: 给你一个有n个数序列,现在让你确定一个x,使得x通过挨着加这个序列的每一个数能出现所有给出的k个数. 问合法的x有多少个.题目保证这k个数完全不同. 题解: 显然,要将这n个数求一下前缀和,并且排一下序,这样,能出现的数就可以表示为x+a,x+b,x+c了. 这里 x+a,x+b,x+c是递增的.这里我把这个序列叫做A序列 然后对于给出的k个数,我们也排一下序,这里我把它叫做B序列,如果我

Codeforces Round #400 C 前缀和,思维

ICM Technex 2017 and Codeforces Round #400 (Div. 1 + Div. 2, combined) C. Molly's Chemicals 题意:n个数,问有多少个区间的和是k的次方数,即sum([l, r])=k^x, x>=0. abs(k)<=10. tags:一开始O(n^2)统计,果然炸了.. 这题要在统计到第 i 个数时,看s[i]-k^x是否在前面出现过.因为k指数增长很快,这样就是O(n). // #400 #include<b

[Codeforces] Round #352 (Div. 2)

人生不止眼前的狗血,还有远方的狗带 A题B题一如既往的丝帛题 A题题意:询问按照12345678910111213...的顺序排列下去第n(n<=10^3)个数是多少 题解:打表,输出 1 #include<bits/stdc++.h> 2 using namespace std; 3 int dig[10],A[1005]; 4 int main(){ 5 int aa=0; 6 for(int i=1;;i++){ 7 int x=i,dd=0; 8 while(x)dig[++dd

Codeforces Round #273 (Div. 2)

Codeforces Round #273 (Div. 2) 题目链接 A:签到,仅仅要推断总和是不是5的倍数就可以,注意推断0的情况 B:最大值的情况是每一个集合先放1个,剩下都丢到一个集合去,最小值是尽量平均去分 C:假如3种球从小到大是a, b, c,那么假设(a + b) 2 <= c这个比較明显答案就是a + b了.由于c肯定要剩余了,假设(a + b)2 > c的话,就肯定能构造出最优的(a + b + c) / 3,由于肯定能够先拿a和b去消除c,而且控制a和b成2倍关系或者消除