- 题目题解
P14361 [CSP-S 2025] 社团招新 题解
- @ 2026-8-25 22:21:41
引言
反悔贪心,我的做法可能有点奇异搞笑,权当看个乐(os:就是因为太奇异搞笑才放出来)
题意
给定n个人的三个宗门倾向度,每个宗门最多接受n/2个人,求最大倾向度总和
思考
-
注意到
n/2这条分界线,若没有宗门超过n/2,直接贪心让每个人去最喜欢的宗门即可,超过n/2了,考虑要不要替换 -
假设宗门 A 人数已满,当前处理第 人, 对各宗门倾向度分别为 ,其中
-
那么考虑从原有分配到宗门 A 中的人抽取一个替换到其它宗门腾位置给 是否更优
-
假设替换掉 ,它对 A 的倾向度为 ,因为 B,C 宗门一定不满( A 已达到
n/2),故使 去第二倾向的宗门一定更优,记第二倾向度为 -
如果替换,对 ,使总和 减少 ,对 ,使总和增加
-
如果不替换, 记 的第二倾向度为 ,同上可证明它去第二志愿是更优的,使总和增加
-
由此可推出
替换总贡献
不替换总贡献
-
综上,满足 时替换
-
用小根堆维护 即可
Code
#include <bits/stdc++.h>
#define endl '\n'
using ll =long long;
using namespace std;
const int N=1e5+9;
struct node{
int a,b,c;
}no[N];
int n;
void solved(){
cin>>n;
int cnta=0,cntb=0,cntc=0;//定义为最大意愿出现次数
priority_queue<int,vector<int>,greater<int> > pq;//小根堆
for(int i=1;i<=n;i++){
int a,b,c;cin>>a>>b>>c;
no[i].a=a,no[i].b=b,no[i].c=c;
int mx=max({a,b,c});
if(mx==a)cnta++;//使最大意愿次数增加
else if(mx==b)cntb++;
else cntc++;
}
int mx=max({cnta,cntb,cntc});
if(mx==cnta)mx=0;
else mx= cntb>=cntc?1:2;//定义mx为可能超过n/2的社团,0对应a社团
ll ans=0;//1对应b,2对应c
cnta=cntb=cntc=0;//重新设置以便后续反悔贪心
for(int i=1;i<=n;i++){
int a=no[i].a,b=no[i].b,c=no[i].c;
int t=max({a,b,c});//当前最大
if(t==a){
int f=max(b,c);
if(mx==0){//如果当前社团为会超过的
if(cnta==n/2){//如果已经满了
if(a>f+pq.top()){//判断是否满足替换要求,即替换更优
ans+=(a-pq.top());pq.pop();
pq.push(a-f);
}else ans+=f;
}else cnta++,pq.push(a-f),ans+=a;
}
else ans+=a;
}else if(t==b){//copy and paste
int f=max(a,c);
if(mx==1){
if(cntb==n/2){
if(b>f+pq.top()){
ans+=(b-pq.top());pq.pop();
pq.push(b-f);
}else ans+=f;
}else cntb++,pq.push(b-f),ans+=b;
}
else ans+=b;
}else{
int f=max(a,b);
if(mx==2){
if(cntc==n/2){
if(c>f+pq.top()){
ans+=(c-pq.top());pq.pop();
pq.push(c-f);
}else ans+=f;
}else cntc++,pq.push(c-f),ans+=c;
}
else ans+=c;
}
}
cout<<ans<<endl;
}
int main(){
ios::sync_with_stdio(false),cin.tie(0),cout.tie(0);
int t;cin>>t;while(t--)solved();
return 0;
}
不想成为小场面先生,大赛软脚虾
1 条评论
-
HuiKuai LV 8 @ 2026-8-26 2:12:38四层(@v@ )
- 1