[题目链接]

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. Django学习案例一(blog):四. 使用Admin

    1. 创建超级用户 python manage.py createsuperuser 创建过程中输入用户名,并设定密码(记住). 后台管理汉化.修改settings.py中LANGUAGE_CODE ...

  2. 【Oracle】redo与undo

    一 .redo(重做信息) 是Oracle在线(或归档)重做日志文件中记录的信息,万一出现失败时可以利用这些数据来“重放”(或重做)事务.Oracle中记录这些信息的文件叫做redo log file ...

  3. DNN结构演进History—CNN-GoogLeNet :Going Deeper with Convolutions

    抄袭了一片文章,进行少量修改:http://www.gageet.com/2014/09203.php       作者:Christian Szegedy( google )  刘伟(北卡罗来纳  ...

  4. Webpack 快速上手(下)

    杏仁前端开发工程师,代码洁癖症早期,关注前端技术. 由于文章篇幅较长,为了更好的阅读体验,本文分为上.中.下三篇: 上篇介绍了什么是 webpack,为什么需要 webpack,webpack 的文件 ...

  5. 【转载】Java IO基础总结

    Java中使用IO(输入输出)来读取和写入,读写设备上的数据.硬盘文件.内存.键盘......,根据数据的走向可分为输入流和输出流,这个走向是以内存为基准的,即往内存中读数据是输入流,从内存中往外写是 ...

  6. 【剑指Offer】52、正则表达式匹配

      题目描述:   请实现一个函数用来匹配包括'.'和'*'的正则表达式.模式中的字符'.'表示任意一个字符,而'*'表示它前面的字符可以出现任意次(包含0次). 在本题中,匹配是指字符串的所有字符匹 ...

  7. 【剑指Offer】 24、二叉树中和为某一值的路径

      题目描述:   输入一颗二叉树的根结点和一个整数,打印出二叉树中结点值的和为输入整数的所有路径.路径定义为从树的根结点开始往下一直到叶结点所经过的结点形成一条路径.(注意: 在返回值的list中, ...

  8. 15.3 Task 异常

    1. 在等待时拆包异常 在等待任务时,任务出错或取消都将抛出异常,但并不是 AggregateException .大多情 况下为方便起见,抛出的是 AggregateException 中的第一个异 ...

  9. [frontend] 根据文字长度 自适应宽度 自适应高度+ Uncaught ReferenceError: xxx is not defined at HTMLDivElement.onclick

    CSS一行代码就可以解决第一个问题: 1.1 根据文字长度,自适应标签宽度 解决方法:把width的设置删掉,加一行代码 display:table; .tag-footdetail{  /*widt ...

  10. css画圆

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <meta name ...