跳到正文

题解

题解:P14361 [CSP-S 2025] 社团招新 / club

chenzexin2012约 2 分钟
原文
文章信息与版本

文章信息

收录
2025年12月12日
更新
2025年12月12日
篇幅
约 2 分钟
题目
P14361

版本

  1. v2当前
  2. v1

题解:P14361 [CSP-S 2025] 社团招新 / club

Part1——思路

优先选择自己最满意的社团,若社团人员过多,则分配去别的 22 家社团,其中优先选择代价较小的社团(即满意度第二大的社团)。

疑惑:有没有可能别的社团超员

答案:不可能。 若一个社团中有人需要被调走,则其他两家社团员工之和一定不超过 n2\frac{n}{2} ,因为当前社团人数一定大于 n2\frac{n}{2} 。

Part2——代码分析

首先要分别定义三个变量(统计社团人数)与三个优先队列(priority−queuepriority-queue)(存储从当前社团调走人的代价的最小值),后续使用三个循环判断公司是否超员

Part3——代码实现

C++
#include<bits/stdc++.h>
#define int long long
using namespace std;
priority_queue<int> xx,yy,zz;
//大的在堆顶
int T,n;
signed main(){
	cin>>T;
	while(T--){
		int cntx=0,cnty=0,ans=0,cntz=0;
		scanf("%lld",&n);
		for(int i=1;i<=n;i++){
			int x,y,z;
			scanf("%lld%lld%lld",&x,&y,&z);
			int cnt=max(max(x,y),z);
			if(cnt==x) cntx++,ans+=x,xx.push(max(z-x,y-x));
			else if(cnt==y) cnty++,ans+=y,yy.push(max(x-y,z-y));
			else cntz++,ans+=z,zz.push(max(x-z,y-z));
		}
		while(cntx>n/2){
			ans+=xx.top();
			xx.pop();
			cntx--;
		}
		while(cnty>n/2){
			ans+=yy.top();
			yy.pop();
			cnty--;
		}
		while(cntz>n/2){
			ans+=zz.top();
			zz.pop();
			cntz--;
		}
		printf("%lld\n",ans);
		while(!xx.empty()) xx.pop();
		while(!yy.empty()) yy.pop();
		while(!zz.empty()) zz.pop();
	}
	return 0;
}
原文