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. mysql--测试前缀索引能否用于order by 或者 group by

    创建一个测试用表 mysql> desc two; +-------+-------------+------+-----+---------+----------------+ | Field ...

  2. 3098: Hash Killer II

    3098: Hash Killer II Time Limit: 5 Sec  Memory Limit: 128 MBSec  Special JudgeSubmit: 1219  Solved:  ...

  3. wap页面笔记

    wap页面是自行应页面 必须在head中加入 <meta content="width=device-width, initial-scale=1.0, maximum-scale=1 ...

  4. NOI2018准备Day19

    5天没写. 伸展树  主席树 3到线段树模板题

  5. c++返回值 注意事项

    1.不要返回指向局部变量或临时对象的引用.函数执行完毕后,局部变量和临时对象会消失,引用将指向不存在的数据 2.返回指向const对象的引用 使用const引用的常见原因是旨在提高效率,但对于何时采用 ...

  6. Jquery 页面首次加载方式

    $(document).ready(function(){ alert("111"); }); $(function(){ alert("222"); }); ...

  7. 深入grootJs(进阶教程)

    深入grootJs 这篇教程的原则是把grootJs原理讲透,主要真正理解了原理才能用起来随心所欲 mvvm模式简介 grootJs的vm结构 扫描函数sweep 垃圾回收的原理 加载器中的预编 ,控 ...

  8. Android Stduio统计项目的代码行数

    android studio统计项目的代码行数的步骤如下: 1)按住Ctrl+Shift+A,在弹出的框输入‘find’,然后选择Find in Path.(或者使用快捷键Ctrl+Shift+F) ...

  9. checkbox js onclick ajax,列表页表格中修改数据

    <input type='checkBox' value='".$row["p_id"]."' onclick='changeisNew(this);'& ...

  10. setTimeout和setinterval的区别

    setTimeout("alert('久等了')",2000)是等待多长时间开始执行函数 setinterval(fn,1000)是每隔多长时间执行一次函数 setTimeout和 ...