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详解
虚方法virtual详解 从C#的程序编译的角度来看,它和其它一般的函数有什么区别呢?一般函数在编译时就静态地编译到了执行文件中,其相对地址在程序运行期间是不发生变化的,也就是写死了的!而虚函数在 ...
- re匹配语法-match、search和findall
1.re.match() 匹配第一个值 列表里的值可以有多个范围,有一个符合就可以. match只匹配第一个值,所以列表里的范围是第一个值得取值范围.如果第一个值被设定好且存在,那么列表的取值范围变为 ...
- ubuntu下安装redis扩展
采用源码编译安装 下载 下载redis源码包到本地一个临时目录 git clone https://github.com/phpredis/phpredis.git 移动文件到合适目录 mv phpr ...
- Android(java)学习笔记156:开源框架post和get方式提交数据(qq登录案例)
1. 前面提到Http的get/post方式 . HttpClient方式,实际工作的时候不常用到,因为这些方式编写代码是很麻烦的 2. Android应用会经常使用http协议进行传输,网上会有很 ...
- mysql 表锁死的问题
select * from information_schema.innodb_trx; kill 34863;kill 34856;kill 34860;kill 34859;kill 34845; ...
- Spring自动注入的几种方式
---恢复内容开始--- @Service("accountEmailService")public class AccountEmailServiceImpl impleme ...
- 在移动端实现1px的边框
由于分辨率 DPI 的差异,高清手机屏上的 1px 实际上是由 2×2 个像素点来渲染,有的屏幕甚至用到了 3×3 个像素点 所以 border: 1px 在移动端会渲染为 2px 的边框 与设计图产 ...
- 苹果面临起诉:App Store 涉嫌垄断吗?
今日导读 现代社会,手机在手,啥也不愁.购物不用带钱包,网上支付可以搞定一切:旅游懒得背相机,修图 App 帮你打造最美的自己.看直播.学英语.玩游戏.听音乐,我们的生活已经充斥着各类 App.据悉, ...
- ERROR: HHH000091: Expected type: java.sql.Timestamp, actual value: java.lang.Integer
在将MySql的数据对应到Java的实体类的时候, 遇到了错误 关键点: Hibernate从mysql数据库查出来的Timestamp类型的数据, 会被Hibernate(或者是数据库连接池Drui ...
- merge dict key
#!/usr/local/python # -*- coding:utf-8 -*-user_dict = {'python': 23, 'Python': 51, '机器':10, 'PYTHON' ...