本题贪心+反悔
蒟蒻第1次写题解~~~~
基本思路是将每个成员加入社团所能造成的贡献从大到小排序。
将贡献最大的那一个社团作为第一志愿,次大作为第二志愿。
统计每各社团作为第一志愿有几个学生选择?若其中一个大于n/2,再把每个加入这个社团的学生按最大贡献与次大贡献之差排序。
为什么要这么排序呢?
因为这样排序可以删去最后那些差值最小的最大值。
不多说,见代码。
#include<bits/stdc++.h>
#define ll long long
#define PII pair<ll,ll>
#define mp make_pair
//ccf让用make_pair啊。
using namespace std;
const ll N=1e6;
ll read(){
ll ret=0,f=1;
char ch=getchar();
if(ch=='-'){
f=-1;
ch=getchar();
}
while(ch>='0'&&ch<='9')ret=ret*10+ch-48,ch=getchar();
return ret*f;
}//快读
struct D{
priority_queue<PII,vector<PII> >q;//大根堆优先队列。
}a[N];
ll n,x1,x2,x3;//不开~~浪浪~~见祖宗。
bool cmp1(D w1,D w2){
if(w1.q.top().second==w2.q.top().second){
PII s1=w1.q.top();w1.q.pop();PII e1=w1.q.top();w1.q.push(s1);
PII s2=w2.q.top();w2.q.pop();PII e2=w1.q.top();w2.q.push(s2);
//将最大值与次大值取出,为s1,e1和s2,e2,下面同理
return s1.first-e1.first<s2.first-e2.first;
//将差值进行比较,排序
}
return w1.q.top().second<w2.q.top().second;
//将社团一放到最前面。
}
bool cmp2(D w1,D w2){
if(w1.q.top().second==w2.q.top().second){
PII s1=w1.q.top();w1.q.pop();PII e1=w1.q.top();w1.q.push(s1);
PII s2=w2.q.top();w2.q.pop();PII e2=w1.q.top();w2.q.push(s2);
return s1.first-e1.first<s2.first-e2.first;
}
if(w2.q.top().second==2)return 1;
return 0;
//将社团二放到最前面。
}
bool cmp3(D w1,D w2){
if(w1.q.top().second==w2.q.top().second){
PII s1=w1.q.top();w1.q.pop();PII e1=w1.q.top();w1.q.push(s1);
PII s2=w2.q.top();w2.q.pop();PII e2=w1.q.top();w2.q.push(s2);
return s1.first-e1.first<s2.first-e2.first;
}
//将社团三放到最前面。
return w1.q.top().second>w2.q.top().second;
}
void solve(){
n=read();
x1=x2=x3=0;
//初始化
for(ll i=1;i<=n;i++){
while(a[i].q.size())a[i].q.pop();
a[i].q.push(mp(read(),1));
a[i].q.push(mp(read(),2));
a[i].q.push(mp(read(),3));
}//输入,添加到优先队列大根堆,自动排序
for(ll i=1;i<=n;i++){
if(a[i].q.top().second==1)x1++;
else if(a[i].q.top().second==2)x2++;
else x3++;
}//统计每个社团有多少人加入
if(2*x1>n){
sort(a+1,a+n+1,cmp1);
ll i=n/2+1;
while(a[i].q.size()&&a[i].q.top().second==1){
a[i].q.pop();
i++;
//将多余的第一志愿都降到第二志愿。
}
ll sum=0;
for(ll i=1;i<=n;i++)sum+=a[i].q.top().first;
cout<<sum<<"\n";
return;
}else if(2*x2>n){
sort(a+1,a+n+1,cmp2);
ll i=n/2+1;
while(a[i].q.size()&&a[i].q.top().second==2){
a[i].q.pop();
i++;
}
ll sum=0;
for(ll i=1;i<=n;i++)sum+=a[i].q.top().first;
cout<<sum<<"\n";
return;
}else if(2*x3>n){
sort(a+1,a+n+1,cmp3);
ll i=n/2+1;
while(a[i].q.size()&&a[i].q.top().second==3){
a[i].q.pop();
i++;
}
ll sum=0;
for(ll i=1;i<=n;i++)sum+=a[i].q.top().first;
cout<<sum<<"\n";
return;
}else{
ll sum=0;
for(ll i=1;i<=n;i++)sum+=a[i].q.top().first;
cout<<sum<<"\n";
return;
//如果没有社团多余人,那么直接输出每个人的最大贡献。
}
}
int main(){
ios::sync_with_stdio(false),cin.tie(0),cout.tie(0);
//freopen("club.in","r",stdin);
//freopen("club.out","w",stdout);
ll _=read();
while(_--){
solve();
//多测。
}
return 0;
}