1367: [Baltic2004]sequence

Time Limit: 20 Sec  Memory Limit: 64 MB

Submit: 1090  Solved: 432
[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

 

【题解】:

详见:《左偏树的特点及其应用》(P18-20)

【代码】:

//左偏树,最短的高级数据结构了
#include<cstdio>
#include<cstdlib>
#include<iostream>
using namespace std;
const int N=1e6+;
int n,tot,count,root[N],l[N],r[N],z[N],num[N],cnt[N];
struct node{
int l,r,dis,w;
}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].l].dis<heap[heap[a].r].dis)
swap(heap[a].l,heap[a].r);
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",&z[i]),z[i]-=i;
for(int i=;i<=n;i++){
++tot;
l[tot]=r[tot]=i;
num[tot]=cnt[tot]=;
root[tot]=++count;
heap[count].w=z[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(z[j]-w);
cout<<ans;
return ;
}

 

 

1367: [Baltic2004]sequence的更多相关文章

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

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

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

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

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

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

  4. bzoj 1367: [Baltic2004]sequence

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

  5. 【BZOJ】1367: [Baltic2004]sequence

    题意 给\(n(n \le 10^6)\)个数的序列\(a\),求一个递增序列\(b\)使得\(\sum_{i=1}^{n} |a_i-b_i|\)最小. 分析 神题啊不会. 具体证明看黄源河论文&l ...

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

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

  7. 【bzoj1367】[Baltic2004]sequence

    2016-05-31 17:31:26 题目:http://www.lydsy.com/JudgeOnline/problem.php?id=1367 题解:http://www.cnblogs.co ...

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

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

  9. 【BZOJ1367】[Baltic2004]sequence 左偏树

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

随机推荐

  1. net9:图片变成二进制流存入XML文档,从XML文档中读出图片以及从XML文档中读取并创建图片文件

    原文发布时间为:2008-08-10 -- 来源于本人的百度文章 [由搬家工具导入] fileToXml类: using System;using System.Data;using System.C ...

  2. Why do I keep getting mixed tabs and spaces in a Visual Studio C# code window?[vs power tools issue transfered]

    goto tools->option->power tools-> turn "use mixed tabs" option to off. you won`t ...

  3. wpf LookUpEdit PopupContentTemplate

    <dxg:LookUpEdit Name="searchLookUpEdit" HorizontalAlignment="Stretch" PopupHe ...

  4. 天梯赛 - L2-002 链表去重

    GG思密达,第二个测试点的三分怎么也拿不上,我还是比较熟悉指针,用指针来写~,写完去上概率论 题目链接:https://www.patest.cn/contests/gplt/L2-002 #incl ...

  5. Codeforces 932 B.Recursive Queries-前缀和 (ICM Technex 2018 and Codeforces Round #463 (Div. 1 + Div. 2, combined))

    B. Recursive Queries   time limit per test 2 seconds memory limit per test 256 megabytes input stand ...

  6. HDU 2874 Connections between cities(LCA)

    题目链接 Connections between cities LCA的模板题啦. #include <bits/stdc++.h> using namespace std; #defin ...

  7. Concurrency and Application Design (一)

    在计算机发展的早期,单位工作时间的最高限额是一台计算机可以执行通过CPU的时钟速度确定.但是,随着技术的进步和处理器设计变得更紧凑,热等物理约束开始限制处理器的最高时钟速度.因此,芯片制造商寻找其它的 ...

  8. win7 32 c++环境

    http://jingyan.baidu.com/article/455a99509c76d8a1662778f6.html 首先我们先来到这个网址下载MinGW的下载程序,百度搜索官网即可.下载之后 ...

  9. npm、yarn、pnpm

    它们都是当前主流的包管理工具 pnpm:https://github.com/pnpm/pnpm yarn: https://github.com/yarnpkg/yarn npm: https:// ...

  10. 关于C++ 命名空间Namespace 的解析

    使用命名空间的目的是对标识符的名称进行本地化,以避免命名冲突.在C++中,变量.函数和类都是大量存在的.如果没有命名空间,这些变量.函数.类的名称将都存在于全局命名空间中,会导致很多冲突.比如,如果我 ...