【BZOJ-1367】sequence 可并堆+中位数
1367: [Baltic2004]sequence
Time Limit: 20 Sec Memory Limit: 64 MB
Submit: 932 Solved: 348
[Submit][Status][Discuss]
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
Source
Solution
论文中的例题,非常吼啊....思想巧妙
Code
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<cstring>
using namespace std;
int read()
{
int x=,f=; char ch=getchar();
while (ch<'' || ch>'') {if (ch=='-') f=-; ch=getchar();}
while (ch>='' && ch<='') {x=x*+ch-''; ch=getchar();}
return x*f;
}
#define maxn 1000100
long long ans;
int n,data[maxn],root[maxn],L[maxn],R[maxn],cnt[maxn],tot;
struct LeftTreeNode
{
int sz,son[maxn][],d[maxn],size[maxn],a[maxn];
// LeftTreeNode ()
// {
// sz=0;memset(son,0,sizeof(son));memset(size,0,sizeof(size));
// memset(cnt,0,sizeof(cnt));memset(a,0,sizeof(a));memset(d,0,sizeof(d));
// }
int Merge(int x,int y)
{
if (!x) return y;
if (!y) return x;
if (a[x]<a[y]) swap(x,y);
son[x][]=Merge(son[x][],y);
size[x]=size[son[x][]]+size[son[x][]]+;
if (d[son[x][]]>d[son[x][]]) swap(son[x][],son[x][]);
d[x]=d[son[x][]]+;
return x;
}
int Push(int x)
{
a[++sz]=x; size[sz]=;
son[sz][]=son[sz][]=d[sz]=;
return sz;
}
int Pop(int x) {return Merge(son[x][],son[x][]);}
int Top(int x) {return a[x];}
int Size(int x) {return size[x];}
}LTHeap;
int main()
{
n=read();
for (int i=; i<=n; i++) data[i]=read()-i;
for (int i=; i<=n; i++)
{
tot++;
root[tot]=LTHeap.Push(data[i]); cnt[tot]=; L[tot]=R[tot]=i;
while (tot> && LTHeap.Top(root[tot])<LTHeap.Top(root[tot-]))
{
tot--;
root[tot]=LTHeap.Merge(root[tot],root[tot+]); cnt[tot]+=cnt[tot+]; R[tot]=R[tot+];
while (LTHeap.Size(root[tot])*>cnt[tot]+)
root[tot]=LTHeap.Pop(root[tot]);
}
}
for (int i=; i<=tot; i++)
for (int j=L[i],top=LTHeap.Top(root[i]); j<=R[i]; j++)
ans+=abs(data[j]-top);
printf("%lld\n",ans);
return ;
}
被YveH发现了...
【BZOJ-1367】sequence 可并堆+中位数的更多相关文章
- 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 Submit: 521 Solved: 159 [ Subm ...
- 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 解题报告
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 (可并堆)
题面:BZOJ传送门 题目大意:给你一个序列$a$,让你构造一个递增序列$b$,使得$\sum |a_{i}-b_{i}|$最小,$a_{i},b_{i}$均为整数 神仙题.. 我们先考虑b不递减的情 ...
- 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 好题啊!论文上的题: 论文上只给出了不下降序列的求法: 先考虑特殊情况,如果原序列上升 ...
- sequence(bzoj 1367)
Description Input Output 一个整数R Sample Input 794820141518 Sample Output 13 HINT 所求的Z序列为6,7,8,13,14,15 ...
随机推荐
- 035医疗项目-模块三:药品供应商目录模块——供货商药品目录(批量)添加药品的功能---------Service
这篇文章我们重点介绍Service层.因为Dao层就是用Gysypml逆向生成的Mapper就可以了.所以这里重点讲解Service层. 业务逻辑如下: 1:我们从前端页面传入有两个值:1:userg ...
- topshelf包装redis为windows服务
topshelf包装redis为windows服务 Redis服务端目前用的是控制台程序运行,部署的时候能作为windows服务后台运行感觉更好.找到一篇文章Running Redis as a Wi ...
- Alpha版本发布说明
软件发布的同时,在团队博客上写一个发布说明 ▪ 列出这一版本的新功能 ▪ 这一版本修复的缺陷 ▪ 对运行环境的要求 ▪ 安装方法 ▪ 描述系统已知的问题和限制 ...
- 轻量级iOS蓝牙库:LGBluetooth(项目中用过的)
LGBluetooth 是个简单的,基于块的,轻量级 CoreBluetooth 库: iOS 6引入了Core Bluetooth,让低功耗蓝牙设备之间的通信变得简单.但如果CoreBluetoot ...
- Java应用程序项目的打包与发行(run.bat形式)
参考: http://www.iteye.com/topic/57312 背景: 以前一直都是在eclipse上面创建应用程序,每次要要运行的时候都要打开eclipse, 直到有个同事叫我帮忙写一个应 ...
- JAVA GC 简单总结
GC分代 GC的英文全拼是Garbage Collection,意思是垃圾收集. Java 将堆内存分为三代来管理: - 年轻代 (Young Generation) - 年老代 (Old Gener ...
- 从 datetime2 数据类型到 datetime 数据类型的转换产生一个超出范围的值
具体的错误原因是:C#中的DateTime类型比SqlServer中的datetime范围大.SqlServer的datetime有效范围是1753年1月1日到9999年12月31日,如果超出这个范围 ...
- [Codevs1403]新三国争霸(MST+DP)
题目:http://codevs.cn/problem/1403/ 分析: 很容易想到对于某个确定的一天,就是求个最小生成树,又因为数据范围很小,所以可以暴力.但问题的关键是如果相邻两天的方案不同,就 ...
- Spring + SpringMVC + MyBatis
1.需求说明实现用户通过数据库验证登录需求,采用Myeclipse+Tomcat 6.0+Mysql 5.0+JDK 1.6 2.数据库表开发所用是Mysql数据库,只建立单张用户表T_USER,表结 ...
- killall 根据名称终止进程
根据名称终止进程 killall [option] name-list killall 将信号发送到一个或多个进程用来终止它.除超级用户外,只有进程的所有者才可以对进程执行killall,超级用户可以 ...