sequence(bzoj 1367)
Description

Input

Output
Sample Input
9
4
8
20
14
15
18
Sample Output
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)的更多相关文章
- BZOJ 1367 [Baltic2004]sequence 解题报告
BZOJ 1367 [Baltic2004]sequence Description 给定一个序列\(t_1,t_2,\dots,t_N\),求一个递增序列\(z_1<z_2<\dots& ...
- 【BZOJ 1367】 1367: [Baltic2004]sequence (可并堆-左偏树)
1367: [Baltic2004]sequence Description Input Output 一个整数R Sample Input 7 9 4 8 20 14 15 18 Sample Ou ...
- BZOJ 1367: [Baltic2004]sequence [可并堆 中位数]
1367: [Baltic2004]sequence Time Limit: 20 Sec Memory Limit: 64 MBSubmit: 1111 Solved: 439[Submit][ ...
- bzoj 1367: [Baltic2004]sequence
1367: [Baltic2004]sequence Time Limit: 20 Sec Memory Limit: 64 MB Description Input Output 一个整数R Sa ...
- bzoj 1367 [ Baltic 2004 ] sequence —— 左偏树
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=1367 好题啊!论文上的题: 论文上只给出了不下降序列的求法: 先考虑特殊情况,如果原序列上升 ...
- BZOJ 1367 [Baltic2004]sequence (可并堆)
题面:BZOJ传送门 题目大意:给你一个序列$a$,让你构造一个递增序列$b$,使得$\sum |a_{i}-b_{i}|$最小,$a_{i},b_{i}$均为整数 神仙题.. 我们先考虑b不递减的情 ...
- bzoj 1367 - sequence
Description 给定一个序列\(t_1,t_2,\cdots,t_n\),求一个递增序列\(z_1<z_2<...<z_n\), 使得 \(R=|t_1−z_1|+|t_2− ...
- BZOJ 1367([Baltic2004]sequence-左偏树+中位数贪心)
1367: [Baltic2004]sequence Time Limit: 20 Sec Memory Limit: 64 MB Submit: 521 Solved: 159 [ Subm ...
- 【BZOJ 1049】 1049: [HAOI2006]数字序列 (LIS+动态规划)
1049: [HAOI2006]数字序列 Description 现在我们有一个长度为n的整数序列A.但是它太不好看了,于是我们希望把它变成一个单调严格上升的序列.但是不希望改变过多的数,也不希望改变 ...
随机推荐
- 如何安装Virtual Box的VBox Guest Additions扩展程序
Virtual Box的默认安装是不包含Guest Addition这个扩展的,在实际使用过程中带来种种不便,比如只能通过小窗口访问虚拟机的操作系统,通过默认的右Ctrl切换鼠标,不能和宿主操作系统共 ...
- Linux之用户权限管理
chmod(更改目录或文件权限) 在linux中,文件的权限分为3中,拥有者,群组,其他人.而chmod则是对权限更改的命令. u 表示该文件的拥有者,g 表示与该文件的拥有者属于同一个组,o 表示其 ...
- 油猴 tamperMonkey 在百度首页 添加自己的自定义链接
发现 GM_addStyle 函数不能用了,从写加载css函数. 剩下找个定位 添加内容 就很简单了. // ==UserScript== // @name helloWorld // @namesp ...
- css 两列 左侧列固定 width: 100px; float: left; 右侧列自适应 margin-left:100px; 注意要用在div上的style
css 两列 左侧列固定 width: 100px; float: left; 右侧列自适应 margin-left:100px; 注意要用在div上的style .con1{ width: 100p ...
- [SQL]连续三天有销售额
店铺 销售日期 销售额 A 2017-10-11 300 A 2017-10-12 200 B 2017-10-11 400 B 2017-10-12 200 A 2017-10-13 100 A 2 ...
- 【软件构造】第三章第三节 抽象数据型(ADT)
第三章第三节 抽象数据型(ADT) 3-1节研究了“数据类型”及其特性 ; 3-2节研究了方法和操作的“规约”及其特性:在本节中,我们将数据和操作复合起来,构成ADT,学习ADT的核心特征,以及如何设 ...
- HTML基础(三)图像和超链接
图像 img 元素向网页中嵌入一幅图像. 语法 <img src="" alt="" /> img标签常用属性 src 跳转的url alt 图片不 ...
- 异常:java.lang.NoClassDefFoundError: org/springframework/expression/ParserContext
异常:java.lang.NoClassDefFoundError: org/springframework/expression/ParserContext 解决方案:缺少spring-expres ...
- C# 关于datetime的用法(网上考的)
实例: 用户输入一个日期,要求输出这个日期是星期几和在这一年中的第几天: 复制代码代码如下: //声明一个DateTime类型的变量用于存放用户输入的日期DateTime dt;Console.Wri ...
- 点击增加删除class
<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8" ...