一种不基于中位数的反悔贪心

· · 题解

前置知识:反悔贪心,可并堆。

思路

首先需要理解反悔贪心的做法,以样例为例子。

定义数组 z 表示当前贪心的选最优的。
定义数组 a 表示当前 z_i 减少 1 后 |z_i-a_i| 是否会更小,如果会变小就是 1,会变大就是 -1。

贪心一定要贪,所以 z_1 是 9。

a 数组 t 数组 z 数组
-1 9 9
4
^ 8 ^
^ 20 ^
^ 14 ^
^ 15 ^
^ 18 ^

由于 z_1\lt z_2,而我们这里想要尽量和 t_2 接近,所以 z_2 是 10。

a 数组 t 数组 z 数组
-1 9 9
1 4 10
8
^ 20 ^
^ 14 ^
^ 15 ^
^ 18 ^

我们定义对于一个区间 [l,r],对于每一个 l\le i\lt r 都有 z_i+1=z_{i+1},则称区间 [l,r] 为等差段。
对于一个等差段,如果 \sum^r_{i=l}a_i\ge 0 就说明对这个区间进行区间同时减少一些值是不劣的。
而贪心的想,肯定这个区间的值越少后面的答案会越小,所以每当有这样的情况时都要区间减。
比如这个时候区间 [1,2] 是等差段,而且 a_1+a_2\ge 0 ,则考虑将这一段的 z 区间减少直到 a_1+a_2\lt0。
那么这一段区间减 6 之后,此时 z_2=4,所以 |z_2-t_2|\le|(z_2-1)-t_2|,所以 a_2=-1,a_1+a_2\lt0,停止减少。

a 数组 t 数组 z 数组
-1 9 3
-1 4 4
8
^ 20 ^
^ 14 ^
^ 15 ^
^ 18 ^

接下来两个是上升子序列,可以直接贪。

a 数组 t 数组 z 数组
-1 9 3
-1 4 4
-1 8 8
-1 20 20
14
^ 15 ^
^ 18 ^

和第二步一样,z_5 只能填 21。

a 数组 t 数组 z 数组
-1 9 3
-1 4 4
-1 8 8
-1 20 20
1 14 21
15
^ 18 ^

这个时候区间 [4,5] 是等差段,而 a_4+a_5=0,所以我们减少这一段直到 z_5=14,此时 a_5=-1,停止减少。

a 数组 t 数组 z 数组
-1 9 3
-1 4 4
-1 8 8
-1 20 13
-1 14 14
15
^ 18 ^

剩下两个直接贪。

a 数组 t 数组 z 数组
-1 9 3
-1 4 4
-1 8 8
-1 20 13
-1 14 14
-1 15 15
-1 18 18

最终的 z 数组就是题目要求的那个数组。

总结一下,贪心策略就是先尽量贪心的填下一个值,然后如果最后面出现了 \sum^r_{i=l}a_i\ge 0 的等差段。
就用堆来维护 a_i=1 到 a_i=-1 所需减少最小的一个数(实际上是维护 z_i-a_i\gt0 的最小值),取出来之后区间减。
有一种情况会有问题就是这一段减了之后比上一段小,如果出现这种情况就先减到可以跟上一段组成一个更大的等差段,可以用可并堆实现。

正确性比较显然,每次新增节点都是当前最优的,而每次区间减都不劣,但是作者不会严谨证明,留作习题。

代码

丑丑的代码,可并堆用斜堆实现。

#include<bits/stdc++.h>
using namespace std;
#ifdef ONLINE_JUDGE
char buf[1 << 23] , * p1 = buf , * p2 = buf , obuf[1 << 23] , * O = obuf;
#define getchar() (p1==p2&&(p2=(p1=buf)+fread(buf,1,1<<21,stdin),p1==p2)?EOF:*p1++)
#endif
inline int rd() {
    int x = 0 , f = 1;char ch = getchar();
    while (!isdigit(ch)) { if (ch == '-') f = -1;ch = getchar(); }
    while (isdigit(ch)) x = x * 10 + (ch ^ 48) , ch = getchar();
    return x * f;
}
const int N=1e6+9;
int f[N],zc[N],rt[N],ls[N],rs[N];
long long bh[N],sc[N],z[N];
void pt(int x){
    if(sc[x]){
        if(ls[x])sc[ls[x]]+=sc[x],z[ls[x]]-=sc[x];
        if(rs[x])sc[rs[x]]+=sc[x],z[rs[x]]-=sc[x];
        sc[x]=0;
    }
}
int hb(int x,int y){
    if((!x)||(!y))return x|y;
    if(z[x]>z[y])swap(x,y);
    pt(x),pt(y);
    swap(ls[x],rs[x]);
    ls[x]=hb(ls[x],y);
    return x;
}
long long an[N],cf[N];
long long yb[N];
int gf(int x){
    if(f[x]!=x)f[x]=gf(f[x]);
    return f[x];
}
int main(){
    ios::sync_with_stdio(0);
    long long a=rd(),no=-5e9,zq=0,ans=0;
    for(int i=1;i<=a;i++)f[i]=i;
    for(int i=1,x;i<=a;i++){
        yb[i]=x=rd();
        if(x-no>1){
            bh[i]=x-no-1,zc[i]--;
            f[i]=i,no=x,an[i]=x;
            continue;
        }
        no++;
        f[i]=f[i-1],an[i]=no;
        int t=gf(i);
        zc[t]--,z[i]=no-x; 
        if(z[i]>0)rt[t]=hb(rt[t],i),zc[t]+=2;
        while(rt[t]&&zc[t]>=0){
            int x=z[rt[t]];
            if(t!=1&&bh[t]<=x){
                if(rt[t]){
                    x=bh[t];
                    cf[t]-=x,cf[i+1]+=x,no-=x;
                    bh[t]-=x;
                    if(!z[rt[t]]){
                        zc[t]-=2;
                        pt(rt[t]);
                        rt[t]=hb(ls[rt[t]],rs[rt[t]]);
                    }
                    sc[rt[t]]+=x,z[rt[t]]-=x;
                }
                f[t]=t-1;
                int p=gf(t);
                rt[p]=hb(rt[p],rt[t]);
                zc[p]+=zc[t];
                t=p;
                continue;
            }
            cf[t]-=x,cf[i+1]+=x,bh[t]-=x;
            zc[t]-=2,no-=x;
            pt(rt[t]);
            rt[t]=hb(ls[rt[t]],rs[rt[t]]);
            if(rt[t])sc[rt[t]]+=x,z[rt[t]]-=x;
        }
    }
    for(int i=1;i<=a;i++)cf[i]+=cf[i-1],an[i]+=cf[i];
    for(int i=1;i<=a;i++)ans=ans+abs(yb[i]-an[i]);
    cout<<ans<<'\n';
    for(int i=1;i<=a;i++)cout<<an[i]<<'\n';
    return 0;
}

总时间复杂度为 O(n\log n),因为总共最多合并 n 次段,每个点最多进堆一次,出堆一次。