0
点赞
收藏
分享

微信扫一扫

2019牛客暑期多校训练营(第二场) F Partition problem


链接:​​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 ∑i=12N∑j=i+12N(vij if i-th person is not in the same team as j-th person else 0)\sum_{i=1}^{2N} \sum_{j=i+1}^{2N} (v_{ij} \text{ 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 vijv_{ij}vij is the j-th value of the i-th line which indicates the competitive value of person i and person j. * 1≤N≤141 \leq N \leq 141≤N≤14 * 0≤vij≤1090 \leq v_{ij} \leq 10^{9}0≤vij≤109 * vij=vjiv_{ij} = v_{ji}vij=vji


输出描述:


Output one line containing an integer representing the maximum possible total competitive value.


示例1

输入

复制


1 0 3 3 0


输出

复制


3


题意:

给你2*n个人,分成两组,给你一个(2n)*(2n)的矩阵,表示i对j的影响值,求使得影响值最大的

分析:

因为a[i][j]=a[j][i],所以两队的影响值是固定的,所以我们可以固定第一个人在某一队,然后dfs枚举和每一个n-1个人在一队的影响和。

看一下代码秒懂

#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
#define N 100
int n,m;
LL a[N][N];
LL sum=0;
LL ans=0,res;
///sta表示当前状态,第i为为1,则表示被选中
///num表示选的人数
///pre 表示选的前一个人
///res表示当前的影响和
void dfs(LL sta,int num,int pre,LL res)
{
if(num>=n)
{
ans=max(res,ans);
return ;
}
///应该在选的人数<还没选的人数
if(pre-num>n)
{
return ;
}
for(int i=pre+1;i<=2*n;i++)
{
LL temp=res;
for(int j=1;j<=2*n;j++)
{
if((sta>>j)&1)
{
temp-=a[i][j];
}
else
{
temp+=a[i][j];
}

}
dfs(sta|(1<<i),num+1,i,temp);
}
}
int main()
{
scanf("%d",&n);
for(int i=1; i<=2*n; i++)
{
for(int j=1; j<=2*n; j++)
scanf("%lld",&a[i][j]);
}
for(int i=1;i<=2*n;i++)
{
sum+=a[1][i];
}

dfs(2,1,1,sum);
printf("%lld\n",ans);
return 0;
}

 

举报

相关推荐

0 条评论