题目链接

BZOJ1367

题解

又是一道神题,,

我们考虑一些简单的情况:

我们先假设\(b_i\)单调不降,而不是递增

对于递增序列\(\{a_i\}\),显然答案\(\{b_i\}\)满足\(b_i = a_i\)

对于递减序列\(\{a_i\}\),显然答案\(\{b_i\}\)满足\(b_i\)为\(a_i\)的中位数

于是我们有了初步的想法:

将\(a_i\)分成若干个单调递减的段,每段的答案为其中位数

然后顺次访问段

如果两段的答案是递增的,显然这两段就没有影响,相互独立了,就保留答案

如果相邻两段的答案是递减的,就合并这两段,重新寻找它们的中位数

可以证明是对的

对于单调递增的处理,我们只需令\(A[i] = A[i] - i\),即可转变为单调不下降

维护中位数可以用对顶堆实现,由于涉及堆的合并,那就使用左偏树

#include<algorithm>
#include<iostream>
#include<cstring>
#include<cstdio>
#include<cmath>
#include<map>
#define Redge(u) for (int k = h[u],to; k; k = ed[k].nxt)
#define REP(i,n) for (int i = 1; i <= (n); i++)
#define mp(a,b) make_pair<int,int>(a,b)
#define cls(s) memset(s,0,sizeof(s))
#define cp pair<int,int>
#define LL long long int
using namespace std;
const int maxn = 1000005,maxm = 100005,INF = 1000000000;
inline int read(){
int out = 0,flag = 1; char c = getchar();
while (c < 48 || c > 57){if (c == '-') flag = -1; c = getchar();}
while (c >= 48 && c <= 57){out = (out << 3) + (out << 1) + c - 48; c = getchar();}
return out * flag;
}
int val[maxn],ls[maxn],rs[maxn],d[maxn],siz[maxn],rt[maxn],Rt[maxn];
int merge(int a,int b){
if (!b) return a;
if (!a) return b;
if (val[b] < val[a]) swap(a,b);
rs[a] = merge(rs[a],b);
siz[a] = siz[ls[a]] + 1 + siz[rs[a]];
if (d[ls[a]] < d[rs[a]]) swap(ls[a],rs[a]);
d[a] = rs[a] ? d[rs[a]] + 1 : 0;
return a;
}
int Merge(int a,int b){
if (!b) return a;
if (!a) return b;
if (val[b] > val[a]) swap(a,b);
rs[a] = Merge(rs[a],b);
siz[a] = siz[ls[a]] + 1 + siz[rs[a]];
if (d[ls[a]] < d[rs[a]]) swap(ls[a],rs[a]);
d[a] = d[rs[a]] + 1;
return a;
}
int n,pos[maxn],len[maxn],K;
LL A[maxn];
void work(){
int tmp; d[0] = -1;
for (int i = 1; i <= n; i++){
pos[++K] = i; len[K] = 1; rt[i] = i; siz[rt[i]] = 1; val[i] = A[i];
while (K > 1 && val[rt[pos[K]]] < val[rt[pos[K - 1]]]){
K--;
rt[pos[K]] = merge(rt[pos[K]],rt[pos[K + 1]]);
Rt[pos[K]] = Merge(Rt[pos[K]],Rt[pos[K + 1]]);
len[K] += len[K + 1];
while (siz[rt[pos[K]]] > siz[Rt[pos[K]]]){
tmp = rt[pos[K]];
rt[pos[K]] = merge(ls[rt[pos[K]]],rs[rt[pos[K]]]);
ls[tmp] = rs[tmp] = 0; siz[tmp] = 1;
Rt[pos[K]] = Merge(Rt[pos[K]],tmp);
}
while (siz[rt[pos[K]]] < siz[Rt[pos[K]]]){
tmp = Rt[pos[K]];
Rt[pos[K]] = Merge(ls[Rt[pos[K]]],rs[Rt[pos[K]]]);
ls[tmp] = rs[tmp] = 0; siz[tmp] = 1;
rt[pos[K]] = merge(rt[pos[K]],tmp);
}
}
//printf("[%d,%d] mid = %d\n",i - len[K] + 1,i,val[rt[pos[K]]]);
}
LL ans = 0,v;
for (int i = 1,l = 1; i <= K; i++){
v = siz[rt[pos[i]]] > siz[Rt[pos[i]]] ? val[rt[pos[i]]] : val[Rt[pos[i]]];
//printf("[%d,%d] v = %lld\n",l,l + len[i] - 1,v);
for (int j = 0; j < len[i]; j++)
ans += abs(v - A[l + j]);
l += len[i];
}
printf("%lld\n",ans);
}
int main(){
n = read();
REP(i,n) A[i] = read() - i;
//REP(i,n) printf("%lld ",A[i]); puts("");
work();
return 0;
}

BZOJ1367 [Baltic2004]sequence 【左偏树】的更多相关文章

  1. bzoj1367 [Baltic2004]sequence 左偏树+贪心

    题目传送门 https://lydsy.com/JudgeOnline/problem.php?id=1367 题解 先考虑条件为要求不下降序列(不是递增)的情况. 那么考虑一段数值相同的子段,这一段 ...

  2. BZOJ1367: [Baltic2004]sequence(左偏树)

    Description Input Output 一个整数R Sample Input 7 9 4 8 20 14 15 18 Sample Output 13 解题思路: 有趣的数学题. 首先确定序 ...

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

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

  4. BZOJ1367 BOI2004Sequence(左偏树)

    首先考虑把bi和ai同时减i,问题变为非严格递增.显然如果a是一个递减序列,b序列所有数都取其中位数最优.于是划分原序列使得每一部分递减,然后考虑合并相邻两段.如果前一段的中位数<=后一段的中位 ...

  5. bzoj 1367 [ Baltic 2004 ] sequence —— 左偏树

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=1367 好题啊!论文上的题: 论文上只给出了不下降序列的求法: 先考虑特殊情况,如果原序列上升 ...

  6. BZOJ1367 [Baltic2004]sequence 堆 左偏树

    欢迎访问~原文出处——博客园-zhouzhendong 去博客园看该题解 题目传送门 - BZOJ1367 题意概括 Description Input Output 一个整数R 题解 http:// ...

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

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

  8. 洛谷P4331 [BOI2004] Sequence 数字序列 [左偏树]

    题目传送门 数字序列 题目描述 给定一个整数序列 a1​,a2​,⋅⋅⋅,an​ ,求出一个递增序列 b1​<b2​<⋅⋅⋅<bn​ ,使得序列 ai​ 和 bi​ 的各项之差的绝对 ...

  9. [BOI2004]Sequence 数字序列(左偏树)

    PS:参考了黄源河的论文<左偏树的特点及其应用> 题目描述:给定一个整数序列\(a_1, a_2, - , a_n\),求一个递增序列\(b_1 < b_2 < - < ...

随机推荐

  1. 一个web应用的诞生(2)--使用模板

    经过了第一章的内容,已经可以做出一些简单的页面,首先用这种方式做一个登录页面,首先要创建一个login的路由方法: @app.route("/login",methods=[&qu ...

  2. ConfigurationProperties cannot be resolved to a type

    pom.xml 中报错之前: <parent> <groupId>org.springframework.boot</groupId> <artifactId ...

  3. 2019年猪年颁奖典礼、公司年会、跨年晚会、科技会议、年终答谢会之幕布背景展板PSD模板-第三部分

    16套--2019年猪年颁奖典礼.公司年会.跨年晚会.科技会议.年终答谢会之幕布.背景和展板PSD模板,免费颁奖典礼PSD展板背景幕布,下载地址:百度网盘,https://pan.baidu.com/ ...

  4. win10 Docker Toolbox 默认路径不能写问题

    2018-8-30 这几天正学习docker,win10系统家庭版,未升级成专业版,只能安装Docker Toolbox来练习, 安装好后准备做个镜像,默认路径新建目录却提示不允许创建 由于Docke ...

  5. Shiro 修改权限,刷新权限

    shiro 访问鉴权:Realm AuthorizingRealm->doGetAuthorizationInfo doGetAuthorizationInfo protected abstra ...

  6. Laya 自适应 不拉伸处理

    Laya.init(640, Laya.Browser.width / 640 * 1028, WebGL); Laya.stage.scaleMode = "fixedwidth" ...

  7. Javascript深入__proto__和prototype的区别和联系

    有一个一个装逼的同事,写了一段代码 function a(){} a.__proto__.__proto__.__proto__ 然后问我,下面这个玩意a.__proto__.__proto__.__ ...

  8. Hyperledger Fabric 1.0 从零开始(十三)——orderer分布式方案

    简述 在搭建HyperLedger Fabric环境的过程中,我们会用到一个configtx.yaml文件(可参考Hyperledger Fabric 1.0 从零开始(八)——Fabric多节点集群 ...

  9. IO多路复用(一)-- Select、Poll、Epoll

    在上一篇博文中提到了五种IO模型,关于这五种IO模型可以参考博文IO模型浅析-阻塞.非阻塞.IO复用.信号驱动.异步IO.同步IO,本篇主要介绍IO多路复用的使用和编程. IO多路复用的概念 多路复用 ...

  10. 基于C#的机器学习--颜色混合-自组织映射和弹性神经网络

    自组织映射和弹性神经网络 自组织映射(SOM),或者你们可能听说过的Kohonen映射,是自组织神经网络的基本类型之一.自组织的能力提供了对以前不可见的输入数据的适应性.它被理论化为最自然的学习方式之 ...