码迷,mamicode.com
首页 > 其他好文 > 详细

bzoj 1367: [Baltic2004]sequence

时间:2017-05-17 23:26:28      阅读:231      评论:0      收藏:0      [点我收藏+]

标签:lld   int   https   如何   main   seq   border   limit   alt   

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]]+1;
    if(dis[lc[x]]<dis[rc[x]]) swap(lc[x],rc[x]);
    dis[x]=dis[rc[x]]+1;
    return x;
}
int main()
{
    int n;
    scanf("%d",&n);
    for(int i=1;i<=n;i++) scanf("%d",&a[i]),a[i]-=i;
    for(int i=1;i<=n;i++)
    {
        root[++now]=i;
        lp[now]=rp[now]=i;
        siz[root[now]]=tot[now]=1;
        while(now>1&&a[root[now-1]]>a[root[now]])
        {
            now--;
            rp[now]=rp[now+1]; tot[now]+=tot[now+1];
            root[now]=merge(root[now],root[now+1]);
            while(siz[root[now]]*2>tot[now]+1)
              root[now]=merge(lc[root[now]],rc[root[now]]);
        }
    }
    long long ans=0;
    for(int i=1;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

标签:lld   int   https   如何   main   seq   border   limit   alt   

原文地址:http://www.cnblogs.com/TheRoadToTheGold/p/6869859.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!