问题描述

输入格式

输出格式

一个整数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. leetcode 39. 组合总和(python)

    给定一个无重复元素的数组 candidates 和一个目标数 target ,找出 candidates 中所有可以使数字和为 target 的组合. candidates 中的数字可以无限制重复被选 ...

  2. MongoDB--(NoSQL)入门介绍

    NoSQL中比较优秀的产品 windows 下载安装 shell 基本操作(javascript 引擎) BSON扩充的数据类型(JSON的扩展,浮点型,日期型的扩充) step1.创建数据库 use ...

  3. CentOS7设置启动模式问题

    参考地址 https://www.linuxidc.com/Linux/2015-12/126356.htm

  4. Delphi XE2 之 FireMonkey 入门(4) - 控件天生可做容器

    1.新建 FM(HD) 工程, 先添加 TLine(默认名称是 Line1);2.在 Line1 选择状态下添加 Button1;3.取消选择后添加 Button2 此时, Button1.Paren ...

  5. 【BASIS系列】SAP /usr/sap//DVEBMGS00满了怎么处理

    公众号:SAP Technical 本文作者:matinal 原文出处:http://www.cnblogs.com/SAPmatinal/ 原文链接:[BASIS系列]SAP /usr/sap//D ...

  6. ArrayList,linkedList vecator的实现和区别

    1.线程安全问题. ArrayList 和 linkedList 线程是不安全的,而vecator是线程安全的. 因为ArrayList 和 linkedList 是线程不同步的,vecator是同步 ...

  7. Spring Framework基础学习

    Spring Framework基础学习 Core support for dependency injection,transaction management,web applications,d ...

  8. 第七周实验报告&课程总结

    一.完成火车站售票程序的模拟. 要求: (1)总票数1000张: (2)10个窗口同时开始卖票: (3)卖票过程延时1秒钟: (4)不能出现一票多卖或卖出负数号票的情况. 代码: public cla ...

  9. Python3数据分析与挖掘建模实战 学习 教程

    Python3数据分析与挖掘建模实战 学习 教程 Python数据分析简介Python入门 运行:cmd下"python hello.py" 基本命令: 第三方库安装Windows ...

  10. 旧接口注册LED字符驱动设备(动态映射)

    #include <linux/init.h> // __init __exit #include <linux/module.h> // module_init module ...