Codeforces 868F Yet Another Minimization Problem(分治+莫队优化DP)
题目链接 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)的更多相关文章
- Codeforces 868F. Yet Another Minimization Problem【决策单调性优化DP】【分治】【莫队】
LINK 题目大意 给你一个序列分成k段 每一段的代价是满足\((a_i=a_j)\)的无序数对\((i,j)\)的个数 求最小的代价 思路 首先有一个暴力dp的思路是\(dp_{i,k}=min(d ...
- CF868F Yet Another Minimization Problem 分治决策单调性优化DP
题意: 给定一个序列,你要将其分为k段,总的代价为每段的权值之和,求最小代价. 定义一段序列的权值为$\sum_{i = 1}^{n}{\binom{cnt_{i}}{2}}$,其中$cnt_{i}$ ...
- CodeForces 868F Yet Another Minimization Problem(决策单调性优化 + 分治)
题意 给定一个序列 \(\{a_1, a_2, \cdots, a_n\}\),要把它分成恰好 \(k\) 个连续子序列. 每个连续子序列的费用是其中相同元素的对数,求所有划分中的费用之和的最小值. ...
- Codeforces 868F Yet Another Minimization Problem 决策单调性 (看题解)
Yet Another Minimization Problem dp方程我们很容易能得出, f[ i ] = min(g[ j ] + w( j + 1, i )). 然后感觉就根本不能优化. 然后 ...
- Codeforces 868F. Yet Another Minimization Problem
Description 给出一个长度为 \(n\) 的序列,你需要将它分为 \(k\) 段,使得每一段的价值和最小,每一段的价值是这一段内相同的数的个数 题面 Solution 容易想到设 \(f[i ...
- 【CodeForces】868F. Yet Another Minimization Problem
原题链接 题目大意是有N个数,分成K段,每一段的花费是这个数里相同的数的数对个数,要求花费最小 如果只是区间里相同数对个数的话,莫队就够了 而这里是!边单调性优化边莫队(只是类似莫队)!而移动的次数和 ...
- Codeforces 375D - Tree and Queries(dfs序+莫队)
题目链接:http://codeforces.com/contest/351/problem/D 题目大意:n个数,col[i]对应第i个数的颜色,并给你他们之间的树形关系(以1为根),有m次询问,每 ...
- Codeforces 940F Machine Learning (带修改莫队)
题目链接 Codeforces Round #466 (Div. 2) Problem F 题意 给定一列数和若干个询问,每一次询问要求集合$\left\{c_{0}, c_{1}, c_{2}, ...
- Codeforces 700D - Huffman Coding on Segment(莫队+根分)
Codeforces 题目传送门 & 洛谷题目传送门 好家伙,刚拿到此题时我连啥是 huffman 编码都不知道 一种对 \(k\) 个字符进行的 huffman 编码的方案可以看作一个由 \ ...
随机推荐
- leetcode-17-BST
530. Minimum Absolute Difference in BST Given a binary search tree with non-negative values, find th ...
- python数据类型之字典(dict)和其常用方法
字典的特征: key-value结构key必须可hash,且必须为不可变数据类型.必须唯一. # hash值都是数字,可以用类似于2分法(但比2分法厉害的多的方法)找.可存放任意多个值.可修改.可以不 ...
- TTL与COMS的区别
1.电平的上限和下限定义不一样,CMOS具有更大的抗噪区域. 同是5伏供电的话,ttl一般是1.7V和3.5V的样子,CMOS一般是 2.2V,2.9V的样子,不准确,仅供参考. 2.电流驱动能力不 ...
- HDU 3394 双连通分量 桥 Railway
第一个答案是统计图中桥的个数 如果一个点-双连通分量中边的个数大于点的个数那么这个块中所有的边都是冲突的,累加到第二个答案中去. #include <iostream> #include ...
- python - 读取配置文件
# -*- coding:utf-8 -*- ''' @project: jiaxy @author: Jimmy @file: read_config.py @ide: PyCharm Commun ...
- Python学习-day5 常用模块
day5主要是各种常用模块的学习 time &datetime模块 random os sys shutil json & picle shelve xml处理 yaml处理 conf ...
- TensorFlow——零碎语法知识点
张量(tensor) 张量 是图中计算节点的运算结果的引用,由name,shape,type三个属性构成. 张量的命名 可以以“name:src_output”的形式给出,node为计算节点名称,sr ...
- [错误解决]UnicodeEncodeError: 'ascii' codec can't encode characters in position 0-3: ordinal not in range(128)
python2内容无法写入csv,报错:UnicodeEncodeError: 'ascii' codec can't encode characters in position 0-3: ordin ...
- 【bzoj2111】[ZJOI2010]Perm 排列计数 dp+Lucas定理
题目描述 称一个1,2,...,N的排列P1,P2...,Pn是Mogic的,当且仅当2<=i<=N时,Pi>Pi/2. 计算1,2,...N的排列中有多少是Mogic的,答案可能很 ...
- ie7中position:fixed定位后导致margin:0 auto;无效
布局网页时,需要把header固定在上方.直接使用position:fixed;现代浏览器以及ie8以上均正常显示,但是ie7中,header里面的子元素设置的水平居中并没有效果.做了各种尝试,都没有 ...