Estimation
给出一个长度为n序列\(\{a_i\}\),将其划分成连续的K段,对于其中一段\([l,r]\),设其中位数为m,定义其权值为\(\sum_{i=l}^r|m-a_i|\),求最小的权值之和,\(n\leq 2000,K\leq 25\)。
解
显然设\(f[i][j]\)表示前i个数划分成j段的的最小权值和,设\(m(i,j)\)为\(i\sim j\)的作为一段的权值,所以有
\]
边界:\(f[0][0]=0\),其余无限大
答案:\(f[n][K]\)
注意到时间复杂度\(2000^2\times 25=10^8\),为一亿,可以险过,关键在于快速求二元函数m,而求m需要解决的是动态维护一段序列中中间大的数,显然中位数的位置是递增的,可以考虑双堆堆顶优化,不难得知对于序列\(b_1,b_2,...,b_p\)而言设其中位数为\(b_q\),于是有权值为
\]
\]
\]
于是对于权值,我们只要维护这样一个式子即可,步骤如下
- 枚举左端点i,设大根堆为H,小根堆为E
- 初始化\(m(i,i)=0,k=-a_i\),H加入\(a_i\)
- 枚举右端点j
- 如果\(a_j\leq H.top()\),那么\(E.push(H.top()),H.pop(),k+=E.top()\times 2,H.push(a_j),k-=a_j\)
- 否则\(E.push(a_j),k+=a_j\)
- 如果\(i-j+1\)为偶数的话,那么\(H.push(E.top()),E.pop(),k-=H.top()\times 2\)
- 计入答案\(m(i,j)=k+(2q-p)\times H.top()\)
参考代码:
#include <iostream>
#include <cstdio>
#include <queue>
#include <functional>
#include <cstring>
#define il inline
#define ri register
#define intmax 0x7fffffff
using namespace std;
int a[2001],m[2001][2001],dp[2001][26];
priority_queue<int,vector<int>,less<int> >H;
priority_queue<int,vector<int>,greater<int> >E;
il void read(int&);
int main(){
int n,K;
while(read(n),read(K),n&&K){
for(int i(1);i<=n;++i)read(a[i]);
for(int i(1),j,k;i<=n;++i){
while(H.size())H.pop();
while(E.size())E.pop();
H.push(a[i]),k=-a[i];
for(j=i+1;j<=n;++j){
if(a[j]<=H.top())
E.push(H.top()),H.pop(),H.push(a[j]),
k-=a[j],k+=E.top()<<1;
else E.push(a[j]),k+=a[j];
if((j-i+1)&1)k-=E.top()<<1,H.push(E.top()),E.pop();
m[i][j]=k+H.top()*((H.size()<<1)-(j-i+1));
}
}memset(dp,2,sizeof(dp)),dp[0][0]=0;
for(int i,j(1),k;j<=K;++j)
for(i=j;i<=n;++i)
for(k=0;k<i;++k)
if(dp[i][j]>dp[k][j-1]+m[k+1][i])
dp[i][j]=dp[k][j-1]+m[k+1][i];
printf("%d\n",dp[n][K]);
}
return 0;
}
il void read(int &x){
x&=0;ri char c;while(c=getchar(),c==' '||c=='\n'||c=='\r');
ri bool check(false);if(c=='-')check|=true,c=getchar();
while(c>='0'&&c<='9')x=(x<<1)+(x<<3)+(c^48),c=getchar();
if(check)x=-x;
}
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 ...
- 最大似然估计 (Maximum Likelihood Estimation), 交叉熵 (Cross Entropy) 与深度神经网络
最近在看深度学习的"花书" (也就是Ian Goodfellow那本了),第五章机器学习基础部分的解释很精华,对比PRML少了很多复杂的推理,比较适合闲暇的时候翻开看看.今天准备写 ...
随机推荐
- Spring Boot Restful WebAPI集成 OAuth2
系统采用前后端分离的架构,采用OAuth2协议是很自然的事情. 下面开始实战,主要依赖以下两个组件: <dependency> <groupId>org.springframe ...
- 如何发现 Redis 热点 Key ,解决方案有哪些?
Java技术栈 www.javastack.cn 优秀的Java技术公众号 来源:http://t.cn/EAEu4to 一.热点问题产生原因 热点问题产生的原因大致有以下两种: 1.1 用户消费的数 ...
- 看了Google编码规范,我突然有个感觉
那么个编码规范,充分体现了西方人的自我感觉良好,以及以自己为中心的程度, 以及西方人对待事物的双重标准.
- Use on Git
Preface The document is about to introduce some specialties on PLM development and mainte ...
- linux常用命令 满足99%的开发需要
1.# 表示权限用户(如:root),$ 表示普通用户开机提示:Login:输入用户名password:输入口令 用户是系统注册用户成功登陆后,可以进入相应的用户环境.退出当前shell,输入:exi ...
- bean创建
看<spring源码解析>的笔记 1.通过@Bean创建bean,类上需要添加@Configuration @Configuration public class MainConfig { ...
- 用Cygwin实现在window环境下使用Linux命令-nohup 来后台运行程序
1.安装Cygwin 下载 cygdrive-选择64或32位 http://www.cygwin.com/ 注:可以百度搜索安装步骤 2.配置它的环境变量 添加到path路径中 3.cmd 执 ...
- Vue登录登出以及JWT认证
数据模型 主要用户名,密码,邮箱,头像,身份 const mongoose = require('mongoose') const schema = new mongoose.Schema({ use ...
- leetcode-159周赛-5230-缀点成线
自己的提交: class Solution: def checkStraightLine(self, coordinates: List[List[int]]) -> bool: if not ...
- 使用VC6.0编译C++代码的时候报错:fatal error C1071: unexpected end of file found in comment(Mark ZZ)
fatal error C1071: unexpected end of file found in comment(Mark ZZ) 今天在一论坛上看到一人发帖: 『最近遇到一个奇怪的问题,代码中的 ...