题解
2026-03-31 11:29:28
发布于:浙江
1阅读
0回复
0点赞
由于最多只有一个部门人数会超出n/2,所以先选择每人满意度最高的,把损失排序后依次减去
#include<bits/stdc++.h>
using namespace std;
const int N=1e5+7;
int main(){
cout.tie(0);
ios::sync_with_stdio(0);
cin.tie(0);
int t;
cin>>t;
while(t--){
int n;
int a[N],b[N],c[N];
int fa[N],fb[N],fc[N];
int x=0,y=0,z=0;
long long ans=0;
cin>>n;
for(int i=1;i<=n;i++){
cin>>a[i]>>b[i]>>c[i];
if(a[i]>=b[i]&&a[i]>=c[i]){
ans+=a[i];
x++;
fa[x]=a[i]-max(b[i],c[i]);
}
else if(b[i]>=c[i]&&b[i]>=a[i]){
ans+=b[i];
y++;
fb[y]=b[i]-max(a[i],c[i]);
}
else{
ans+=c[i];
z++;
fc[z]=c[i]-max(a[i],b[i]);
}
}
sort(fa+1,fa+1+x);
sort(fb+1,fb+1+y);
sort(fc+1,fc+1+z);
if(x>n/2){
for(int i=1;i<=x-n/2;i++) ans-=fa[i];
}
else if(y>n/2){
for(int i=1;i<=y-n/2;i++) ans-=fb[i];
}
else if(z>n/2) {
for(int i=1;i<=z-n/2;i++) ans-=fc[i];
}
cout<<ans<<'\n';
}
return 0;
}
这里空空如也




有帮助,赞一个