[题目链接]

http://acm.hdu.edu.cn/showproblem.php?pid=4261

[算法]

首先,有一个结论 :

| a[1] - k | + | a[2] - k | + ... + | a[n] - k | 当k取(a[1],a[2], ... , a[n])的中位数时,式子的值最小

考虑动态维护中位数

我们用一个大根堆和一个小根堆,大根堆中存放前[1..N/2](向上取整)小的数,小根堆中存放[N/2 + 1,N]小的数,还需维护两个变量s1和s2,分别为小根堆中所有数的和和大根堆中所有数的和

这样,我们就可以预处理出每一段的最小值

然后,我们用f[i][j]表示前i个数分成j段取得的最小值,有状态转移方程 :

f[i][j]  = min{ f[k][j - 1] + middle( k + 1,i) ) (其中,middle(k + 1,i)表示[k + 1,i]中每个数与中位数的差值和)

答案即为f[n][k]

[代码]

#include<bits/stdc++.h>
using namespace std;
#define MAXN 2010
#define MAXK 30
const int INF = 2e9; int i,j,k,s1,s2,x,y,n,m,middle;
int a[MAXN],sum[MAXN][MAXN];
int f[MAXN][MAXK]; struct Sheap
{
int tot;
int a[MAXN];
inline void clear()
{
tot = ;
}
inline void up(int now)
{
if (now == ) return;
int fa = now >> ;
if (a[now] < a[fa])
{
swap(a[now],a[fa]);
up(fa);
}
}
inline void down(int now)
{
int son = now << ;
if (son > tot) return;
if (son + <= tot && a[son + ] < a[son]) son++;
if (a[son] < a[now])
{
swap(a[son],a[now]);
down(son);
}
}
inline void insert(int x)
{
a[++tot] = x;
up(tot);
}
inline void del()
{
swap(a[],a[tot]);
tot--;
down();
}
inline int getroot()
{
return a[];
}
} S;
struct Bheap
{
int tot;
int a[MAXN];
inline void clear()
{
tot = ;
}
inline void up(int now)
{
if (now == ) return;
int fa = now >> ;
if (a[now] > a[fa])
{
swap(a[now],a[fa]);
up(fa);
}
}
inline void down(int now)
{
int son = now << ;
if (son > tot) return;
if (son + <= tot && a[son + ] > a[son]) son++;
if (a[son] > a[now])
{
swap(a[now],a[son]);
down(son);
}
}
inline void insert(int x)
{
a[++tot] = x;
up(tot);
}
inline void del()
{
swap(a[],a[tot]);
tot--;
down();
}
inline int getroot()
{
return a[];
}
} B; int main()
{ while (scanf("%d%d",&n,&m) && (n || m))
{
for (i = ; i <= n; i++) scanf("%d",&a[i]);
for (i = ; i <= n; i++)
{
B.clear();
S.clear();
sum[i][i] = ;
B.insert(a[i]);
s1 = a[i];
s2 = ;
for (j = i + ; j <= n; j++)
{
if (B.tot <= (j - i) / )
{
B.insert(a[j]);
s1 += a[j];
} else
{
S.insert(a[j]);
s2 += a[j];
}
x = B.getroot();
y = S.getroot();
if (x > y)
{
B.del();
s1 -= x;
S.del();
s2 -= y;
S.insert(x);
s2 += x;
B.insert(y);
s1 += y;
}
middle = B.getroot();
sum[i][j] = middle * B.tot - s1 + s2 - middle * S.tot;
}
}
for (i = ; i <= n; i++)
{
for (j = ; j <= m; j++)
{
f[i][j] = INF;
}
}
for (i = ; i <= n; i++) f[i][] = sum[][i];
for (i = ; i <= n; i++)
{
for (j = ; j <= m; j++)
{
for (k = i - ; k >= ; k--)
{
f[i][j] = min(f[i][j],f[k][j - ] + sum[k + ][i]);
}
}
}
printf("%d\n",f[n][m]);
} return ; }

[HDU 4261] Estimation的更多相关文章

  1. 【HDOJ】4261 Estimation

    挺不错的一道题,基本思路是dp.关键点是如何求区间内的Sigma|A_i-B_i|.线段树做TLE了,优先队列可以过. /* 4261 */ #include <iostream> #in ...

  2. $2019$ 暑期刷题记录1:(算法竞赛DP练习)

    $ 2019 $ 暑期刷题记录: $ POJ~1952~~BUY~LOW, BUY~LOWER: $ (复杂度优化) 题目大意:统计可重序列中最长上升子序列的方案数. 题目很直接的说明了所求为 $ L ...

  3. hdu 4882 ZCC Loves Codefires(数学题+贪心)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4882 ------------------------------------------------ ...

  4. HDU 5130 Signal Interference(计算几何 + 模板)

    HDU 5130 Signal Interference(计算几何 + 模板) 题目链接http://acm.hdu.edu.cn/showproblem.php?pid=5130 Descripti ...

  5. D - 淡黄的长裙 HDU - 4221(贪心)

    D - 淡黄的长裙 HDU - 4221(贪心) James is almost mad! Currently, he was assigned a lot of works to do, so ma ...

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

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

  7. HDOJ 2111. Saving HDU 贪心 结构体排序

    Saving HDU Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total ...

  8. 【HDU 3037】Saving Beans Lucas定理模板

    http://acm.hdu.edu.cn/showproblem.php?pid=3037 Lucas定理模板. 现在才写,noip滚粗前兆QAQ #include<cstdio> #i ...

  9. hdu 4859 海岸线 Bestcoder Round 1

    http://acm.hdu.edu.cn/showproblem.php?pid=4859 题目大意: 在一个矩形周围都是海,这个矩形中有陆地,深海和浅海.浅海是可以填成陆地的. 求最多有多少条方格 ...

随机推荐

  1. jQuery学习笔记之jQuery的Ajax(3)

    jQuery学习笔记之jQuery的Ajax(3) 6.jQuery的Ajax插件 源码地址: https://github.com/iyun/jQueryDemo.git ------------- ...

  2. 百鸡百钱===百马百担====for循环嵌套

    package com.zuoye.test;//百鸡百钱5文钱可以买一只公鸡,3文钱可以买一只母鸡,1文钱可以买3只雏鸡.public class Baiji { public static voi ...

  3. C语言笔记(二)

    注释 编译器会用空格代替代码中原来的注释,并先于预处理指令执行/*…*/ 这种形式的注释不能嵌套只要斜杠(/)和星号(*)之间没有空格,都会被当作注释的开始.例如这样:y = x/*p; \ 是一个接 ...

  4. CNN:Windows下编译使用Caffe和Caffe2

    用于检测的CNN分为基于回归网络的方法和基于区域+CNN网络的方法,其中基于回归网络的方法典型为YOLO9000,可以兼容使用VGG-Net框架.其中基于区域+CNN网络方法,大量使用了Caffe作为 ...

  5. Typeclassopedia

    https://wiki.haskell.org/wikiupload/8/85/TMR-Issue13.pdf By Brent Yorgey, byorgey@gmail.com Original ...

  6. 招银网络面试题、考点、知识点总结(Java岗)

    java基础 全是基础不用多说肯定考的多,尤其是招银 OOP特性/java语言特性:封装.继承.多态 多态具体的表现:多态应用举例.如何调用父类方法(super).重写和重载(重写父类方法的规则.构造 ...

  7. C#第九节课

    try catch using System;using System.Collections.Generic;using System.Linq;using System.Text;using Sy ...

  8. stylus解决移动端1像素线等问题

    引用了yo框架中的_border.scss(用来获取yo框架封装的border)   以及   variables.scss(用来获取媒体查询的规则) border($border-width = 1 ...

  9. ldap 禁止匿名登录(5)

    [root@zabbix1 ~]# cat disable_anon.ldif dn: cn=config changetype: modify add: olcDisallows olcDisall ...

  10. Atcoder ARC 082C/D

    C - Together 传送门:http://arc082.contest.atcoder.jp/tasks/arc082_a 本题是一个数学问题. 有一个长度为n的自然数列a[1..n],对于每一 ...