【BZOJ】1367: [Baltic2004]sequence
题意
给\(n(n \le 10^6)\)个数的序列\(a\),求一个递增序列\(b\)使得\(\sum_{i=1}^{n} |a_i-b_i|\)最小。
分析
神题啊不会。
具体证明看黄源河论文《左偏树的特点及其应用》
思路:
- 将问题转化为求一个不降序列\(b\)。
- 如果\(a_1 \le a_2 \le ... \le a_n\),则最优解显然是\(b_i=a_i\)
- 如果\(a_1 \ge a_2 \ge ... \ge a_n\),则最优解显然是\(b_i=w\),其中\(w\)是\(a\)序列的中位数\(a_{\left \lfloor \frac{n+1}{2} \right \rfloor}\)
如果有两段\(a_1, a_2, ..., a_n\)和\(a_{n+1}, a_{n+2}, ..., a_{m}\),其中左边的最优解是\(b_i=u(1 \le i \le n)\),右边的最优解是\(b_i=w(n < i \le m)\),则 - 当\(u \le w\),则最优解显然是\(b_i=u(1 \le i \le n), b_i=w(n < i \le m)\)
- 当\(u > w\)时,则最优解是\(b_i=x(1 \le i \le m)\),其中\(x\)是\(a\)序列的中位数\(a_{\left \lfloor \frac{m+1}{2} \right \rfloor}\),证明如下:
首先我们来证明,对于任意序列\(a\),如果最优解是\(b_i=w(1 \le i \le n)\),其中\(w\)是中位数,那么对于所有的\(w \le w ' \le c_1\)或\(c_n \le w ' \le w\),解\(b_i=c_i(1 \le i \le n)\)都不会比解\(b_i=w ' (1 \le i \le n)\)更优。
然后我并没有看懂那个归纳证明QAQ
就是这句:
因为如果解变坏了,由归纳假设可知a[2],a[3],...,a[n]的中位数w>u,这样的话,最优解就应该为(u, u, ... , u, w, w, ... ,w ),矛盾。
现在回到\(2\),显然最优解中\(b_n \le u, b_{n+1} \ge w\),然后根据刚刚我们证明的东西,则最优解中肯定\(b_i=b_n(1 \le i < n), b_i=b_{n+1}(n < i \le m)\)。
也就是说,给你\(m\)个点要求找两个值\(u \le w\),使得前\(n\)个点到\(u\)的距离和加上剩下的点到\(w\)的距离和最短。显然一组最优解是\(u=w=a_{\left \lfloor \frac{m+1}{2} \right \rfloor}\)
至于思路\(1\)中怎么转化问题,就很简单了:
\(\sum_{i=1}^{n} |a_i-b_i| = \sum_{i=1}^{n} |(a_i-i)-(b_i-i)|\)
则令新的\(a ' _ i = a_i - i\)就行了。
至于思路\(2\)中怎么讨论,一个长度为\(n\)的不降序列可以看做\(n\)个不升序列。
题解
所以我们从左到右合并,如果新加进来的数和前面的数不构成不升序列,则合并相邻的。
于是问题转化为如何维护中位数。
然后发现对于正整数\(n, m\)有\(\left \lfloor \frac{n+1}{2} \right \rfloor + \left \lfloor \frac{m+1}{2} \right \rfloor \ge \left \lfloor \frac{n+m+1}{2} \right \rfloor\),所以我们只需要维护两个区间的中位数及比中位数小的数即可。然后合并的时候再考虑删掉一些数即可。
所以删除最大的数、合并两个东西这个活交给左偏树。
#include <bits/stdc++.h>
using namespace std;
inline int getint() {
int x=0;
char c=getchar();
for(; c<'0'||c>'9'; c=getchar());
for(; c>='0'&&c<='9'; x=x*10+c-'0', c=getchar());
return x;
}
const int N=1000105;
int q[N], s[N], t[N], n;
struct node *null;
struct node {
node *c[2];
int d, w;
void init(int _w) {
c[0]=c[1]=null;
w=_w;
d=0;
}
void up() {
if(c[0]->d<c[1]->d) {
swap(c[0], c[1]);
}
d=c[1]->d+1;
}
}Po[N], *iT=Po, *root[N];
inline node *newnode(int w) {
iT->init(w);
return iT++;
}
inline node *merge(node *l, node *r) {
if(l==null || r==null) {
return l==null?r:l;
}
if(l->w<r->w) {
swap(l, r);
}
l->c[1]=merge(l->c[1], r);
l->up();
return l;
}
int main() {
null=newnode(-(~0u>>1));
null->c[0]=null->c[1]=null;
n=getint();
int top=0;
for(int i=1; i<=n; ++i, ++top) {
root[top+1]=newnode(t[i]=getint()-i);
q[top+1]=i;
s[top+1]=1;
for(; top && root[top]->w>root[top+1]->w; --top) {
s[top]+=s[top+1];
root[top]=merge(root[top], root[top+1]);
for(int mid=(i-q[top]+2)>>1; s[top]>mid; --s[top], root[top]=merge(root[top]->c[0], root[top]->c[1]));
}
}
long long ans=0;
q[top+1]=0;
top=0;
for(int i=1; i<=n; ++i) {
for(; q[top+1]==i; ++top);
ans+=abs(t[i]-root[top]->w);
}
printf("%lld\n", ans);
return 0;
}
【BZOJ】1367: [Baltic2004]sequence的更多相关文章
- 【BZOJ 1367】 1367: [Baltic2004]sequence (可并堆-左偏树)
1367: [Baltic2004]sequence Description Input Output 一个整数R Sample Input 7 9 4 8 20 14 15 18 Sample Ou ...
- BZOJ 1367 [Baltic2004]sequence 解题报告
BZOJ 1367 [Baltic2004]sequence Description 给定一个序列\(t_1,t_2,\dots,t_N\),求一个递增序列\(z_1<z_2<\dots& ...
- BZOJ 1367: [Baltic2004]sequence [可并堆 中位数]
1367: [Baltic2004]sequence Time Limit: 20 Sec Memory Limit: 64 MBSubmit: 1111 Solved: 439[Submit][ ...
- bzoj 1367: [Baltic2004]sequence
1367: [Baltic2004]sequence Time Limit: 20 Sec Memory Limit: 64 MB Description Input Output 一个整数R Sa ...
- 1367: [Baltic2004]sequence
1367: [Baltic2004]sequence Time Limit: 20 Sec Memory Limit: 64 MB Submit: 1090 Solved: 432 [Submit ...
- 【BZOJ】3052: [wc2013]糖果公园
http://www.lydsy.com/JudgeOnline/problem.php?id=3052 题意:n个带颜色的点(m种),q次询问,每次询问x到y的路径上sum{w[次数]*v[颜色]} ...
- 【BZOJ】3319: 黑白树
http://www.lydsy.com/JudgeOnline/problem.php?id=3319 题意:给一棵n节点的树(n<=1e6),m个操作(m<=1e6),每次操作有两种: ...
- 【BZOJ】3319: 黑白树(并查集+特殊的技巧/-树链剖分+线段树)
http://www.lydsy.com/JudgeOnline/problem.php?id=3319 以为是模板题就复习了下hld............................. 然后n ...
- 【BZOJ】1013: [JSOI2008]球形空间产生器sphere
[BZOJ]1013: [JSOI2008]球形空间产生器sphere 题意:给n+1个n维的点的坐标,要你求出一个到这n+1个点距离相等的点的坐标: 思路:高斯消元即第i个点和第i+1个点处理出一个 ...
随机推荐
- [LeetCode] Binary Tree Zigzag Level Order Traversal
Given a binary tree, return the zigzag level order traversal of its nodes' values. (ie, from left to ...
- MS SQL 合并结果集并求和 分类: SQL Server 数据库 2015-02-13 10:59 92人阅读 评论(0) 收藏
业务情景:有这样一张表:其中Id列为表主键,Name为用户名,State为记录的状态值,Note为状态的说明,方便阅读. 需求描述:需要查询出这样的结果:某个人某种状态的记录数,如:张三,待审核记录数 ...
- ORA-03113:通信通道的文件结尾解决
今天跟往常一样,登陆PL/SQL,确登陆失败,出现一个错误“ORA-01034”和“ORA-27101”如图: 然后就就通过命令提示符去登陆Oracle,去查看怎么回事,然后问题进一步出现,错误“OR ...
- WPF中加载高分辨率图片性能优化
在最近的项目中,遇到一个关于WPF中同时加载多张图片时,内存占用非常高的问题. 问题背景: 在一个ListView中同时加载多张图片,注意:我们需要加载的图片分辨率非常高. 代码: XAML: < ...
- 卸载Eclipse安装的插件
背景:先前安装过Java Decompiler,不知道怎么弄的eclipse出问题之后不能用了,折腾了几次都没弄好,这次准备把这个插件先卸掉再装一次,结果发现,卸也卸不掉,最终是强制删除,以下为试过的 ...
- 1.ok6410移植bootloader,移植u-boot,学习u-boot命令
ok6410移植u-boot 既然是移植u-boot当然首先需要u-boot源码,这里的u-boot代码是由国嵌提供的. 一.配置编译u-boot A. 解压 u-boot 压缩文件 B. 进入解压生 ...
- App界面交互设计规范(转)
在上篇<APP界面设计风格>确定下来后,产品经理(兼交互设计)还不用着急将所有的交互稿扔给设计师进行细致的界面设计.在细节设计启动前,拉上设计师和安卓前端开发.ios前端开发一起商议确定设 ...
- 用c语言写一个函数把十进制转换成十六进制(转)
#include "stdio.h" int main() { int num=0;int a[100]; int i=0; int m=0;int yushu; char hex ...
- js日期格式化
<html> <head> <script> function test(){ //Js获取当前日期时间及其它操作 var myDate = new Date(); ...
- CSS 基本1
HTML元素可以分为两种: 块级元素 内联元素(行内元素) 两者的区别在于以下三点: 块级元素会独占一行(即无法与其他元素显示在同一行内,除非你显示修改元素的 display 属性),而内联元素则都会 ...