问题描述

输入格式

输出格式

一个整数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. DRF 组件

    DRF组件中的认证  授权  频率限制   分页  注册器  url控件

  2. 三十一、python中urllib和requests包详解

    A.urllibimport urllibimport urllib.requestimport json '''1.loads,dumpsjson.loads():将字符串转化成python的基础数 ...

  3. 阶段1 语言基础+高级_1-3-Java语言高级_04-集合_01 Collection集合_2_集合框架介绍

  4. set()运算

    1 计算两个list的关系时,可转化为set进行运算. 参考:https://www.runoob.com/python3/python3-set.html a =[1,4,3,5,6,6,7,7,7 ...

  5. 获取当前进程当前runtime加载的appdomain

    using System.Runtime.InteropServices; // Add the following as a COM reference - C:\WINDOWS\Microsoft ...

  6. 查询SQL Server数据库所有表字段备注

    SELECT 表名 = case when a.colorder=1 then d.name else '' end, 表说明 = case when a.colorder=1 then isnull ...

  7. node+express 中安装nodemon实时更新server.js

    每次启动node server.js,有一个缺点,每次server.js文件有改动时,必须重新执行指令node server.js,新的代码才会起作用 解决方案1 全局安装 npm install s ...

  8. REACT--》fetch---基本使用

    [WangQI]---fetch---基本使用   一.fetch fetch是一种XMLHttpRequest的一种替代方案,在工作当中除了用ajax获取后台数据外我们还可以使用fetch.axio ...

  9. linux系统中的基础监控(硬盘,内存,系统负载,CPU,网络等)

      Linux系统常见日常监控 系统信息 查看 CentOS 版本号:cat /etc/redhat-release 综合监控 nmon 系统负载 命令:w(判断整体瓶颈) 12:04:52 up 1 ...

  10. 剑指Offer编程题(Java实现)——替换空格

    题目描述 请实现一个函数,将一个字符串中的每个空格替换成“%20”.例如,当字符串为We Are Happy.则经过替换之后的字符串为We%20Are%20Happy. 解题思路1 在字符串尾部填充任 ...