1367: [Baltic2004]sequence

Time Limit: 20 Sec  Memory Limit: 64 MB
Submit: 932  Solved: 348
[Submit][Status][Discuss]

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

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 可并堆+中位数的更多相关文章

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

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

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

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

  3. bzoj 1367 - sequence

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

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

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

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

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

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

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

  7. bzoj 1367: [Baltic2004]sequence

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

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

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

  9. sequence(bzoj 1367)

    Description Input Output 一个整数R Sample Input 794820141518 Sample Output 13 HINT 所求的Z序列为6,7,8,13,14,15 ...

随机推荐

  1. Centos6 安装 Redis

    先确认gcc和tcl已经安装 sudo yum install gcc-c++ sudo yum install tcl 解压, 编译和安装 .tar.gz /usr/src/ cd /usr/src ...

  2. Notes: sensitivity & specificity

    terminology: True positive (TP); False positive (FP): originally negative; True negative (TN); False ...

  3. try catch finally的执行顺序(有return的情况下)

    结论:1.不管有木有出现异常,finally块中代码都会执行:2.当try和catch中有return时,finally仍然会执行:3.finally是在return后面的表达式运算后执行的(此时并没 ...

  4. 经典算法和OJ网站(开发者必备-转)

    一. Online Judge简介: Online Judge系统(简称OJ)是一个在线的判题系统.用户可以在线提交程序多种程序(如C.C++.Pascal)源代码,系统对源代码进行编译和执行,并通过 ...

  5. QT QT练习一

    界面中通过三个 QLineEdit控件,一个QPushButton实现+ - * /四则运算,点击pushbutton后将运算结果显示在QLabel控件上. #ifndef WIDGET_H #def ...

  6. oracle:db-link使用

    二个oracle instance,如果需要在一个instance上,直接查询另一个instance上的数据,就要用到db-link 创建: create public database link 链 ...

  7. 数据字典生成工具之旅(3):PowerDesign文件组成结构介绍及操作

    从这篇开始将正式讲解整个重要部分的实现细节,本篇讲解Pdm文件的解析.其实PDM文件就是XML文件,可以用Editplus或者VS打开查看.了解到这一点之后大家就能猜到,可以用解析XML的方式读取PD ...

  8. winform程序自动升级

    可参考下面这个链接,描述挺详细的,下次用的时候试试,感谢牛逼的作者. http://www.fishlee.net/soft/simple_autoupdater/

  9. C#之发送邮件汇总

    最近想搞个网站,其中找回密码用到了我们常见到的利用邮箱找回.利用邮箱的好处是可以有效确认修改密码者的身份. 百度了几篇博客,各有千秋.最终采用了QI Fei同志的博客,有Demo下载,看了看思路清晰, ...

  10. nginx location语法使用说明

    语法规则: location [=|~|~*|^~] /uri/ { … } = 开头表示精确匹配 ^~ 开头表示uri以某个常规字符串开头,理解为匹配 url路径即可.nginx不对url做编码,因 ...