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

[可并堆] Bzoj P1367 sequence

时间:2019-08-19 09:52:04      阅读:73      评论:0      收藏:0      [点我收藏+]

标签:wap   sgd   sequence   baidu   str   EAP   for   ios   src   

Description

技术图片

 

题解

 

代码

 1 #include <cstdio>
 2 #include <iostream>
 3 #include <algorithm>
 4 #define N 1000010
 5 using namespace std;
 6 struct ltree
 7 {
 8     int cnt,l[N],r[N],v[N],sz[N],d[N];
 9     int merge(int x,int y)
10     {
11         if (x==0||y==0) return x+y;
12         if (v[x]<v[y]) swap(x,y);
13         r[x]=merge(r[x],y),sz[x]=sz[l[x]]+sz[r[x]]+1;
14         if (d[r[x]]>d[l[x]]) swap(l[x],r[x]);
15         d[x]=d[r[x]]+1;
16         return x;
17     }
18     int top(int x) { return v[x]; }
19     int size(int x) { return sz[x]; }
20     void pop(int &x) { x=merge(l[x],r[x]); }
21     int new_heap(int x) { v[++cnt]=x,sz[cnt]=1,l[cnt]=r[cnt]=d[cnt]=0; return cnt; }
22 }heap;
23 int n,now,a[N],root[N],L[N],R[N],tot[N];
24 long long ans;
25 int main()
26 {
27     scanf("%d",&n);
28     for (int i=1;i<=n;i++) scanf("%d",&a[i]),a[i]-=i;
29     for (int i=1;i<=n;i++)
30     {
31         now++,root[now]=heap.new_heap(a[i]),tot[now]=1,L[now]=R[now]=i;
32         while (now>1&&heap.top(root[now-1])>heap.top(root[now]))
33         {
34             now--,root[now]=heap.merge(root[now],root[now+1]),tot[now]+=tot[now+1],R[now]=R[now+1];
35             while (heap.size(root[now])*2>tot[now]+1) heap.pop(root[now]);
36         }
37     }
38     for (int i=1;i<=now;i++) for (int j=L[i],t=heap.top(root[i]);j<=R[i];j++) ans+=abs(a[j]-t);
39     printf("%lld",ans);
40 }

 

[可并堆] Bzoj P1367 sequence

标签:wap   sgd   sequence   baidu   str   EAP   for   ios   src   

原文地址:https://www.cnblogs.com/Comfortable/p/11375162.html

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