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. webfrom ASP开发基础跟模式

    ASP.NET - .net开发网站应用程序的技术总称 ASP WebForm           MVC   是ASP.NET的两个技术方法 WebForm类似于WinForm,可视化操作 MVC类 ...

  2. hihocoder offer收割编程练习赛9 B 水陆距离

    思路: 宽搜,多个起点. 实现: #include <iostream> #include <cstdio> #include <algorithm> #inclu ...

  3. python绘图工具包 matplotlib 中文乱码问题

    环境: python2.7 windows 8.1 解决: 改配置文件,把字体改为支持中文的字体. 找到python安装目录下的 \Lib\site-packages\matplotlib\mpl-d ...

  4. sed.exe 在bat中使用时,需要另外起一个文件

    今天在windows使用sed.exe时,同一个文件死活不生效,然后换了一个bat,再来调用,就可以了,怀疑跟sed.exe的代码有关.有时间再研究

  5. Ajax的项目搭建

    在搭建Ajax项目之前,首先我们的安装nginx,因为Ajax是基于nginx来运行的, 1.安装nginx 和基本的语法 http://nginx.org/ 上面的nginx的官网,下载直接安装就好 ...

  6. Java编译时根据调用该方法的类或对象所属的类决定

    class Base{     int x = 1;     static int y = 2; } class Subclass extends Base{     int x = 4;     i ...

  7. Debian9镜像安装问题

    Debian9下载地址 https://www.debian.org/distrib/ Debian9有三个镜像文件 第一个包含系统2.3两个主要是一些软件的安装包只需下载第一个安装系统即可 默认安装 ...

  8. Linux基础命令——查看进程命令

    linux是一个 多进程   多用户的操作系统 ps(显示当前进程的状态) ps -ef  查看当前linux 进程 ps -ef | grep 'mysqld'  过滤mysql的进程 (grep  ...

  9. echo - 显示一行文本

    SYNOPSIS(总览) echo[OPTION]... [STRING]... DESCRIPTION(描述) 允许在标准输出上显示STRING(s). -n 不输出行尾的换行符. -e 允许对下面 ...

  10. ssget使用方法

    语法: (ssget [sel-method] [pt1 [pt2]] [pt-list] [filter-list]) ssget 的参数均为可选参数,需要注意的是可选参数之间的组合条件.以下语法表 ...