链接:https://ac.nowcoder.com/acm/contest/882/F
来源:牛客网
时间限制:C/C++ 4秒,其他语言8秒
空间限制:C/C++ 262144K,其他语言524288K
64bit IO Format: %lld
题目描述
Given 2N people, you need to assign each of them into either red team or white team such that each team consists of exactly N people and the total competitive value is maximized.
Total competitive value is the summation of competitive value of each pair of people in different team.
The equivalent equation is ∑2Ni=1∑2Nj=i+1(vij if i-th person is not in the same team as j-th person else 0)∑i=12N∑j=i+12N(vij if i-th person is not in the same team as j-th person else 0)
输入描述:
The first line of input contains an integers N. Following 2N lines each contains 2N space-separated integers vijvij is the j-th value of the i-th line which indicates the competitive value of person i and person j. * 1≤N≤141≤N≤14 * 0≤vij≤1090≤vij≤109 * vij=vjivij=vji
输出描述:
Output one line containing an integer representing the maximum possible total competitive value.
示例1
输入
复制
1 0 3 3 0
输出
复制
3
题意:将2*n只队伍分成两只,其中不同队伍之间的成员可以两两对战然后把权值加起来,求怎么分才能使权值和最大
题解:普通做法是 C(2n,n)*n*n;但这样是不符合题目时间复杂度的,所以我们可以先计算出一个人的,然后O(n)的添加人,每添加一个人的时候我们就把那个人的贡献加上,同时减去之前的人和他对战的贡献,这样就能做到On处理了;
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
ll v[50][50];
ll ans;
int n;
void dfs(int mask,int cnt,int pre,ll cst){
if(cnt*2==n){
ans=max(ans,cst);return;
}
if(n-pre-1+cnt<n/2)return;
for(int i=pre+1;i<n;i++){
if(!(mask&(1<<i))){
ll _cst=cst;
for(int j=0;j<n;j++){
if(!(mask&(1<<j))){
_cst+=v[i][j];
}
else{
_cst-=v[i][j];
}
}
dfs(mask|(1<<i),cnt+1,i,_cst);
}
}
}
int main(){
cin >>n;
n<<=1;
for(int i=0;i<n;i++){
for(int j=0;j<n;j++){
scanf("%lld",&v[i][j]);
}
}
ll cst=0;
for(int i=0;i<n;i++){
cst+=v[0][i];
}
dfs(1<<0,1,0,cst);
printf("%lld\n",ans);
return 0;
}