HDOJ4261 Estimation
一道需要用堆初始化的\(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的更多相关文章
- 萌新笔记——Cardinality Estimation算法学习(一)(了解基数计算的基本概念及回顾求字符串中不重复元素的个数的问题)
最近在菜鸟教程上自学redis.看到Redis HyperLogLog的时候,对"基数"以及其它一些没接触过(或者是忘了)的东西产生了好奇. 于是就去搜了"HyperLo ...
- Noise Contrastive Estimation
Notes from Notes on Noise Contrastive Estimation and Negative Sampling one sample: \[x_i \to [y_i^0, ...
- 手势估计- Hand Pose Estimation
http://blog.csdn.net/myarrow/article/details/51933651 1. 目前进展 1.1 相关资料 1)HANDS CVPR 2016 2 ...
- SQL Server 2014里的针对基数估计的新设计(New Design for Cardinality Estimation)
对于SQL Server数据库来说,性能一直是一个绕不开的话题.而当我们去分析和研究性能问题时,执行计划又是一个我们一直关注的重点之一. 我们知道,在进行编译时,SQL Server会根据当前的数据库 ...
- Click Models for Web Search(2) - Parameter Estimation
在Click Model中进行参数预估的方法有两种:最大似然(MLE)和期望最大(EM).至于每个click model使用哪种参数预估的方法取决于此model中的随机变量的特性.如果model中的随 ...
- 解读Cardinality Estimation<基数估计>算法(第一部分:基本概念)
基数计数(cardinality counting)是实际应用中一种常见的计算场景,在数据分析.网络监控及数据库优化等领域都有相关需求.精确的基数计数算法由于种种原因,在面对大数据场景时往往力不从心, ...
- 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 ...
- 【Deep Learning学习笔记】Efficient Estimation of Word Representations in Vector Space_google2013
标题:Efficient Estimation of Word Representations in Vector Space 作者:Tomas Mikolov 发表于:ICLR 2013 主要内容: ...
- Comparing randomized search and grid search for hyperparameter estimation
Comparing randomized search and grid search for hyperparameter estimation Compare randomized search ...
随机推荐
- 03_java基础(三)之第一个程序与开发工具的安装
1.第一个Java程序 ① 找一个特定的放代码的地方 ② 新建一个文本文档 --> 改名 Hello.java 注意 : 必须先把文件的后缀名都显示出来 不同的操作系 ...
- Centos7 安装 erlang rabbitmq
1.安装Erlang依赖采用官网的rpm包的形式进行安装,不采用yum(由系统进行自动安装 可能因为版本低的问题而出现一系列问题) erlang依赖 rpm包下载地址https://github.co ...
- uuid生成工具类
public class UUIDTool { public static String getUUID() { return UUID.randomUUID().toString().replace ...
- pip安装离线包
离线包从pypi.org下载 pip download -r requirements.txt -d /tmp/paks/ 在linux下 1.下载指定的包到指定文件夹. ...
- Java读取文件-BufferedReader/FileReader/InputStreamReader/FileInputStream的关系和区别
一.Java读取和存储文件数据流 Java读取文件,实际是将文件中的字节流转换成字符流输出到屏幕的过程 这里面涉及到两个类:InputStreamReader和OutputStreamWriter ...
- 【scrapy】关于爬取的内容是Unicode编码
自己练习爬取拉钩网信息的时候爬取的信息如下: {'jobClass': [u'\u9500\u552e\u52a9\u7406'], 'jobUrl': u'https://www.lagou.com ...
- java面试题:jvm
jvm内存区域 Q:jvm内存怎么划分的? 答: 方法区(线程共享):各个线程共享的一个区域,用于存储虚拟机加载的类信息.常量.静态变量.即时编译器编译后的代码等数据.虽然 Java 虚拟机规范把方法 ...
- dedecms list 添加自定义字段方法
在内容模型管理中,添加字段时需这样:
- JAVA jar 参数
-client to select the "client" VM -server to select the "server" ...
- 问题1:canvas绘制图片加载不出来
<head> <script src="js/index.js" type="text/javascript" charset="u ...