1367: [Baltic2004]sequence

Time Limit: 20 Sec  Memory Limit: 64 MB

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

详细证明请看IOI2005国家集训队论文  黄源河

https://wenku.baidu.com/view/20e9ff18964bcf84b9d57ba1.html

两个极端情况:

1、a[i]<a[i+1]<a[i+2]<a[i+3]……  ans[i]=a[i]

2、a[i]>a[i+1]>a[i+2]>a[i+3]……  ans[i]=ans[i+1]=ans[i+2]=a[i+3]=区间中位数

将每一个点看做一个区间,如果前一个区间的中位数比这个大,则合并

所以我们对于每一段已求好的序列,既要维护它的中位数,又要支持合并

因为我们合并的前提是:中位数(i)>中位数(i+1),那么对于合并后的i而言,中位数肯定是不升的

根据这个性质我们又可以用可并堆了,堆顶元素表示该序列中的中位数

当堆的元素个数*2>序列长度+1的时候就可以弹出堆顶

如何保证严格上升?

常用套路:a[i]-i

#include<cstdio>
#include<algorithm>
#define N 1000001
using namespace std;
int a[N],siz[N],tot[N],root[N],lc[N],rc[N],now,lp[N],rp[N],dis[N];
int merge(int x,int y)
{
if(!x) return y;
if(!y) return x;
if(a[x]<a[y]) swap(x,y);
rc[x]=merge(rc[x],y);
siz[x]=siz[lc[x]]+siz[rc[x]]+;
if(dis[lc[x]]<dis[rc[x]]) swap(lc[x],rc[x]);
dis[x]=dis[rc[x]]+;
return x;
}
int main()
{
int n;
scanf("%d",&n);
for(int i=;i<=n;i++) scanf("%d",&a[i]),a[i]-=i;
for(int i=;i<=n;i++)
{
root[++now]=i;
lp[now]=rp[now]=i;
siz[root[now]]=tot[now]=;
while(now>&&a[root[now-]]>a[root[now]])
{
now--;
rp[now]=rp[now+]; tot[now]+=tot[now+];
root[now]=merge(root[now],root[now+]);
while(siz[root[now]]*>tot[now]+)
root[now]=merge(lc[root[now]],rc[root[now]]);
}
}
long long ans=;
for(int i=;i<=now;i++)
for(int j=lp[i];j<=rp[i];j++)
ans+=abs((long long)a[j]-a[root[i]]);
printf("%lld",ans);
}

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

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

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

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

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

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

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

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

  6. 1367: [Baltic2004]sequence

    1367: [Baltic2004]sequence Time Limit: 20 Sec  Memory Limit: 64 MB Submit: 1090  Solved: 432 [Submit ...

  7. 【BZOJ】1367: [Baltic2004]sequence

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

  8. 【bzoj1367】[Baltic2004]sequence

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

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

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

随机推荐

  1. Beta冲刺第二周王者荣耀交流协会第四次会议

    1.例会照片: 成员:王超,高远博,冉华,王磊,王玉玲,任思佳,袁玥全部到齐. master:王玉玲 2.时间跨度: 2017年11月20日 18:00 — 18:13,总计13分钟. 3.地点: 一 ...

  2. 智能客服 对话实现--python aiml包

    利用了python的aiml包进行应答 什么是AIML? AIML是Richard Wallace开发的. 他开发了一个叫A.L.I.C.E(Artificial Linguistics Intern ...

  3. (Miller Rabin算法)判断一个数是否为素数

    1.约定 x%y为x取模y,即x除以y所得的余数,当x<y时,x%y=x,所有取模的运算对象都为整数. x^y表示x的y次方.乘方运算的优先级高于乘除和取模,加减的优先级最低. 见到x^y/z这 ...

  4. RAR和ZIP:压缩大战真相 (挺赞值得了解)

    前言--王者归来? 等待足足两年之久,压缩霸主WinZip终于在万众期待下发布了9.0正式版.全世界自然一片沸腾,在世界各大知名下载网站中,WinZip9.0再次带起下载狂潮.然而此时国内并没有王者回 ...

  5. HDU 5642 King's Order dp

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5642 King's Order  Accepts: 381  Submissions: 1361   ...

  6. Swift-setValuesForKeysWithDictionary

    重写 setValuesForKeysWithDictionary 那么字典中可以有的字段在类中没有对应属性 class Person : NSObject { var age :Int = // 重 ...

  7. n元一维向量向左循环移位i的几种算法

    1.最简单的算法借助于一个n元的中间向量在n步时间内完成 时间复杂度:O(n)  空间复杂度O(n) void shift_easy(int arr[], int _arr[], int n, int ...

  8. pro9笔记

  9. PAT 甲级 1046 Shortest Distance

    https://pintia.cn/problem-sets/994805342720868352/problems/994805435700199424 The task is really sim ...

  10. PAT 甲级 1019 General Palindromic Number

    https://pintia.cn/problem-sets/994805342720868352/problems/994805487143337984 A number that will be ...