Description

Input

Output

一个整数R

Sample Input

7
9
4
8
20
14
15
18

Sample Output

13

HINT

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

/*
思维很扭曲(反正我想不出来)的一道题。
先想想不下降的:
考虑一个正序的序列,z[i]=t[i]
考虑一个逆序的序列,z[i]=x(x是逆列的中位数)
既然是这样那么我们就可以把整个序列化分成逆序的若干段,对于每段求中位数(正序的可看成num个逆序的)。
维护中位数用左偏树,具体方法是始终保持堆中数的个数不大于原数个数的一半。
至于改成上升的,把原序列t[i]-=i。 PS:题解中的root[i]和堆中下标的关系把我看蒙了,所以这里说一下。
代码中是建立了n个堆,然而不断合并,最终变成了tot个。
root[i]表示第i个堆(合并后的)的堆顶是哪个元素。
*/
#include<cstdio>
#include<iostream>
#include<cstdlib>
#define N 1000010
using namespace std;
int t[N],root[N],l[N],r[N],num[N],cnt[N],n,tot;
struct node{
int l,r,dis,w;
};node heap[N];
int merge(int a,int b){
if(a==||b==)return a+b;
if(heap[a].w<heap[b].w)swap(a,b);
heap[a].r=merge(heap[a].r,b);
if(heap[heap[a].r].dis>heap[heap[a].l].dis)
swap(heap[a].r,heap[a].l);
heap[a].dis=heap[heap[a].r].dis+;
return a;
}
int pop(int a){
return merge(heap[a].l,heap[a].r);
}
int main(){
scanf("%d",&n);
for(int i=;i<=n;i++)
scanf("%d",&t[i]),t[i]-=i;
for(int i=;i<=n;i++){
++tot;
l[tot]=r[tot]=i;
num[tot]=cnt[tot]=;
root[tot]=i;
heap[i].dis=heap[i].l=heap[i].r=;
heap[i].w=t[i]; while(tot>&&heap[root[tot]].w<heap[root[tot-]].w){
--tot;
root[tot]=merge(root[tot],root[tot+]);
num[tot]+=num[tot+],cnt[tot]+=cnt[tot+],r[tot]=r[tot+];
for(;cnt[tot]*>num[tot]+;--cnt[tot])
root[tot]=pop(root[tot]);
}
}
long long ans=;
for(int i=;i<=tot;i++)
for(int j=l[i],w=heap[root[i]].w;j<=r[i];j++)
ans+=abs(t[j]-w);
cout<<ans;
return ;
}

sequence(bzoj 1367)的更多相关文章

  1. BZOJ 1367 [Baltic2004]sequence 解题报告

    BZOJ 1367 [Baltic2004]sequence Description 给定一个序列\(t_1,t_2,\dots,t_N\),求一个递增序列\(z_1<z_2<\dots& ...

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

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

  3. BZOJ 1367: [Baltic2004]sequence [可并堆 中位数]

    1367: [Baltic2004]sequence Time Limit: 20 Sec  Memory Limit: 64 MBSubmit: 1111  Solved: 439[Submit][ ...

  4. bzoj 1367: [Baltic2004]sequence

    1367: [Baltic2004]sequence Time Limit: 20 Sec  Memory Limit: 64 MB Description Input Output 一个整数R Sa ...

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

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

  6. BZOJ 1367 [Baltic2004]sequence (可并堆)

    题面:BZOJ传送门 题目大意:给你一个序列$a$,让你构造一个递增序列$b$,使得$\sum |a_{i}-b_{i}|$最小,$a_{i},b_{i}$均为整数 神仙题.. 我们先考虑b不递减的情 ...

  7. bzoj 1367 - sequence

    Description 给定一个序列\(t_1,t_2,\cdots,t_n\),求一个递增序列\(z_1<z_2<...<z_n\), 使得 \(R=|t_1−z_1|+|t_2− ...

  8. BZOJ 1367([Baltic2004]sequence-左偏树+中位数贪心)

    1367: [Baltic2004]sequence Time Limit: 20 Sec   Memory Limit: 64 MB Submit: 521   Solved: 159 [ Subm ...

  9. 【BZOJ 1049】 1049: [HAOI2006]数字序列 (LIS+动态规划)

    1049: [HAOI2006]数字序列 Description 现在我们有一个长度为n的整数序列A.但是它太不好看了,于是我们希望把它变成一个单调严格上升的序列.但是不希望改变过多的数,也不希望改变 ...

随机推荐

  1. 借教室 线段树and二分

    描述 在大学期间,经常需要租借教室.大到院系举办活动,小到学习小组自习讨论,都需要向学校申请借教室.教室的大小功能不同,借教室人的身份不同,借教室的手续也不一样. 面对海量租借教室的信息,我们自然希望 ...

  2. 一些关于Spring的随笔

    Spring的IOC.AOP IOC(Inversion of Control): spring容器控制了所有的bean,不用spring以前,一个bean要依赖另一个bean就在这个bean里初始化 ...

  3. re正则表达式讲解—初步认识

    # f = open(r"C:\Users\LENOVO\Desktop\模特.txt",'r') # 1.常规提取文档内容方法 # contacts = [] # for i i ...

  4. java实现汉诺塔算法

    package com.ywx.count; import java.util.Scanner; /** * @author Vashon * date:20150410 * * 题目:汉诺塔算法(本 ...

  5. php-PHP Warning: PHP Startup: Invalid library (maybe not a PHP library) 'xxx.so' in Unknown on line 0

    关于xxx.so,今天在安装php的模块时候老是报,xxx.so的问题,虽然不影响使用,但作为一名当年的程序员强迫症患者,誓死要把 他搞清楚,后面发现是删除了也没有影响,因为在安装php的时候已经将他 ...

  6. Oracle的Central Inventory和Local inventory详解

    很多朋友对Oracle的inventory信息不太了解以至遇到相关的问题不知道如何处理,这篇文章我们将详细讲解Oracle的Central Inventory (oraInventory)和Local ...

  7. 2019_京东JAVA实习生招聘机试第一题

    题意抽象出来就是,求根节点的所有子节点中,以这些子节点为根的子树的最大节点数. 已有向图的方式来保存无向图,所以叶子结点i的eage[i].size()==1. import java.util.Ar ...

  8. vscode setting.json

    setting.json { "sync.gist": "#github的码##", "sync.lastUpload": "20 ...

  9. MPP(大规模并行处理)简介

    1. 什么是MPP? MPP (Massively Parallel Processing),即大规模并行处理,在数据库非共享集群中,每个节点都有独立的磁盘存储系统和内存系统,业务数据根据数据库模型和 ...

  10. Navicat连不上MySQL的解决办法

    USE mysql; ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY '{your password}'; ...