题目链接  Yet Another Minimization Problem

题意  给定一个序列,现在要把这个序列分成k个连续的连续子序列。求每个连续子序列价值和的最小值。

设$f[i][j]$为前$i$个数分成$j$段的最优解

不难得出状态转移方程$f[i][j] = min(f[k][j - 1], calc(j + i, i))$

该DP具有决策单调性

即若$f[i][j]$是从$f[x][j - 1]$转移到的,$f[i+1][j]$是从$f[y][j - 1]$转移到的,那么一定有$x <= y$。

考虑到这一点就可以用分治优化。

还有一点就是$calc()$的计算。

用莫队计算就可以了(分治的时候同一个递归状态下莫队查询端点的改变都是连续的)

时间复杂度$O(nklogn)$

#include <bits/stdc++.h>

using namespace std;

#define rep(i, a, b)	for (int i(a); i <= (b); ++i)
#define dec(i, a, b) for (int i(a); i >= (b); --i) typedef long long LL; const int N = 1e5 + 10;
const int A = 22; LL f[N][A], ret;
int a[N], cnt[N];
int n, m, l, r; void query_init(){
memset(cnt, 0, sizeof cnt);
l = 1, r = 0;
ret = 0;
} LL query(int ql, int qr){
while (r < qr){
++r;
ret += 1ll * cnt[a[r]];
++cnt[a[r]];
} while (r > qr){
--cnt[a[r]];
ret -= 1ll * cnt[a[r]];
--r;
} while (l > ql){
--l;
ret += 1ll * cnt[a[l]];
++cnt[a[l]];
} while (l < ql){
--cnt[a[l]];
ret -= 1ll * cnt[a[l]];
++l;
} return ret;
} void solve(int j, int l, int r, int st, int ed){
if (l > r) return;
int mid = (l + r) >> 1;
int x; rep(i, st, min(mid, ed)){
LL now = query(i, mid);
if (f[i - 1][j - 1] + now <= f[mid][j]){
f[mid][j] = f[i - 1][j - 1] + now;
x = i;
}
} if (l != r){
solve(j, l, mid - 1, st, x);
solve(j, mid + 1, r, x, ed);
}
} int main(){ scanf("%d%d", &n, &m);
rep(i, 1, n) scanf("%d", a + i); query_init(); rep(i, 1, n) rep(j, 0, m) f[i][j] = 1e18;
rep(i, 1, n) f[i][1] = query(1, i);
rep(j, 2, m) solve(j, 1, n, 1, n); printf("%lld\n", f[n][m]);
return 0;
}

Codeforces 868F Yet Another Minimization Problem(分治+莫队优化DP)的更多相关文章

  1. Codeforces 868F. Yet Another Minimization Problem【决策单调性优化DP】【分治】【莫队】

    LINK 题目大意 给你一个序列分成k段 每一段的代价是满足\((a_i=a_j)\)的无序数对\((i,j)\)的个数 求最小的代价 思路 首先有一个暴力dp的思路是\(dp_{i,k}=min(d ...

  2. CF868F Yet Another Minimization Problem 分治决策单调性优化DP

    题意: 给定一个序列,你要将其分为k段,总的代价为每段的权值之和,求最小代价. 定义一段序列的权值为$\sum_{i = 1}^{n}{\binom{cnt_{i}}{2}}$,其中$cnt_{i}$ ...

  3. CodeForces 868F Yet Another Minimization Problem(决策单调性优化 + 分治)

    题意 给定一个序列 \(\{a_1, a_2, \cdots, a_n\}\),要把它分成恰好 \(k\) 个连续子序列. 每个连续子序列的费用是其中相同元素的对数,求所有划分中的费用之和的最小值. ...

  4. Codeforces 868F Yet Another Minimization Problem 决策单调性 (看题解)

    Yet Another Minimization Problem dp方程我们很容易能得出, f[ i ] = min(g[ j ] + w( j + 1, i )). 然后感觉就根本不能优化. 然后 ...

  5. Codeforces 868F. Yet Another Minimization Problem

    Description 给出一个长度为 \(n\) 的序列,你需要将它分为 \(k\) 段,使得每一段的价值和最小,每一段的价值是这一段内相同的数的个数 题面 Solution 容易想到设 \(f[i ...

  6. 【CodeForces】868F. Yet Another Minimization Problem

    原题链接 题目大意是有N个数,分成K段,每一段的花费是这个数里相同的数的数对个数,要求花费最小 如果只是区间里相同数对个数的话,莫队就够了 而这里是!边单调性优化边莫队(只是类似莫队)!而移动的次数和 ...

  7. Codeforces 375D - Tree and Queries(dfs序+莫队)

    题目链接:http://codeforces.com/contest/351/problem/D 题目大意:n个数,col[i]对应第i个数的颜色,并给你他们之间的树形关系(以1为根),有m次询问,每 ...

  8. Codeforces 940F Machine Learning (带修改莫队)

    题目链接  Codeforces Round #466 (Div. 2) Problem F 题意  给定一列数和若干个询问,每一次询问要求集合$\left\{c_{0}, c_{1}, c_{2}, ...

  9. Codeforces 700D - Huffman Coding on Segment(莫队+根分)

    Codeforces 题目传送门 & 洛谷题目传送门 好家伙,刚拿到此题时我连啥是 huffman 编码都不知道 一种对 \(k\) 个字符进行的 huffman 编码的方案可以看作一个由 \ ...

随机推荐

  1. 用Python设置matplotlib.plot的坐标轴刻度间隔以及刻度范围

    一.用默认设置绘制折线图 import matplotlib.pyplot as plt x_values=list(range(11)) #x轴的数字是0到10这11个整数 y_values=[x* ...

  2. python中字符串的一些用法

    一.字符串的拼接:      a=‘123’      b=‘abc’       d=‘hello world’ 1.print(a+b) 2.print(a,b) 3. c=‘ ’.join((a ...

  3. Applied Nonparametric Statistics-lec7

    Ref: https://onlinecourses.science.psu.edu/stat464/print/book/export/html/9 经过前面的步骤,我们已经可以判断几个样本之间是否 ...

  4. arm的开发工具

    网上有free的ide可以开发arm cortex的芯片,可以参考List of ARM Cortex-M development tools,Wikipedia,里面有emIDE,embitz等,虽 ...

  5. MVC&JQuery如何根据List动态生成表格

    背景:在编码中,常会遇到根据Ajax的结果动态生成Table的情况,本篇进行简要的说明.这已经是我第4.5篇和Ajax有关的随笔了,互相之间有很多交叠的地方,可自行参考. 后台代码如下: public ...

  6. vagrant 安装ubuntu12.04 64 bit

    1 下载用于ubuntu 12.04 用于vagrant的镜像,虚拟机是virtualbox $ wget http://files.vagrantup.com/precise64.box jb@e3 ...

  7. Linux和 Mac下git pull/push 免输入密码和账号

    linux下面可以直接创建.git-credential文件,命令如下: 创建文件,进入文件,输入内容: cd ~ touch .git-credentials vim .git-credential ...

  8. HDU 4565 So Easy! 矩阵快速幂

    题意: 求\(S_n=\left \lceil (a+\sqrt{b})^n \right \rceil mod \, m\)的值. 分析: 设\((a+\sqrt{b})^n=A_n+B_n \sq ...

  9. .NET开发时让人头痛的SESSION超时

    前言 不知道大家在使用用.NET的SESSION的时候有没有遇到过很奇怪的问题,不时候不知道怎么回事,这个SESSION就无缘无故的丢失了 怎么也想不通,不是说SESSION很可靠的吗?这个问题要好好 ...

  10. provider:命名管道提供程序,error:40 - 无法打开到SQL Server的连接 (Microsoft

    最近一直在配置服务器, 这当中最头疼的就是配置数据库 我们用的是SQL Server 数据库 2008 版本,数据库配置完之后从另一台电脑访问数据库死活连接不上,提示信息如下 " 无法连接到 ...