跳到正文

题解

题解:P14361 [CSP-S 2025] 社团招新 / club(民间数据)

Zjl120919约 8 分钟
原文
文章信息与版本

文章信息

发布
2025年11月1日
收录
2026年6月4日
更新
2026年11月16日
篇幅
约 8 分钟
获赞
1
题目
P14361

版本

  1. v1当前

这道题在考场上卡了我整整四个小时!!!
于是决定发篇题解纪念下。

思路

  1. 先将每个人对分配到的部门的满意度同时减去最小的一个满意度,同时计数变量 sum 加上最小的一个满意度。这样可以将其中的一个满意度变为 00 。
    部分代码
C++
int k = min(a, min(b, c));
a -= k, b -= k, c -= k;
sum += k;
  1. 然后我们定义三个 vector 。
    作用:存储余下每人两个满意度中较大那个属于 a , b 还是 c 。
    部分代码
代码
if (a >= b && a >= c) {
  node u;
  u.a = a, u.b = b, u.c = c;
  ma.push_back(u);
  aa++;
} else if (b >= a && b >= c) {
  node u;
  u.a = a, u.b = b, u.c = c;
  mb.push_back(u);
  bb++;
} else if (c >= b && c >= a) {
  node u;
  u.a = a, u.b = b, u.c = c;
  mc.push_back(u);
  cc++;
}

注: aa , bb , cc 是 vector 的大小。

  1. 接下来对 33 个 vector 进行排序。
    值得注意的是排序的规则, 剩余的两个满意度的差值越大越应该排前面。
    部分代码
C++
bool cmpa(node xx, node yy) {
	int xxx, yyy;
	if (xx.b != 0) {
		xxx = xx.b;
	} else {
		xxx = xx.c;
	}
	if (yy.b != 0) {
		yyy = yy.b;
	} else {
		yyy = yy.c;
	}
	return xx.a - xxx > yy.a - yyy;
}
代码
sort(ma.begin(), ma.end(), cmpa);

其余两个 vector 同理。

  1. 接下来就可以遍历 vector 了。
    这里要注意的是第一步先遍历到 n2\frac{n}{2} 即可,用 sum 加上最大值。
    部分代码
C++
for (int i = 0; i < min(aa, n / 2); i++) {
  sum += ma[i].a;
  ma[i].a = 0, ma[i].b = 0, ma[i].c = 0;
}
for (int i = 0; i < min(bb, n / 2); i++) {
  sum += mb[i].b;
  mb[i].a = 0, mb[i].b = 0, mb[i].c = 0;
}
for (int i = 0; i < min(cc, n / 2); i++) {
  sum += mc[i].c;
  mc[i].a = 0, mc[i].b = 0, mc[i].c = 0;
}

注:这里累加和后清零是为了方便下一步的遍历。

  1. 遍历超过 n2\frac{n}{2} 的部分。
    以 a 部门为例:
    因为 a 部门的人超过了 n2\frac{n}{2} ,所以剩余的人只能去 b 或者 c 部门(用sum加上剩余两个的最大值就行),就和你选修课没选上一个道理。
    并且要满意度更高,即,
    max(ma[i].b, ma[i].c);
    同理, b c 部门也是一样的。
    部分代码
C++
for (int i = 0; i < aa; i++) {
  sum += max(ma[i].b, ma[i].c);
}
for (int i = 0; i < bb; i++) {
  sum += max(mb[i].a, mb[i].c);
}
for (int i = 0; i < cc; i++) {
  sum += max(mc[i].b, mc[i].a);
}

之所以这里可以直接遍历完,是因为前面已经把遍历过的清零了,所以满意度不会变多。
6. OK ,最后输出 sum 就行啦。

代码

C++
#include<bits/stdc++.h>
using namespace std;
struct node {
	int a, b, c;
};

vector<node> ma, mb, mc;
bool cmpa(node xx, node yy) {
	int xxx, yyy;
	if (xx.b != 0) {
		xxx = xx.b;
	} else {
		xxx = xx.c;
	}
	if (yy.b != 0) {
		yyy = yy.b;
	} else {
		yyy = yy.c;
	}
	return xx.a - xxx > yy.a - yyy;
}
bool cmpb(node xx, node yy) {
	int xxx, yyy;
	if (xx.a != 0) {
		xxx = xx.a;
	} else {
		xxx = xx.c;
	}
	if (yy.a != 0) {
		yyy = yy.a;
	} else {
		yyy = yy.c;
	}
	return xx.b - xxx > yy.b - yyy;
}
bool cmpc(node xx, node yy) {
	int xxx, yyy;
	if (xx.b != 0) {
		xxx = xx.b;
	} else {
		xxx = xx.a;
	}
	if (yy.b != 0) {
		yyy = yy.b;
	} else {
		yyy = yy.a;
	}
	return xx.c - xxx > yy.c - yyy;
}

int main() {
	//freopen("club.in","r",stdin);
	//freopen("club.out","w",stdout);
	int t;
	cin >> t;
	while (t--) {
		ma.clear();
		mb.clear();
		mc.clear();
		int sum = 0;
		int n;
		int aa = 0, bb = 0, cc = 0;
		cin >> n;
		for (int i = 1; i <= n; i++) {
			int a, b, c;
			cin >> a >> b >> c;
			int k = min(a, min(b, c));
			a -= k, b -= k, c -= k;
			sum += k;
			if (a >= b && a >= c) {
				node u;
				u.a = a, u.b = b, u.c = c;
				ma.push_back(u);
				aa++;
			} else if (b >= a && b >= c) {
				node u;
				u.a = a, u.b = b, u.c = c;
				mb.push_back(u);
				bb++;
			} else if (c >= b && c >= a) {
				node u;
				u.a = a, u.b = b, u.c = c;
				mc.push_back(u);
				cc++;
			}
		}
		sort(ma.begin(), ma.end(), cmpa);
		sort(mb.begin(), mb.end(), cmpb);
		sort(mc.begin(), mc.end(), cmpc);
		for (int i = 0; i < min(aa, n / 2); i++) {
			sum += ma[i].a;
			ma[i].a = 0, ma[i].b = 0, ma[i].c = 0;
		}
		for (int i = 0; i < min(bb, n / 2); i++) {
			sum += mb[i].b;
			mb[i].a = 0, mb[i].b = 0, mb[i].c = 0;
		}
		for (int i = 0; i < min(cc, n / 2); i++) {
			sum += mc[i].c;
			mc[i].a = 0, mc[i].b = 0, mc[i].c = 0;
		}

		for (int i = 0; i < aa; i++) {
			sum += max(ma[i].b, ma[i].c);
		}
		for (int i = 0; i < bb; i++) {
			sum += max(mb[i].a, mb[i].c);
		}
		for (int i = 0; i < cc; i++) {
			sum += max(mc[i].b, mc[i].a);
		}
		cout << sum << "\n";
	}
	return 0;
}

等等,别忘了在每次循环时初始化 vector。
AC记录
祝大家考一个好成绩!

完结撒花

原文