一道需要用堆初始化的\(DP\)

原题链接

显然对于每一个部分,当\(b[i]\)为\(a\)对于部分的中位数时,差错最小。设\(S(x,y)\)表示\(x\sim y\)这一部分的差错。

\(DP\)的转移方程应该并不难推。

定义\(f[i][j]\)表示前\(i\)个数字分成\(j\)组导致的差错的最小值。

\(\qquad\qquad f[i][j]=\min\limits_{k=0}^{i-1}\{f[i][j],f[k][j-1]+S(k+1,i)\}\)

如果我们直接暴力计算\(S\),显然会超时,所以我们需要初始化\(S\),且因为在初始化过程中需要用到动态中位数,所以我们采用一个小根堆和一个大根堆来维护。

我是维护小根堆堆顶作为中位数。

先在小根堆中插入第一个数,定为当前中位数。

然后循环扫到下一个数,若该数比当前小根堆堆顶小,则插入大根堆,否则插入小根堆。

而在插入过程中,必须保证小根堆的大小比大根堆大\(1\)或相等,而在循环的过程中,小根堆堆顶即是当前区间内的中位数。

#include<cstdio>
#include<queue>
#include<vector>
#include<cstring>
using namespace std;
const int N = 2010;
const int K = 27;
int f[N][K], a[N], v[N][N];
priority_queue<int>bg;
priority_queue<int, vector<int>, greater<int> >sm;
int re()
{
int x = 0;
char c = getchar();
bool p = 0;
for (; c<'0' || c>'9'; c = getchar())
p = (c == '-' || p) ? 1 : 0;
for (; c >= '0'&&c <= '9'; c = getchar())
x = x * 10 + (c - '0');
return p ? -x : x;
}
inline int minn(int x, int y)
{
return x < y ? x : y;
}
int main()
{
int i, j, n, m, k, s_sm, s_bg;
while (1)
{
n = re();
m = re();
if (!n && !m)
return 0;
for (i = 1; i <= n; i++)
a[i] = re();
for (i = 1; i <= n; i++)
{
while (!sm.empty())
sm.pop();
while (!bg.empty())
bg.pop();
for (j = i + 1, s_sm = a[i], s_bg = 0, sm.push(a[i]); j <= n; j++)
{
if (a[j] < sm.top())
{
bg.push(a[j]);
s_bg += a[j];
}
else
{
sm.push(a[j]);
s_sm += a[j];
}
if (sm.size() > bg.size() + 1)
{
bg.push(k = sm.top());
s_sm -= k;
s_bg += k;
sm.pop();
}
if (bg.size() > sm.size())
{
sm.push(k = bg.top());
s_bg -= k;
s_sm += k;
bg.pop();
}
v[i][j] = s_sm - sm.size()*sm.top() + bg.size()*sm.top() - s_bg;
}
}
memset(f, 60, sizeof(f));
f[0][0] = 0;
for (j = 1; j <= m; j++)
for (i = j; i <= n; i++)
for (k = 0; k < i; k++)
f[i][j] = minn(f[i][j], f[k][j - 1] + v[k + 1][i]);
printf("%d\n", f[n][m]);
}
return 0;
}

HDOJ4261 Estimation的更多相关文章

  1. 萌新笔记——Cardinality Estimation算法学习(一)(了解基数计算的基本概念及回顾求字符串中不重复元素的个数的问题)

    最近在菜鸟教程上自学redis.看到Redis HyperLogLog的时候,对"基数"以及其它一些没接触过(或者是忘了)的东西产生了好奇. 于是就去搜了"HyperLo ...

  2. Noise Contrastive Estimation

    Notes from Notes on Noise Contrastive Estimation and Negative Sampling one sample: \[x_i \to [y_i^0, ...

  3. 手势估计- Hand Pose Estimation

    http://blog.csdn.net/myarrow/article/details/51933651 1. 目前进展 1.1 相关资料      1)HANDS CVPR 2016      2 ...

  4. SQL Server 2014里的针对基数估计的新设计(New Design for Cardinality Estimation)

    对于SQL Server数据库来说,性能一直是一个绕不开的话题.而当我们去分析和研究性能问题时,执行计划又是一个我们一直关注的重点之一. 我们知道,在进行编译时,SQL Server会根据当前的数据库 ...

  5. Click Models for Web Search(2) - Parameter Estimation

    在Click Model中进行参数预估的方法有两种:最大似然(MLE)和期望最大(EM).至于每个click model使用哪种参数预估的方法取决于此model中的随机变量的特性.如果model中的随 ...

  6. 解读Cardinality Estimation<基数估计>算法(第一部分:基本概念)

    基数计数(cardinality counting)是实际应用中一种常见的计算场景,在数据分析.网络监控及数据库优化等领域都有相关需求.精确的基数计数算法由于种种原因,在面对大数据场景时往往力不从心, ...

  7. Time vs Story Points Estimation [转]

    One of the most common questions we get is whether to estimate in time or points. It seems like poin ...

  8. 【Deep Learning学习笔记】Efficient Estimation of Word Representations in Vector Space_google2013

    标题:Efficient Estimation of Word Representations in Vector Space 作者:Tomas Mikolov 发表于:ICLR 2013 主要内容: ...

  9. Comparing randomized search and grid search for hyperparameter estimation

    Comparing randomized search and grid search for hyperparameter estimation Compare randomized search ...

随机推荐

  1. AMQP协议与RabbitMQ、MQ消息队列的应用场景

    什么是AMQP? 在异步通讯中,消息不会立刻到达接收方,而是被存放到一个容器中,当满足一定的条件之后,消息会被容器发送给接收方,这个容器即消息队列,而完成这个功能需要双方和容器以及其中的各个组件遵守统 ...

  2. js高级-函数的四种调用模式

    1.对象方法调用模式  方法内部的this指向当前调用者的对象d 定义类 (构造函数) function Dog (dogName){ //创建一个空对象   让空对象==this this.name ...

  3. php json中文被转义

    php 5.4 json_encode($str, JSON_UNESCAPED_UNICODE); 5.4版本以下 方法一function encode_json($str){ $code = js ...

  4. Eclipse git 冲突合并

    Eclipse有一个git的插件叫EGit,用于实现本地代码和远程代码对比.合并以及提交.但是在本地代码和远程代码有冲突的时候,EGit的处理方案还是有点复杂.今天就彻底把这些步骤给理清楚,并公开让一 ...

  5. Functional and Class Components

    [Functional and Class Components] The simplest way to define a component is to write a JavaScript fu ...

  6. python学习day9 字符编码和文件处理

    1.字符编码 x='上' #unicode的二进制--------->编码-------->gbk格式的二进制 res=x.encode('gbk') #bytes 字节类型 print( ...

  7. 数据库类型空间效率探索(四)-tinyint与enum与set

    mysql> select count(*) from userinfo;+----------+| count(*) |+----------+| 115597 |+----------+1 ...

  8. Attention Please

    关于BJJ与Matlab的学习时间安排在五六日晚间: 其余一切重心在学术!

  9. NumPy 从数值范围创建数组

    NumPy 从数值范围创建数组 这一章节我们将学习如何从数值范围创建数组. numpy.arange numpy 包中的使用 arange 函数创建数值范围并返回 ndarray 对象,函数格式如下: ...

  10. TOJ 4383 n % ( pow( p , 2) ) ===0

    传送门:http://acm.tzc.edu.cn/acmhome/problemdetail.do?&method=showdetail&id=4383 描述 There is a ...