题意:中文题。。
代码:
#include<map>
#include<set>
#include<cmath>
#include<queue>
#include<math.h>
#include<cstdio>
#include<vector>
#include<string>
#include<cstring>
#include<iostream>
#include<algorithm>
#pragma comment(linker, "/STACK:102400000,102400000")
using namespace std;
const int N=1000010;
const int MAX=151;
const int MOD=1000000007;
const int MOD1=100000007;
const int MOD2=100000009;
const int INF=2100000000;
const double EPS=0.00000001;
typedef long long ll;
typedef unsigned long long uI64;
int read()
{
int x=0,f=1;char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
return x*f;
}
int a[N],h[N],lc[N],rc[N],fa[N];
struct node {
int l,r,t,s;
}f[N];
int mergee(int x,int y) {
if (!x||!y) return x+y;
if (a[x]<a[y]) { x^=y;y^=x;x^=y; }
rc[x]=mergee(rc[x],y);
if (h[rc[x]]>=h[lc[x]]) { lc[x]^=rc[x];rc[x]^=lc[x];lc[x]^=rc[x]; }
h[x]=h[rc[x]]+1;
return x;
}
int main()
{
int i,j,k,n;
ll ans=0;
scanf("%d", &n);
for (i=1;i<=n;i++) {
scanf("%d", &a[i]);a[i]-=i;
}
k=0;h[0]=-1;
for (i=1;i<=n;i++) {
fa[i]=i;h[i]=lc[i]=rc[i]=0;
}
for (i=1;i<=n;i++) {
k++;f[k].s=1;
f[k].l=f[k].r=f[k].t=i;
while (k>1&&a[f[k].t]<a[f[k-1].t]) {
fa[f[k].t]=fa[f[k-1].t]=mergee(f[k].t,f[k-1].t);
f[k-1].t=fa[f[k-1].t];
f[k-1].s+=f[k].s;f[k-1].r=f[k].r;
//printf("%d %d %d %d %d %d ", i, k, f[k-1].t, f[k-1].s, f[k-1].l, f[k-1].r);
while (f[k-1].s>(f[k-1].r-f[k-1].l+2)/2) {
//printf(" * ");
fa[f[k-1].t]=mergee(lc[f[k-1].t],rc[f[k-1].t]);
fa[fa[f[k-1].t]]=fa[f[k-1].t];f[k-1].t=fa[f[k-1].t];
f[k-1].s--;
}
k--;
//printf(" %d %d %d %d %d xxxx\n\n", k, f[k].s, f[k].l, f[k].r, f[i].t);
}
}
//for (i=1;i<=k;i++) printf("%d %d %d %d xxx\n", f[i].l, f[i].r, f[i].s, f[i].t);
//printf("%d xxxx\n", k);
for (i=1;i<=k;i++)
for (j=f[i].l;j<=f[i].r;j++) ans+=(ll)abs((ll)a[f[i].t]-a[j]);
printf("%lld\n", ans);
return 0;
}
/*
7
9 4 8 20 14 15 18
5
1 2 3 4 5
5
5 4 3 2 1
10
876 85765 7665 87656 78765 76454 975675 7448 67546 7687
10
98665567 89856675 785656655 654754765 5646577 764545648 54645777 546547889 75484548 785675675
*/