跳到正文

个人记录

SESOJ--贪心专题进阶

Bryan1676约 24 分钟
原文
文章信息与版本

文章信息

发布
2026年9月9日
收录
2026年9月10日
更新
2026年11月12日
篇幅
约 24 分钟

版本

  1. v2当前
  2. v1

SESOJ—贪心专题进阶

T1 [CSP-S 2019] 树上的数

显然,每个数应该贪心的找能走到的编号最小的点。

考虑怎么去刻画一条路径,不难看出一条路径(u1,u2,…,umu_1,u_2,\dots,u_m)会带来下面的几个性质:

  1. (u1,u2u_1,u_2)是点 u1u_1 上第一条被删除的边。
  2. ∀i∈(1,m)\forall i \in (1,m),在节点 uiu_i 上,(ui−1,ui),(u_{i-1},u_i) 恰好在 (ui,ui+1)(u_i,u_{i+1}) 前面删除,且中间不能删除其它的边。
  3. (um−1,umu_{m-1},u_m)是点 umu_m 上最后一条被删除的边。

考虑采用并查集维护删除边的顺序,具体的,如果边 uu 恰好在边 vv 前面删除,uu 在并查集上向 vv 连边。

对于每个树上的点 ii 建虚点 ii,ii 向这个点第一条删去的边连边,这个点上最后一个删去的边向 ii 连边。

两遍 dfs,第一遍求出所有可达的点,第二遍将可达的编号最小的点到当前点路径上更新。

不难发现只要维护两种操作:将点与点的顺序确定,查询是否可以新增边,因此封装两个函数(merge,check)即可,复杂度 O(tn2α(n))\mathcal O(tn^2 \alpha (n))

本题略微卡常,擦着时限通过。

code
C++
#include<bits/stdc++.h>
using namespace std;

#define int long long
#define PII pair<int,int>
#define fi first
#define se second
#define mk make_pair
#define pb push_back
#define ivoid inline void
ivoid wsp(int x){
	cout<<x<<" ";
}
ivoid wln(int x){
	cout<<x<<"\n";
}
ivoid wstr(string s){
	cout<<s<<"\n";
}
#define cout cerr

const int maxn=6e3+10;
int T,n,p[maxn],deg[maxn],in[maxn],out[maxn],tot,res;
//a -> b ~ in the delete order,a is in front of b
//vi point -> a:a is the first
//a -> vi point:a is the last
//out: if x connect to other
//in: if other connect to x
vector<int> e[maxn];
struct My_hash{
	const int operator()(const PII a)const{
		return a.fi*998244353+a.se;
	}
};
unordered_map<PII,int,My_hash> M;
PII g[maxn];

struct DSU{
	int fa[maxn],sz[maxn];
	inline void init(){for(int i=1;i<=tot;i++) fa[i]=i,sz[i]=1;}
	int find(int u){return fa[u]==u?u:(fa[u]=find(fa[u]));}
	inline void merge(int x,int y){
//		cout<<"merge:"<<x<<" "<<y<<"\n";
		int u=find(x),v=find(y);
		if(u==v) return;
		sz[v]+=sz[u];
		fa[u]=v;
		out[x]=in[y]=1;
	}
	inline bool check(int x,int y,int d){//d:degree+1,check if x can be connect to y
//		cout<<"check:"<<x<<" "<<y<<" "<<d<<"\n";
		if((x<=n||y<=n)&&d==2) return 1;
		if(out[x]||in[y]) return 0;
		int u=find(x),v=find(y);
//		cout<<"u,v:"<<u<<" "<<v<<"\n";
		if(u==v&&sz[u]!=d) return 0;
		return 1;
	}
}dsu;

void dfs1(int x,int f){
//	cout<<"dfs1:"<<x<<"\n";
	if(x!=f&&dsu.check((f^1),x,deg[x]+1)) res=min(res,x);
	for(auto v:e[x]){
		int id=M[mk(x,v)];
		if((id^1)==f) continue;
		if(dsu.check(f<=n?f:(f^1),id,deg[x]+1)) dfs1(v,id);
	}
}

bool dfs2(int x,int pre,int tar){
	if(x==tar){
		dsu.merge((pre^1),x);
		return 1;
	}
	for(auto v:e[x]){
		int id=M[mk(x,v)];
		if((id^1)==pre) continue;
		if(dfs2(v,id,tar)){
//			cout<<x<<" -> "<<v<<"\n";
			dsu.merge(pre<=n?pre:(pre^1),id);
			return 1;
		}
	}
	return 0;
}

signed main(){
//	freopen("tree.in","r",stdin);
//	freopen("tree.out","w",stdout);
	ios::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	cin>>T;
	while(T--){
		M.clear();
		memset(deg,0,sizeof(deg));
		memset(in,0,sizeof(in));
		memset(out,0,sizeof(out));
		cin>>n;
		if(n&1) tot=n+2;
		else tot=n+1;
		for(int i=1;i<=n;i++) cin>>p[i],e[i].clear();
		for(int i=1;i<n;i++){
			int u,v;
			cin>>u>>v;
			e[u].pb(v);
			e[v].pb(u);
			M[mk(u,v)]=++tot;
			g[tot]=mk(u,v);
			M[mk(v,u)]=++tot;
			g[tot]=mk(v,u);
			deg[u]++,deg[v]++;
		}
		dsu.init();
		for(int i=1;i<=n;i++){
//			cout<<"get:"<<i<<"\n";
//			if(i==2) cout<<"-------------"<<dsu.check(1,M[mk(1,4)],3)<<"\n";
			res=n+1;
			dfs1(p[i],p[i]);
			int x=dfs2(p[i],p[i],res);
			if(i!=n) wsp(res);
			else wln(res);
		}
	}
	return 0;
}

T2 [CSP-S 2019] 划分

首先有一个很 native 的 O(n3)\mathcal O(n^3) 的 dp。记 fi,jf_{i,j},表示考虑前 ii 个数,上一段的开端在 jj 处的最小代价,转移考虑枚举决策点。

考虑如何优化,记每个点 ii 的决策点为 posipos_i,不难发现它应该是最大的满足 sum(posj+1,j)≤sum(j+1,i)sum(pos_j+1,j) \le sum(j+1,i) 的 jj。

考虑消掉 dp 的第二维,只考虑第一维,建立辅助数组 pospos 维护决策点,枚举决策点转移复杂度 O(n2)\mathcal O(n^2),而且一看这个形式就很好优化。

发掘可转移的决策点有什么性质,不难发现必然满足 si−sj≥sj−sposjs_i-s_j \ge s_j-s_{pos_j},即 si≥2sj−sposjs_i \ge 2s_j-s_{pos_j}。

发现可以单调队列维护,转移考虑在单调队列上二分,复杂度 O(nlog⁡n)\mathcal O(n\log n)。

再次尝试发掘性质,发现 pospos 单调递增,因此不用二分,直接套双指针即可,复杂度线性。

code
#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/hash_policy.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/pb_ds/trie_policy.hpp>
#include <ext/pb_ds/priority_queue.hpp>
using namespace std;

#define ui unsigned int
#define PII pair<int,int>
#define fi first
#define se second
#define mk make_pair
#define pb push_back
#define ivoid inline void
ivoid wsp(int x){
	cout<<x<<" ";
}
ivoid wln(int x){
	cout<<x<<"\n";
}
ivoid wstr(string s){
	cout<<s<<"\n";
}

const int maxn=4e7+10;
int n,type,stac[maxn],pos[maxn],hd=1,tl=0;
ui a[maxn];
long long s[maxn];

namespace IO{
	const int maxm=1e5+10;
	int x,y,z,m,p[maxm],l[maxm],r[maxm];
	void read(){
		cin>>x>>y>>z>>a[1]>>a[2]>>m;
		for(int i=3;i<=n;i++) a[i]=(x*a[i-1])+(y*a[i-2])+z;
		for(int i=1;i<=n;i++) a[i]=(a[i]<<2)>>2;
		for(int i=1;i<=m;i++){
			cin>>p[i]>>l[i]>>r[i];
			for(int j=p[i-1]+1;j<=p[i];j++){
				a[j]=a[j]%(r[i]-l[i]+1)+l[i];
				s[j]=s[j-1]+a[j];
			}
		}
	}
}

__int128 ans=0;
void dfs(int x){
	if(x==0) return;
	else{
//		cout<<g[x]<<"\n";
		ans+=__int128(s[x]-s[pos[x]])*__int128(s[x]-s[pos[x]]);
		dfs(pos[x]);
	}
}
void write(__int128 x){
	if(x<10){
		cout<<(int)x;
		return;
	}
	else{
		write(x/10);
		cout<<(int)(x%10);
	}
}

signed main(){
	ios::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	cin>>n>>type;
	if(type==0){
		for(int i=1;i<=n;i++) cin>>a[i],s[i]=s[i-1]+a[i];
	}
	else{
		IO::read();
	}
	stac[++tl]=0;
	for(int i=1;i<=n;i++){
		int now=hd;
		while(now<=tl&&2*s[stac[now]]-s[pos[stac[now]]]<=s[i]) now++;
		now--;
		hd=now;
		now=stac[now];
		pos[i]=now;
		while(tl>=hd&&2*s[stac[tl]]-s[pos[stac[tl]]]>=2*s[i]-s[now]) tl--;
		stac[++tl]=i;
	}
	dfs(n);
	write(ans);
	return 0;
}
#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/hash_policy.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/pb_ds/trie_policy.hpp>
#include <ext/pb_ds/priority_queue.hpp>
using namespace std;

#define ui unsigned int
#define PII pair<int,int>
#define fi first
#define se second
#define mk make_pair
#define pb push_back
#define ivoid inline void
ivoid wsp(int x){
	cout<<x<<" ";
}
ivoid wln(int x){
	cout<<x<<"\n";
}
ivoid wstr(string s){
	cout<<s<<"\n";
}

const int maxn=4e7+10;
int n,type,stac[maxn],pos[maxn],hd=1,tl=0;
ui a[maxn];
long long s[maxn];

namespace IO{
	const int maxm=1e5+10;
	int x,y,z,m,p[maxm],l[maxm],r[maxm];
	void read(){
		cin>>x>>y>>z>>a[1]>>a[2]>>m;
		for(int i=3;i<=n;i++) a[i]=(x*a[i-1])+(y*a[i-2])+z;
		for(int i=1;i<=n;i++) a[i]=(a[i]<<2)>>2;
		for(int i=1;i<=m;i++){
			cin>>p[i]>>l[i]>>r[i];
			for(int j=p[i-1]+1;j<=p[i];j++){
				a[j]=a[j]%(r[i]-l[i]+1)+l[i];
				s[j]=s[j-1]+a[j];
			}
		}
	}
}

__int128 ans=0;
void dfs(int x){
	if(x==0) return;
	else{
//		cout<<g[x]<<"\n";
		ans+=__int128(s[x]-s[pos[x]])*__int128(s[x]-s[pos[x]]);
		dfs(pos[x]);
	}
}
void write(__int128 x){
	if(x<10){
		cout<<(int)x;
		return;
	}
	else{
		write(x/10);
		cout<<(int)(x%10);
	}
}

signed main(){
	ios::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	cin>>n>>type;
	if(type==0){
		for(int i=1;i<=n;i++) cin>>a[i],s[i]=s[i-1]+a[i];
	}
	else{
		IO::read();
	}
	stac[++tl]=0;
	for(int i=1;i<=n;i++){
		int now=hd;
		while(now<=tl&&2*s[stac[now]]-s[pos[stac[now]]]<=s[i]) now++;
		now--;
		hd=now;
		now=stac[now];
		pos[i]=now;
		while(tl>=hd&&2*s[stac[tl]]-s[pos[stac[tl]]]>=2*s[i]-s[now]) tl--;
		stac[++tl]=i;
	}
	dfs(n);
	write(ans);
	return 0;
}

「NOIP2021」方差

注意到操作等价于交换差分数组,考虑所有数先全部减去 a1a_1。

考虑到我们有:

s2‾=n∑ai+(∑ai)2n2\overline{s^2}=\frac{n\sum a_i+(\sum a_i)^2}{n^2}

打表可知在最优解时差分数组应呈单谷(证明考虑感性理解应该使数列尽可能“均匀”)。

考虑将差分数组排序,从小到大 dpdp,记录状态 dpi,jdp_{i,j} 表示考虑到第 ii 个数,目前所有 aa 的和为 jj 的最小差分值。

转移考虑讨论插在左侧还是右侧,此时时空复杂度都是 (n2a)\mathcal (n^2a),考虑怎么优化,空间上,明显发现 ii 一维可以滚动数组优化,时间上,发现当 d=0d=0 时无需转移,最终时间复杂度 O(namin⁡(n,a))\mathcal O(na \min(n,a)),空间复杂度 O(na)\mathcal O(na),其中 aa 是值域范围。

code
C++
#include<bits/stdc++.h>
using namespace std;

#define int long long
#define PII pair<int,int>
#define fi first
#define se second
#define mk make_pair
#define pb push_back

const int maxn=1e6+10;
int n,a[maxn],d[maxn],s[maxn];
int dp[2][maxn];
inline void chkmin(int &a,int b){
	a=min(a,b);
}

signed main(){
	freopen("variance.in","r",stdin);
	freopen("variance.out","w",stdout);
	ios::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	cin>>n;
	for(int i=1;i<=n;i++) cin>>a[i];
	for(int i=1;i<n;i++) d[i]=a[i+1]-a[i];
	int ma=a[n]*n;n--;
	sort(d+1,d+n+1);
	for(int i=1;i<=n;i++) s[i]=s[i-1]+d[i];
	int now=0;
	memset(dp,0x3f,sizeof(dp));
	dp[0][0]=0;
	for(int i=1;i<=n;i++){
//		cout<<d[i]<<"\n";
		if(d[i]==0) continue;
		now^=1;
		for(int j=0;j<=ma;j++) dp[now][j]=1e18;
		for(int x=0;x<=ma;x++){
			if(dp[now^1][x]>=1e9) continue;
			//in left
			chkmin(dp[now][x+(i*d[i])],dp[now^1][x]+((n+1)*(2*d[i]*x+(i*(d[i]*d[i]))))-(2*i*x*d[i]+(i*i*d[i]*d[i])));
//			if(dp[now][x+(i*d[i])]<=100000) cout<<"left:"<<i<<" "<<x+(i*d[i])<<" "<<dp[now][x+(i*d[i])]<<" "<<dp[now^1][x]+((n+1)*(2*d[i]*x+(i*(d[i]*d[i]))))-(2*i*x*d[i]+(i*i*d[i]*d[i]))<<"\n";
			//in right
			chkmin(dp[now][x+s[i]],dp[now^1][x]+((n+1)*(s[i]*s[i]))-(s[i]*s[i]+(2*s[i]*x)));
//			if(dp[now][x+s[i]]<=100000) cout<<"right:"<<i<<" "<<x+s[i]<<" "<<dp[now][x+s[i]]<<" "<<dp[now^1][x]+((n+1)*((x+d[i])*(x+d[i])))-(s[i]*s[i]+(2*s[i]*x))<<"\n";
		}
	}
	int ans=1e18;
	for(int i=0;i<=ma;i++) chkmin(ans,dp[now][i]);
	cout<<ans<<"\n";
	return 0;
}

[NOIP2023] 双序列拓展

只考虑 a1<b1a_1<b_1 的情况,大于同理。

考虑一个 01 矩阵 AA,对于每一对 1≤i≤n,1≤j≤m,Ai,j=[ai≤bj]1 \le i \le n,1 \le j \le m,A_{i,j}=[a_i \le b_j],问题转化为在矩阵上走,每次能向右,下,右下走一步,要求所有走到的格子都满足 Ai,j=1A_{i,j}=1,问是否存在这样的路径。

发现,如果 AA 的最大值是全局最大值,只从起始点、终点走到这一行即可,如果 BB 的最小值是全局最小值,只需从起始点、终点走到这一列即可。

这样相当于缩小了矩形的范围,维护前缀,后缀最小值递归处理即可。

复杂度 O(nq)\mathcal O(nq)。

code
C++
#include<bits/stdc++.h>
using namespace std;

#define int long long
#define PII pair<int,int>
#define fi first
#define se second
#define mk make_pair
#define pb push_back

const int maxn=5e5+10;
int n,m,q,x[maxn],y[maxn];
struct node{
	int min,max; //pos
}prex[maxn],prey[maxn],sufx[maxn],sufy[maxn];

bool check1(int x0,int y0){
	if(x0<1||y0<1) return 1;
	node X=prex[x0],Y=prey[y0];
	if(x[X.max]>y[Y.max]) return check1(X.max-1,y0);
	if(y[Y.min]<x[X.min]) return check1(x0,Y.min-1);
	return 0;
}

bool check2(int x0,int y0){
	if(x0>n||y0>m) return 1;
	node X=sufx[x0],Y=sufy[y0];
	if(x[X.max]>y[Y.max]) return check2(X.max+1,y0);
	if(y[Y.min]<x[X.min]) return check2(x0,Y.min+1);
	return 0;
}

void solve(){
	bool flag=0;
	if(x[1]<y[1]) swap(x,y),swap(n,m),flag=1;
	prex[1].min=prex[1].max=prey[1].min=prey[1].max=1;
	sufx[n].min=sufx[n].max=n;
	sufy[m].min=sufy[m].max=m;
	for(int i=2;i<=n;i++){
		if(x[i]<x[prex[i-1].min]) prex[i].min=i;
		else prex[i].min=prex[i-1].min;
		if(x[i]>x[prex[i-1].max]) prex[i].max=i;
		else prex[i].max=prex[i-1].max;
	}
	for(int i=2;i<=m;i++){
		if(y[i]<y[prey[i-1].min]) prey[i].min=i;
		else prey[i].min=prey[i-1].min;
		if(y[i]>y[prey[i-1].max]) prey[i].max=i;
		else prey[i].max=prey[i-1].max;
	}
	for(int i=n-1;i>=1;i--){
		if(x[i]<x[sufx[i+1].min]) sufx[i].min=i;
		else sufx[i].min=sufx[i+1].min;
		if(x[i]>x[sufx[i+1].max]) sufx[i].max=i;
		else sufx[i].max=sufx[i+1].max;
	}
	for(int i=m-1;i>=1;i--){
		if(y[i]<y[sufy[i+1].min]) sufy[i].min=i;
		else sufy[i].min=sufy[i+1].min;
		if(y[i]>y[sufy[i+1].max]) sufy[i].max=i;
		else sufy[i].max=sufy[i+1].max;
	}
	node X=prex[n],Y=prey[m];
	if(x[X.max]<=y[Y.max]||x[X.min]<=y[Y.min]||x[n]<=y[m]||x[1]<=y[1]){
		cout<<0;
		if(flag) swap(x,y),swap(n,m);
		return;
	}
	cout<<(check1(X.max-1,Y.min-1)&&check2(X.max+1,Y.min+1));
	if(flag) swap(x,y),swap(n,m);
}

signed main(){
	freopen("expand.in","r",stdin);
	freopen("expand.out","w",stdout);
	ios::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	int c;
	cin>>c>>n>>m>>q;
	for(int i=1;i<=n;i++) cin>>x[i];
	for(int j=1;j<=m;j++) cin>>y[j];
	solve();
	vector<PII> modix,modiy;
	for(int i=1;i<=q;i++){
		int kx,ky,p,v;
		cin>>kx>>ky;
		for(int j=1;j<=kx;j++){
			cin>>p>>v;
			modix.pb(mk(p,x[p]));
			x[p]=v;
		}
		for(int j=1;j<=ky;j++){
			cin>>p>>v;
			modiy.pb(mk(p,y[p]));
			y[p]=v;
		}
		solve();
		for(auto u:modix) x[u.fi]=u.se;
		for(auto u:modiy) y[u.fi]=u.se;
		modix.clear();
		modiy.clear();
	}
	return 0;
}
原文