一道需要用堆初始化的\(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. spring-boot-starter-data-elasticsearch 整合elasticsearch 5.x详解

    1.使用原因 近期公司在开发新的项目用到了elasticsearch ,因为项目框架用的spring Cloud所以依赖全用的是starter,从网上找的信息比较旧,并没有整合elasticsearc ...

  2. linus上运行jar包文件增删查

    package com.osplat.util; import com.alibaba.fastjson.JSON; import com.osplat.bean.Resultmodel; impor ...

  3. MVC中html编码与否

    MVCHtmlString可以不进行编码. 例子:string s=‘<a style="color:red">链接</a>’: 1.@s,直接输出< ...

  4. Android笔记:ContextMenu

    ContextMenu,称为上下文菜单,也就是长按界面不放,弹出的菜单.使用ContextMenu有三个步骤: (1)调用registerForContextMenu()方法,为视图注册上下文菜单: ...

  5. js异步导致的错误

    没想到jquery的$.each也是异步,本身是循环验证数据,然后再提交数据,但是发现验证和提交一起发生了. 技术还不到位,所以 在定义了一个变量,var step = 0; 每循环一次step自增, ...

  6. vue通过ID(参数)修改URL复用同一个页面(组件)不重新加载的问题

    项目中经常会用到同一个页面,结构是相同的,我只是在vue-router中通过添加参数的方式来区分状态,参数可以在页面跳转时带上params,或者query,但是有一个问题,即使我们修改了参数,URL也 ...

  7. perl-基础

    1.关系运算符 数字: == != < <= > >= 字符串: eq ne lt le  gt   ge 2.循环 循环:while(){}   for(){}   last ...

  8. JAVA去重

    JAVA中去掉空格 1. String.trim() trim()是去掉首尾空格 2.str.replace(" ", ""); 去掉所有空格,包括首尾.中间 ...

  9. unity缓动插件DOTween Pro v0.9.680

    DoTween Pro是一款unity插件,是unity中最好用的tween插件,比起Dotween的免费版要多很多功能,实现脚本和视觉脚本的新功能,支持包括移动,淡出,颜色,旋转,缩放,打孔,摇动, ...

  10. vue 使用scss

    使用vue-cli模板创建的项目中,使用scss步骤 1. cmd命令: cnpm  install sass-loader --save-dev cnpm install node-sass --s ...