跳到正文

题解

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

Smile_Hammer约 8 分钟
原文

这是 2026年6月30日 存档的第 1 版,不是当前版本。查看当前版本

文章信息与版本

文章信息

发布
2026年6月29日
收录
2026年6月30日
更新
2026年11月11日
篇幅
约 8 分钟
题目
P4331

版本

  1. v2当前
  2. v1

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

思路

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

定义数组 zz 表示当前贪心的选最优的。
定义数组 aa 表示当前 ziz_i 减少 11 后 ∣zi−ai∣|z_i-a_i| 是否会更小,如果会变小就是 11,会变大就是 −1-1。

贪心一定要贪,所以 z1z_1 是 99。

a数组t数组z数组
-199
4
8
20
14
15
18

由于 z1<z2z_1\lt z_2,而我们这里想要尽量和 t2t_2 接近,所以 z2z_2 是 1010。

a数组t数组z数组
-199
1410
8
20
14
15
18

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

a数组t数组z数组
-193
-144
8
20
14
15
18

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

a数组t数组z数组
-193
-144
-188
-12020
14
15
18

和第二步一样,z5z_5 只能填 2121。

a数组t数组z数组
-193
-144
-188
-12020
11421
15
18

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

a数组t数组z数组
-193
-144
-188
-12013
-11414
15
18

剩下两个直接贪。

a数组t数组z数组
-193
-144
-188
-12013
-11414
-11515
-11818

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

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

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

代码

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

C++
#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(nlog⁡n)O(n\log n),因为总共最多合并 nn 次段,每个点最多进堆一次,出堆一次。