浅谈左偏树:https://www.cnblogs.com/AKMer/p/10246635.html

题目传送门:https://lydsy.com/JudgeOnline/problem.php?id=1367

显然,如果给出的数组是递增的,那么答案就是\(0\)。

如果给出的数组是递减的,根据贪心的思想答案就是\(\sum\limits_{i=1}^{n}|x-t_i|\),\(x\)是\(t\)数组的中位数。

但是给出的数组是无序的。

我们可以把这个数组划成一段段的,每一段都选一个\(x\)去当做中位数。简单的来讲,最优的方案是\(z_i=t_i\)的,但是为了保证\(z\)数组的递增性我们必须要把比前一位小的\(z\)和前一位合并到一段里去,形成新的一段,然后这一段的\(z\)就是这一段\(t\)的中位数。

但是这样出现了重复的\(z\),不满足严格递增的性质。但是我们只需要把\(t_i=t_i-i\)就行了。这样就默认是严格递增的了。

时间复杂度:\(O(nlogn)\)

空间复杂度:\(O(n)\)

代码如下:

#include <cstdio>
#include <algorithm>
using namespace std;
typedef long long ll; const int maxn=1e6+5; ll ans;
int n,tot,cnt;
int l[maxn],r[maxn];
int fa[maxn],dist[maxn],son[maxn][2];
int a[maxn],rt[maxn],val[maxn],siz[maxn]; int read() {
int x=0,f=1;char ch=getchar();
for(;ch<'0'||ch>'9';ch=getchar())if(ch=='-')f=-1;
for(;ch>='0'&&ch<='9';ch=getchar())x=x*10+ch-'0';
return x*f;
} int newnode(int v) {
val[++tot]=v;
siz[tot]=1;return tot;
} int merge(int a,int b) {
if(!a||!b)return a+b;
if(val[a]<val[b])swap(a,b);
son[a][1]=merge(son[a][1],b);
if(dist[son[a][1]]>dist[son[a][0]])
swap(son[a][1],son[a][0]);
dist[a]=dist[son[a][1]]+1;
siz[a]=siz[son[a][0]]+1+siz[son[a][1]];
return a;
} int pop(int u) {
int res=merge(son[u][0],son[u][1]);
son[u][0]=son[u][1]=0;
return res;
} int main() {
n=read(),dist[0]=-1;
for(int i=1;i<=n;i++)
a[i]=read()-i;
for(int i=1;i<=n;i++) {
rt[++cnt]=newnode(a[i]);l[cnt]=r[cnt]=i;
while(cnt>1&&val[rt[cnt]]<val[rt[cnt-1]]) {
rt[cnt-1]=merge(rt[cnt-1],rt[cnt]);r[--cnt]=i;
while(siz[rt[cnt]]>(r[cnt]-l[cnt]+2)/2)rt[cnt]=pop(rt[cnt]);
}
}
for(int i=1;i<=cnt;i++)
for(int j=l[i];j<=r[i];j++)
ans+=abs(val[rt[i]]-a[j]);
printf("%lld\n",ans);
return 0;
}

BZOJ1367:[Baltic2004]sequence的更多相关文章

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

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

  2. 【bzoj1367】[Baltic2004]sequence

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

  3. 【bzoj1367】[Baltic2004]sequence 可并堆

    题目描述 输入 输出 一个整数R 样例输入 7 9 4 8 20 14 15 18 样例输出 13 题解 可并堆,黄源河<左偏树的特点及其应用>Page 13例题原题 #include & ...

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

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

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

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

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

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

  7. bzoj 1367: [Baltic2004]sequence

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

  8. 1367: [Baltic2004]sequence

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

  9. 【九度OJ】题目1442:A sequence of numbers 解题报告

    [九度OJ]题目1442:A sequence of numbers 解题报告 标签(空格分隔): 九度OJ 原题地址:http://ac.jobdu.com/problem.php?pid=1442 ...

随机推荐

  1. nginx学习之压缩解压篇(七)

    1.简介 压缩响应可以减少传输数据的大小,节省带宽.但过多的压缩会造成很大的处理开销.在发送给客户端之前,nginx会对响应做压缩,但是如果后端服务器已经 压缩过了,nginx就不再压缩. 2.开启压 ...

  2. R语言做正态性检验

    摘自:吴喜之:<非参数统计>(第二版),中国统计出版社,2006年10月:P164-165 1.ks.test()    例如零假设为N(15,0.2),则ks.test(x," ...

  3. 【leetcode刷题笔记】Binary Tree Level Order Traversal(JAVA)

    Given a binary tree, return the level order traversal of its nodes' values. (ie, from left to right, ...

  4. python 3 json 序列化

    python 3 json 序列化 我们学习过用eval内置方法可以将一个字符串转成python对象,不过,eval方法是有局限性的,对于普通的数据类型,json.loads和eval都能用,但遇到特 ...

  5. curl简单封装 get post

    Curl.php <?php /** * Class Curl curl简单封装 get post */ class Curl { /** * @brief get请求 * @param $ur ...

  6. hiho一下 第四十九周 题目1 : 欧拉路·一【无向图 欧拉路问题】

    题目1 : 欧拉路·一 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 小Hi和小Ho最近在玩一个解密类的游戏,他们需要控制角色在一片原始丛林里面探险,收集道具,并找到最 ...

  7. Mysql视图使用总结

    视图View使用总结: 视图可以看作为“虚拟表”,因为它返回的结果集格式与实体数据表返回的数据集格式类似,并且引用视图的方式与引用数据表的方式相同.每次查询使用视图时,DBMS会动态生成视图结果集所需 ...

  8. castle windsor学习-----Inline dependencies 依赖

    应用程序中的很多组件都会依赖其他的服务组件,很多依赖一些不合法的组件或者容器中没有的组件,例如int类型.string类型.TimeSpan类型 Windsor支持以上的场景,注册API有Depend ...

  9. 算法(Algorithms)第4版 练习 2.2.23

    测试结果: 算法(Algorithms)第4版 练习 2.2.10 算法(Algorithms)第4版 练习 2.2.11(1) 算法(Algorithms)第4版 练习 2.2.11(2) 算法(A ...

  10. 大话设计模式--组合模式 Composite -- C++实现实例

    1. 组合模式: 将对象组合成树形结构以表示"部分--整体"的层次结构,组合模式使用户对单个对象和组合对象的使用具有一致性. 需求中是体现部分与整体层次的结构时,希望用户可以忽略组 ...