[HDU 4261] Estimation
[题目链接]
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的更多相关文章
- 【HDOJ】4261 Estimation
挺不错的一道题,基本思路是dp.关键点是如何求区间内的Sigma|A_i-B_i|.线段树做TLE了,优先队列可以过. /* 4261 */ #include <iostream> #in ...
- $2019$ 暑期刷题记录1:(算法竞赛DP练习)
$ 2019 $ 暑期刷题记录: $ POJ~1952~~BUY~LOW, BUY~LOWER: $ (复杂度优化) 题目大意:统计可重序列中最长上升子序列的方案数. 题目很直接的说明了所求为 $ L ...
- hdu 4882 ZCC Loves Codefires(数学题+贪心)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4882 ------------------------------------------------ ...
- HDU 5130 Signal Interference(计算几何 + 模板)
HDU 5130 Signal Interference(计算几何 + 模板) 题目链接http://acm.hdu.edu.cn/showproblem.php?pid=5130 Descripti ...
- D - 淡黄的长裙 HDU - 4221(贪心)
D - 淡黄的长裙 HDU - 4221(贪心) James is almost mad! Currently, he was assigned a lot of works to do, so ma ...
- 萌新笔记——Cardinality Estimation算法学习(一)(了解基数计算的基本概念及回顾求字符串中不重复元素的个数的问题)
最近在菜鸟教程上自学redis.看到Redis HyperLogLog的时候,对"基数"以及其它一些没接触过(或者是忘了)的东西产生了好奇. 于是就去搜了"HyperLo ...
- HDOJ 2111. Saving HDU 贪心 结构体排序
Saving HDU Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total ...
- 【HDU 3037】Saving Beans Lucas定理模板
http://acm.hdu.edu.cn/showproblem.php?pid=3037 Lucas定理模板. 现在才写,noip滚粗前兆QAQ #include<cstdio> #i ...
- hdu 4859 海岸线 Bestcoder Round 1
http://acm.hdu.edu.cn/showproblem.php?pid=4859 题目大意: 在一个矩形周围都是海,这个矩形中有陆地,深海和浅海.浅海是可以填成陆地的. 求最多有多少条方格 ...
随机推荐
- 日期对话框(DatePickerDialog)和时间对话框(TimePickerDialog)
效果图 布局 <Button android:id="@+id/btn_date" android:text="弹出日期选择对话框" android:la ...
- 书不在多,精读则灵 - Oracle入门书籍推荐
作者:eygle |English [转载时请标明出处和作者信息]|[恩墨学院 OCM培训传DBA成功之道]链接:http://www.eygle.com/archives/2006/08/ora ...
- MSP430之software development flow
MSP430 software development flow. 1) The shaded portion highlights the most common development path; ...
- C# tostring("0000000")
public string ConverNo(string str) { string result = ""; ]; ; i < chars.Length; i++) ch ...
- Memcached 之内存管理与删除机制
一.内存的碎片化 如果用c语言直接 malloc,free 来向操作系统申请和释放内存时,在不断的申请和释放过程中,形成了一些很小的内存片断,无法再利用,这种空闲,但无法利用内存的现象称为内存的碎片化 ...
- Java RMI之HelloWorld经典入门案例
Java RMI 指的是远程方法调用 (Remote Method Invocation).它是一种机制,能够让在某个 Java 虚拟机上的对象调用另一个 Java 虚拟机中的对象上的方法.可以用此方 ...
- Python 切片 day3
你可以处理列表的部分元素——Python称之为切片 . 一.使用方法: 要创建切片,可指定要使用的第一个元素和最后一个元素的索引. 与函数range() 一样,Python在到达你指定的第二个索引前面 ...
- eas之新建窗口
public void actionObjectProp_actionPerformed(ActionEvent e) throws Exception { UIContex ...
- vfs:open.c 源码学习
nameidata路径查找辅助结构 open.c @do_sys_open @get_unused_fd_flags @do_filp_open 1.开始填充nameidata 2.开始填充file ...
- Linux下进程与线程的区别
https://www.cnblogs.com/fah936861121/articles/8043187.html https://my.oschina.net/cnyinlinux/blog/36 ...