bzoj1367: [Baltic2004]sequence

#include<cstdio>
#include<iostream>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<cmath>
using namespace std;
typedef long long LL;

int cnt,L[1100000],R[1100000];
struct heap{int lc,rc,dep,tot;LL c;}h[1100000];int len,rt[1100000];
{
len++;
h[len].lc=h[len].rc=0;h[len].c=d;
h[len].dep=1;h[len].tot=1;

rt[++cnt]=len;
L[cnt]=R[cnt]=x;
}
int merge(int x,int y)
{
if(x==0||y==0)return x+y;
if(h[x].c<h[y].c)swap(x,y);

h[x].tot+=h[y].tot;
h[x].rc=merge(h[x].rc,y);
if(h[h[x].lc].dep<h[h[x].rc].dep)swap(h[x].lc,h[x].rc);
h[x].dep=h[h[x].lc].dep+1;
return x;
}
void pop(int x){rt[x]=merge(h[rt[x]].lc,h[rt[x]].rc);}
void change(int x,int y)
{
int num=(R[x]-L[x]+2)/2;//堆里面扔的数个数
rt[x]=merge(rt[x],rt[y]);
while(h[rt[x]].tot>num)pop(x);
}

LL a[1100000];
int main()
{
freopen("a.in","r",stdin);
freopen("a.out","w",stdout);
int n;
scanf("%d",&n);
for(int i=1;i<=n;i++)
scanf("%lld",&a[i]), a[i]-=i;
len=0;cnt=0;
memset(rt,0,sizeof(rt));
for(int i=1;i<=n;i++)
{
while(cnt>1&&h[rt[cnt-1]].c>h[rt[cnt]].c)
cnt--,R[cnt]=R[cnt+1],change(cnt,cnt+1);
}
LL sum=0;
for(int i=1;i<=cnt;i++)
for(int j=L[i];j<=R[i];j++)
sum+=abs(a[j]-h[rt[i]].c);
printf("%lld\n",sum);

return 0;
}

pain and happy in the cruel world.
posted @ 2018-12-21 20:58  AKCqhzdy  阅读(135)  评论(0编辑  收藏  举报