跳到正文

个人记录

2025 年 CSP 模拟赛总结

cgy20140502约 9 分钟
原文
文章信息与版本

文章信息

发布
2026年10月11日
收录
2026年10月11日
更新
2026年11月16日
篇幅
约 9 分钟
获赞
1

版本

  1. v1当前

T1:P14361 [CSP-S 2025] 社团招新

初始思路

贪心。

考场解题过程

使用反悔贪心,首先先到最喜欢的社团里,然后如果社团超人数限制了,就把人跳到变化量最小的那个社团里。

代码实现有问题。

考场代码

第一版代码
C++
#include <bits/stdc++.h>
using namespace std;
struct node{
	int id,x;
	bool operator < (node b)const
	{
		return x > b.x;
	}
};
int t,n,a[100005][4],ans,b[4],c[100005];
int main(){
	ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
	cin >> t;
	while (t--){
		ans = 0,b[1] = b[2] = b[3] = 0;
		cin >> n;
		for (int i = 1;i <= n;i++) cin >> a[i][1] >> a[i][2] >> a[i][3];
		for (int i = 1;i <= n;i++){
			int maxx = a[i][1],pos = 1;
			if (a[i][2] > maxx) maxx = a[i][2],pos = 2;
			if (a[i][3] > maxx) maxx = a[i][3],pos = 3;
			c[i] = pos,b[pos]++,ans += maxx;
		}
		if (b[1] <= n/2 && b[2] <= n/2 && b[3] <= n/2){
			cout << ans << "\n";
			continue;
		}
		if (b[1] > n/2){
			priority_queue <node> q;
			for (int i = 1;i <= n;i++)
				if (c[i] == 1)
					q.push(node{i,a[i][1]-max(a[i][2],a[i][3])});
			while (b[1] > n/2){
				b[1]--;
				int i = q.top().id;
				ans -= q.top().x;
				if (a[i][1]-a[i][2] > a[i][1]-a[i][3]) b[3]++;
				else b[2]++;
				q.pop();
			}
		}
		if (b[2] > n/2){
			priority_queue <node> q;
			for (int i = 1;i <= n;i++)
				if (c[i] == 2)
				 	q.push(node{i,a[i][2]-max(a[i][1],a[i][3])});
			while (b[2] > n/2){
				b[2]--;
				int i = q.top().id;
				ans -= q.top().x;
				if (a[i][2]-a[i][1] > a[i][2]-a[i][3]) b[3]++;
				else b[1]++;
				q.pop();
			}
		}
		cout << ans << "\n";
	}
	return 0;
}

赛后题解

使用反悔贪心,首先每个人先到最喜欢的社团里,然后如果社团超人数限制了,就把人跳到他第二喜欢的那个社团里。

补题代码

code
C++
#include <bits/stdc++.h>
using namespace std;
int t,n,a[100005][4],ans,b[4],c[100005];
int main(){
	ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
	cin >> t;
	while (t--){
		ans = 0,b[1] = b[2] = b[3] = 0;
		cin >> n;
		for (int i = 1;i <= n;i++) cin >> a[i][1] >> a[i][2] >> a[i][3];
		for (int i = 1;i <= n;i++){
			int maxx = a[i][1],pos = 1;
			if (a[i][2] > maxx) maxx = a[i][2],pos = 2;
			if (a[i][3] > maxx) maxx = a[i][3],pos = 3;
			c[i] = pos,b[pos]++,ans += maxx;
		}
		if (b[1] <= n/2 && b[2] <= n/2 && b[3] <= n/2){
			cout << ans << "\n";
			continue;
		}
		int tiao = -1;
		if (b[1] > n/2) tiao = 1;
		else if (b[2] > n/2) tiao = 2;
		else if (b[3] > n/2) tiao = 3;
		priority_queue <int,vector<int>,greater<int> > q;
		for (int i = 1;i <= n;i++)
			if (c[i] == tiao){
				if (c[i] == 1) q.push(a[i][1]-max(a[i][2],a[i][3]));
				else if (c[i] == 2) q.push(a[i][2]-max(a[i][1],a[i][3]));
				else q.push(a[i][3]-max(a[i][1],a[i][2]));
			}
		while (b[tiao] > n/2){
			ans -= q.top();
			q.pop();
			b[tiao]--;
		}
		cout << ans << "\n";
	}
	return 0;
}

复杂度

时间复杂度:单次操作 O(nlog⁡n)O(n \log n)

空间复杂度:O(n)O(n)

T2:P14362 [CSP-S 2025] 道路修复

初始思路

建图,然后暴力去连边+最小生成树

赛后题解

先建一张普通图,再用最小生成树把废边排除掉,然后把改造乡镇的边连上,用状态压缩枚举改不改造,最后判断有没有改造来确定这条改造之后的边能不能用。

补题代码

code
C++
#include <bits/stdc++.h>
#define int long long
using namespace std;
struct node{
	int u,v,w;
}e[1100005],g[2000005];
int n,m,k,fa[1000005],cnt,c[1000005],ans = LLONG_MAX;
bool cmp(node x,node y){
	return x.w < y.w;
}
int find(int x){
	if (fa[x] == x) return x;
	return fa[x] = find(fa[x]);
}
void Union(int x,int y){
	int fx = find(x),fy = find(y);
	if (fx == fy) return;
	fa[fx] = fy;
}
signed main(){
//	freopen("road.in", "r", stdin);
//	freopen("road.out","w",stdout);
	ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
	cin >> n >> m >> k;
	int u,v,w;
	for (int i = 1;i <= m;i++){
		cin >> u >> v >> w;
		e[i] = node{u,v,w};
	}
	sort(e+1,e+m+1,cmp);
	for (int i = 1;i <= n;i++) fa[i] = i;
	for (int i = 1;i <= m;i++){
		int u = e[i].u,v = e[i].v,w = e[i].w;
		if (find(u) == find(v)) continue;
		Union(u,v);
		g[++cnt] = node{u,v,w};
	}
	for (int i = 1;i <= k;i++){
		cin >> c[i];
		for (int j = 1;j <= n;j++){
			cin >> w;
			g[++cnt] = node{n+i,j,w};
		}
	}
	sort(g+1,g+cnt+1,cmp);
	for (int bit = 0;bit < (1<<k);bit++){
		for (int i = 1;i <= n+k;i++) fa[i] = i;
		int sum = 0;
		for (int i = 1;i <= k;i++)
			if (bit & (1 << i-1)) sum += c[i];
		for (int i = 1;i <= cnt;i++){
			int u = g[i].u,v = g[i].v,w = g[i].w;
			if (u > n && !(bit & (1 << u-n-1))) continue;
			if (v > n && !(bit & (1 << v-n-1))) continue;
			if (find(u) == find(v)) continue;
			Union(u,v);
			sum += w;
		}
		ans = min(ans,sum);
	}
	cout << ans;
	return 0;
}

复杂度

时间复杂度:O(mlog⁡m+knlog⁡kn+2kkn)O(m \log m + kn \log kn+2^k kn)

原文