问题描述

输入格式

输出格式

一个整数R

样例输入

7

9

4

8

20

14

15

18

样例输出

13

数据范围

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

R=13

解析&左偏树详解

IOI2005国家集训队论文 黄源河

代码

#include <iostream>
#include <cstdio>
#define N 1000002
using namespace std;
int n,i,j,a[N],son[N][2],fa[N],dis[N],num[N],l[N],r[N],q[N],top;
long long val[N];
long long abs(long long x)
{
return x>0?x:-x;
}
int merge(int x,int y)
{
if(x==0) return y;
if(y==0) return x;
if(val[x]<val[y]||(val[x]==val[y]&&x>y)) swap(x,y);
son[x][1]=merge(son[x][1],y);
num[x]=num[son[x][0]]+num[son[x][1]]+1;
if(dis[son[x][0]]<dis[son[x][1]]) swap(son[x][0],son[x][1]);
fa[son[x][0]]=fa[son[x][1]]=fa[x]=x;
dis[x]=dis[son[x][1]]+1;
return x;
}
void pop(int x)
{
fa[son[x][0]]=son[x][0];
fa[son[x][1]]=son[x][1];
fa[x]=merge(son[x][0],son[x][1]);
}
int main()
{
cin>>n;
for(i=1;i<=n;i++){
cin>>val[i];
val[i]-=i;
a[i]=val[i];
}
val[0]=-1;
for(i=1;i<=n;i++){
q[++top]=i;
l[top]=r[top]=i;
num[i]=1;
while(top>1&&val[q[top-1]]>val[q[top]]){
q[top-1]=merge(q[top-1],q[top]);
top--;
r[top]=i;
while(num[q[top]]>(r[top]-l[top]+2)/2) pop(q[top]),q[top]=fa[q[top]];
}
}
long long ans=0;
for(i=1;i<=top;i++){
for(j=l[i];j<=r[i];j++){
ans+=abs(a[j]-val[q[i]]);
}
}
cout<<ans<<endl;
return 0;
}

Baltic2014 sequence的更多相关文章

  1. bzoj3917: [Baltic2014]sequence

    Description  序列A由从N开始的连续K个数按顺序构成,现在将A中的每个数只保留某一个数码,记为序列B,给定K和B,求可能的最小的N Input 第一行一个数K,第二行K个数B_i Outp ...

  2. bzoj AC倒序

    Search GO 说明:输入题号直接进入相应题目,如需搜索含数字的题目,请在关键词前加单引号 Problem ID Title Source AC Submit Y 1000 A+B Problem ...

  3. oracle SEQUENCE 创建, 修改,删除

    oracle创建序列化: CREATE SEQUENCE seq_itv_collection            INCREMENT BY 1  -- 每次加几个              STA ...

  4. Oracle数据库自动备份SQL文本:Procedure存储过程,View视图,Function函数,Trigger触发器,Sequence序列号等

    功能:备份存储过程,视图,函数触发器,Sequence序列号等准备工作:--1.创建文件夹 :'E:/OracleBackUp/ProcBack';--文本存放的路径--2.执行:create or ...

  5. DG gap sequence修复一例

    环境:Oracle 11.2.0.4 DG 故障现象: 客户在备库告警日志中发现GAP sequence提示信息: Mon Nov 21 09:53:29 2016 Media Recovery Wa ...

  6. Permutation Sequence

    The set [1,2,3,-,n] contains a total of n! unique permutations. By listing and labeling all of the p ...

  7. [LeetCode] Sequence Reconstruction 序列重建

    Check whether the original sequence org can be uniquely reconstructed from the sequences in seqs. Th ...

  8. [LeetCode] Binary Tree Longest Consecutive Sequence 二叉树最长连续序列

    Given a binary tree, find the length of the longest consecutive sequence path. The path refers to an ...

  9. [LeetCode] Verify Preorder Sequence in Binary Search Tree 验证二叉搜索树的先序序列

    Given an array of numbers, verify whether it is the correct preorder traversal sequence of a binary ...

随机推荐

  1. vue2.0 之 douban (四)创建Swipe图片轮播组件

    swiper中文文档:http://www.swiper.com.cn 1.我们在components文件夹里创建一个swipe组件,将需要用到的js以及css文件复制到assets/lib文件夹下, ...

  2. pc显示,手机隐藏

    <div class="none"><img src="https://www.foresthouse.cn/bigpic.jpg"/> ...

  3. netstat和ps

    ps是查看进程, 主要是针对本机的, 进程活动, 更多的是关注性能, 关注对机器 资源的使用清况 netstat是查看网络状态, 主要是针对网络的.是查看网络上, 对内网 外网的活动情况, 更多的是关 ...

  4. DlgResToDlgTemplate 的代码,提取EXE中的资源,然后转化成C的字符串数组

    代码来源:https://www.codeproject.com/Articles/13330/Using-Dialog-Templates-to-create-an-InputBox-in-C #i ...

  5. 错误:Only the original thread that created a view hierarchy can touch its views——Handler的使用

    在跟随教程学习到显示web页面的html源码时报错:Only the original thread that created a view hierarchy can touch its views ...

  6. 16/7/8_PHP-设置cookie会话控制(session与cookie)

    设置cookie PHP设置Cookie最常用的方法就是使用setcookie函数,setcookie具有7个可选参数,我们常用到的为前5个: name( Cookie名)可以通过$_COOKIE[' ...

  7. 一些输出、处理细节&注意点

    https://blog.csdn.net/qq_41071646/article/details/79953476 输出百分比的时候,结果需要加上一个EPS(1e-6)四舍五入保证精度. 卡精度—— ...

  8. nginx-->基本使用

    Nginx基本使用   一.下载 http://nginx.org/en/download.html 二.解压文件 在当前文件夹下通过终端就可以操作nginx nginx -v 三.配置详解 #use ...

  9. Java第四周编程总结

    第四周编程总结 1.写一个名为Rectangle的类表示矩形.其属性包括宽width.高height和颜色color,width和height都是double型的,而color则是String类型的. ...

  10. 配置Trunk接口

    实验内容 本实验模拟某公司网络场景.公司规模较大,员工200余名,内部网络是-一个大的局域网.公司放置了多台接入交换机(如S1和S2)负责员工的网络接入.接入交换机之间通过汇聚交换机S3相连.公司通过 ...