[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 题目大意: 在一个矩形周围都是海,这个矩形中有陆地,深海和浅海.浅海是可以填成陆地的. 求最多有多少条方格 ...
随机推荐
- Java接口中的成员变量为什么必须声明为public static final?
我想对于每个Java程序员来说,接口都不陌生,接口中的方法也经常使用.而接口中的成员变量,就显得用得少一点,而对于成员变量为什么必须声明为public static final,可能就更不清楚了,而且 ...
- 【JAVA练习】- 给定精度求圆周率π
给定一个精度求圆周率π的近似值 给定公式:π/4=1-1/3+1/5-1/7+1/9-... public static void main(String[] args) { System.out.p ...
- 【SQL】结构化查询语言
一:数据查询语言(DQL:Data Query Language): 其语句,也称为“数据检索语句”,用以从表中获得数据,确定数据怎样在应用程序给出.保留字SELECT是DQL(也是所有SQL)用得最 ...
- 简单的UIButton按钮动画效果iOS源码
这个是简单的UIButton按钮动画效果案例,源码,简单的UIButton按钮动画,可以自定义button属性. 效果图: <ignore_js_op> 使用方法: 使用时把ButtonA ...
- AI:IPPR的数学表示-CNN可视化语义分析
前言: ANN是个语义黑箱的意思是没有通用明确的函数表示,参数化的模型并不能给出函数的形式,更进而不能表示函数的实际意义. 而CNN在图像处理方面具有天然的理论优势,而Conv层和Polling层,整 ...
- 安卓代码迁移:Make.exe: *** [libs/armabi-v7a/gdbserver] Error 1
解决办法1:安装ndk和eclipse修改为x86操作系统 解决办法2:降低更换NDK版本
- 解决Cannot change version of project facet Dynamic Web M 3.0
解决Cannot change version of project facet Dynamic Web M 3.0 dynamic web module 版本之间的区别: Servlet 3.0 D ...
- 微信小程序 PDF下载打印
在开发微信小程序时,需要打印生成的PDF,实现思路是:后端生成相应的PDF,微信小程序下载并打开. 但是微信小程序并不可以打印,所以需要借助其他APP比如:WPS,但是发现微信小程序down的PDF在 ...
- 洛谷P1208 [USACO1.3]混合牛奶 Mixing Milk【贪心+背包】
由于乳制品产业利润很低,所以降低原材料(牛奶)价格就变得十分重要.帮助Marry乳业找到最优的牛奶采购方案. Marry乳业从一些奶农手中采购牛奶,并且每一位奶农为乳制品加工企业提供的价格是不同的.此 ...
- qtp12版本下载安装破解教程
下载链接:https://download.csdn.net/download/weixin_41479750/11240466 下面是安装教程: 解压完成之后,双击运行setup.exe 之后点击运 ...