mirror of
https://gitcode.com/Zengtudor/alg2025.git
synced 2025-11-06 06:43:49 +00:00
feat: 添加P14361.cpp解题代码实现
实现了一个解决特定问题的算法,包含深度优先搜索和贪心策略。对于小规模输入使用DFS,大规模输入采用排序和贪心选择策略来优化性能。
This commit is contained in:
parent
d4b17f35cc
commit
e75cbc7b1b
77
src/11/4/P14361.cpp
Normal file
77
src/11/4/P14361.cpp
Normal file
@ -0,0 +1,77 @@
|
||||
#include <algorithm>
|
||||
#include <bitset>
|
||||
#include <cstdint>
|
||||
#include <iostream>
|
||||
#include <istream>
|
||||
#include <vector>
|
||||
using ll = int64_t;
|
||||
#define sl static inline
|
||||
const ll maxn = 1e5+7;
|
||||
ll n,a[maxn][4],ans,club[4];
|
||||
struct S{
|
||||
ll diff,raw,i,j;
|
||||
inline bool operator<(const S&o)const{
|
||||
if(diff==o.diff)return raw>o.raw;
|
||||
return diff>o.diff;
|
||||
}
|
||||
};
|
||||
std::vector<S>s;
|
||||
std::bitset<maxn> vis;
|
||||
|
||||
sl void dfs(ll now,ll nans){
|
||||
if(now==n+1){
|
||||
ans=std::max(ans,nans);
|
||||
return;
|
||||
}
|
||||
for(ll i=1;i<=3;i++){
|
||||
if(club[i]>=n/2)continue;
|
||||
club[i]++;
|
||||
dfs(now+1,nans+a[now][i]);
|
||||
club[i]--;
|
||||
}
|
||||
}
|
||||
|
||||
sl bool smallsolve(){
|
||||
if(n>10)return false;
|
||||
dfs(0,0);
|
||||
std::cout<<ans<<"\n";
|
||||
return true;
|
||||
}
|
||||
|
||||
sl void solve(){
|
||||
std::cin>>n;
|
||||
ans=0;
|
||||
vis.reset();
|
||||
s.clear();
|
||||
club[1]=club[2]=club[3]=0;
|
||||
for(ll i=1;i<=n;i++){
|
||||
std::cin>>a[i][1]>>a[i][2]>>a[i][3];
|
||||
}
|
||||
if(smallsolve())return;
|
||||
for(ll i=1;i<=n;i++){
|
||||
ll tmp[4];
|
||||
for(ll j=1;j<=3;j++)tmp[j]=a[i][j];
|
||||
std::sort(tmp+1,tmp+4);
|
||||
for(ll j=1;j<=3;j++){
|
||||
s.push_back({a[i][j]-tmp[2],a[i][j],i,j});
|
||||
}
|
||||
}
|
||||
std::sort(s.begin(),s.end());
|
||||
for(const auto[diff,raw,i,j]:s){
|
||||
if(vis[i])continue;
|
||||
if(club[j]>=n/2)continue;
|
||||
vis[i]=true;
|
||||
club[j]++;
|
||||
ans+=raw;
|
||||
}
|
||||
std::cout<<ans<<"\n";
|
||||
}
|
||||
|
||||
int main(){
|
||||
std::iostream::sync_with_stdio(false);
|
||||
std::cin.tie(nullptr);
|
||||
|
||||
ll t;
|
||||
std::cin>>t;
|
||||
while(t--)solve();
|
||||
}
|
||||
Loading…
Reference in New Issue
Block a user