题目描述:

给定一个序列t1,t2,...,tn ,求一个递增序列z1<z2<...<zn , 使得R=|t1−z1|+|t2−z2|+...+|tn−zn| 的值最小。本题中,我们只需要求出这个最小的R值。

样例输入

7 9 4 8 20 14 15 18

样例输出

13

提示

所求的Z序列为6,7,8,13,14,15,18.

R=13

题解:

考虑t1>=t2>=t3>=t4这种递减的情况,那么整个z只需取t数组的中位数即可。

由于z是递增数列,不能全取一样的数。所以我们把t[i]-i;保证z数组就可以取一样的数了 且不会影响答案,因为t[i]-i的同时,求出来z[i]也是减了i的

那么我们就采取分治的思想,把原数列分成m个递减区间,然后分别取中位数,使得该区间的差值尽量小.

设X[i]为没个区间的中位数,那么如果X[i]<X[i-1]时不满足z[i]递增的题意,所以要合并,且合并后的答案会更优。(见网上的论文证明)

以下是详细做法和证明:

还有一个我的傻逼错误

ldis()和rdis()没写return 居然函数没有返回值不warning,浪费了很久

代码如下:

 #include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<cstdlib>
using namespace std;
const int N=;
typedef long long ll;
ll gi()
{
ll str=;char ch=getchar();
while(ch>'' || ch<'')ch=getchar();
while(ch>='' && ch<='')str=str*+ch-'',ch=getchar();
return str;
}
struct node
{
int dis,size,ls,rs;ll x;
node *l,*r;
int ldis(){return l?l->dis:;}
int rdis(){return r?r->dis:;}
}T[N];
ll a[N];node *root[N],*pos=T;
ll st[N],top=;
ll ans=;
void updata(node *&R)
{
if(R)R->size=(R->l?R->l->size:)+(R->r?R->r->size:)+;
}
node *merge(node *p,node *q)
{
if(!p||!q){return p?p:q;}
if(p->x<q->x)swap(p,q);
if(p->ls>q->ls)p->ls=q->ls;
if(p->rs<q->rs)p->rs=q->rs;
p->r=merge(p->r,q);
if(p->ldis()<p->rdis())swap(p->l,p->r);
p->dis=p->rdis()+;
updata(p);
return p;
}
void Delet(int x)
{
node *y=root[x];
root[x]=merge(root[x]->r,root[x]->l);
root[x]->ls=y->ls;root[x]->rs=y->rs;
y->l=y->r=NULL;y->size=;
}
int main()
{
int n=gi(),k;
for(int i=;i<=n;i++){
a[i]=gi()-i;
pos->l=pos->r=NULL;pos->x=a[i];pos->size=;
pos->ls=pos->rs=i;
root[i]=pos;pos->dis=;
pos++;
}
int now;
st[++top]=;
for(int i=;i<=n;i++){
now=i;
while(top){
k=st[top];
if(root[k]->x>root[now]->x){
top--;
root[k]=merge(root[now],root[k]);
while((root[k]->size<<)>(root[k]->rs-root[k]->ls+))Delet(k);
now=k;
if(!top){
st[++top]=now;
break;
}
}
else{
st[++top]=now;
break;
}
}
}
while(top){
k=st[top];top--;
for(int i=root[k]->ls;i<=root[k]->rs;i++)ans+=abs(root[k]->x-a[i]);
}
cout<<ans;
return ;
}

[Baltic2004]sequence的更多相关文章

  1. 【bzoj1367】[Baltic2004]sequence

    2016-05-31 17:31:26 题目:http://www.lydsy.com/JudgeOnline/problem.php?id=1367 题解:http://www.cnblogs.co ...

  2. 【BZOJ 1367】 1367: [Baltic2004]sequence (可并堆-左偏树)

    1367: [Baltic2004]sequence Description Input Output 一个整数R Sample Input 7 9 4 8 20 14 15 18 Sample Ou ...

  3. BZOJ 1367: [Baltic2004]sequence [可并堆 中位数]

    1367: [Baltic2004]sequence Time Limit: 20 Sec  Memory Limit: 64 MBSubmit: 1111  Solved: 439[Submit][ ...

  4. BZOJ 1367 [Baltic2004]sequence 解题报告

    BZOJ 1367 [Baltic2004]sequence Description 给定一个序列\(t_1,t_2,\dots,t_N\),求一个递增序列\(z_1<z_2<\dots& ...

  5. bzoj 1367: [Baltic2004]sequence

    1367: [Baltic2004]sequence Time Limit: 20 Sec  Memory Limit: 64 MB Description Input Output 一个整数R Sa ...

  6. 【BZOJ1367】[Baltic2004]sequence 左偏树

    [BZOJ1367][Baltic2004]sequence Description Input Output 一个整数R Sample Input 7 9 4 8 20 14 15 18 Sampl ...

  7. 1367: [Baltic2004]sequence

    1367: [Baltic2004]sequence Time Limit: 20 Sec  Memory Limit: 64 MB Submit: 1090  Solved: 432 [Submit ...

  8. BZOJ1367——[Baltic2004]sequence

    1.题目大意:给一个序列t,然后求一个序列z,使得$|z1-t1|+|z2-t2|+...+|zn-tn|$的值最小,我们只需要求出这个值就可以了,并且z序列是递增的 2.分析:这道题z序列是递增的, ...

  9. 【BZOJ】1367: [Baltic2004]sequence

    题意 给\(n(n \le 10^6)\)个数的序列\(a\),求一个递增序列\(b\)使得\(\sum_{i=1}^{n} |a_i-b_i|\)最小. 分析 神题啊不会. 具体证明看黄源河论文&l ...

  10. BZOJ1367 [Baltic2004]sequence

    现学的左偏树...这可是道可并堆的好题目. 首先我们考虑z不减的情况: 我们发现对于一个区间[l, r],里面是递增的,则对于此区间最优解为z[i] = t[i]: 如果里面是递减的,z[l] = z ...

随机推荐

  1. 【iOS】swift-如何理解 if let 与guard?

    著作权归作者所有. 商业转载请联系作者获得授权,非商业转载请注明出处. 作者:黄兢成 链接:http://www.zhihu.com/question/36448325/answer/68614858 ...

  2. rcnn fast-rcnn faster-rcnn资料

    ---恢复内容开始--- 框架:https://github.com/rbgirshick 论文:链接: https://pan.baidu.com/s/1jIoChxG 密码: ubgm faste ...

  3. SQLite 带你入门

    SQLite数据库相较于我们常用的Mysql,Oracle而言,实在是轻量得不行(最低只占几百K的内存).平时开发或生产环境中使用各种类型的数据库,可能都需要先安装数据库服务(server),然后才能 ...

  4. JAVA_SE基础——15.循环嵌套

    嵌套循环是指在一个循环语句的循环体中再定义一个循环语句结构,while,do-while,for循环语句都可以进行嵌套,并且可以互相嵌套,下面来看下for循环中嵌套for循环的例子. 如下: publ ...

  5. Python之旅_第一章Python入门

    一.编程语言分类 1.机器语言:即计算机能听懂的二进制语言,0000 0001,直接操控硬件: 2.汇编语言:简写的英文标识符代替二进制语言,本质同样是直接操控硬件: 3.高级语言:用更贴近人类的语言 ...

  6. LeetCode & Q414-Third Maximum Number-Easy

    Array Math Description: Given a non-empty array of integers, return the third maximum number in this ...

  7. es6学习笔记--Interator和Generator(以及for-of的用法)

    这几天学习了遍历器和生成器,看着资料学,有点雾里缭绕的感觉,让人忍不住放弃,还好多看了好几遍,怼着资料里的例子让自己学会了Interator和Generator.   Interator,中文简称:遍 ...

  8. 使用 Angular CLI 和 Webpack 分析包尺寸

    使用 Angular CLI 和 Webpack 分析包尺寸 对于 Web app 来说,高性能总是最高优先级,对于 Angular 也不例外.但是随着应用复杂度的不断增长,我们如何才能知道哪些内容打 ...

  9. C#使用Gecko实现浏览器

    Gecko就是火狐浏览器的内核啦,速度很快,兼容性比.net内置的webbrowser高到不知哪里去了. 使用Gecko首先要下载一堆依赖库,主要是Skybound.Gecko和xulrunner. ...

  10. SourceTree 03 - 跳过账号登录直接进入主界面

    SourceTree系列第1篇 SourceTree 01 - git 客户端介绍(http://www.cnblogs.com/geaosu/p/8807666.html) SourceTree系列 ...