问题描述

输入格式

输出格式

一个整数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. R 时间戳转化

    转 http://stackoverflow.com/questions/1962278/dealing-with-timestamps-in-r You want the (standard) PO ...

  2. Jmeter上传文件、cookie、登录验证

    2.11选择http请求   3.0 cookie 域:十九服务器ip或者域名 路径就是接口路径 登录验证:

  3. 在sql中使用函数,遇到net.sf.jsqlparser.parser.ParseException异常

    异常详情如下 Caused by: net.sf.jsqlparser.parser.ParseException: Encountered " "->" &quo ...

  4. Numpy的补充(重要!!)

    轴的概念 英文解释  https://www.sharpsightlabs.com/blog/numpy-axes-explained/ 汉化解释 https://www.jianshu.com/p/ ...

  5. Java 基础-基本数据类型与表达式

    基本数据类型 基本概念 标识符 标识符与内存中的某个位置对应,Java 中标识符的规范如下: 必须由大小写字母.下划线.美元符号.数字组成 首字母只能是大小写字母.下划线.美元符号 变量 变量的值可以 ...

  6. C#怎么让字符串定长,不够的用空格补齐

    string.PadLeft 或者 string.PadRight  : string.PadLeft 表示如果一个字符串的长度小于指定的值,则在字符串的左侧(也就是前面)用指定的字符填充,直到字符串 ...

  7. 第一章:Java语言概述与环境开发

    1.计算机高级语言按程序的执行方式可以分为编译型和解释型两种: 2.JAVA程序的执行过程必须经过先编译后解释两个步骤: 3.JAVA语言里负责执行字节码文件的是JAVA虚拟机 (Java Virtu ...

  8. python绘制五角星

    问题描述: python中运用turtle图形模块绘制五角星 问题分析: turtle绘制图形时,得知图形中重要点的坐标非常重要. 于是,绘制五角星问题转化成为一个数学问题,计算五个顶点坐标即可. 已 ...

  9. dp(动态规划之最佳路径+dfs)

    http://acm.hdu.edu.cn/showproblem.php?pid=1078 FatMouse and Cheese Time Limit: 2000/1000 MS (Java/Ot ...

  10. React手稿 - Context

    Context Context提供了除props之外的传参数的方式. Context是全局跨组件传递数据的. API React.createContext ``` const {Provider, ...