跳到正文

个人记录

SESOJ--贪心专题进阶

Bryan1676约 14 分钟
原文

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

文章信息与版本

文章信息

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

版本

  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;
}