题目链接
题意
将2n个人分成两组,使每组n个人,最大化每组对另外一组所有人的竞争力的和。
思路
dfs,在dfs时顺便在O(n)的时间内计算影响。1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
using namespace std;
typedef long long ll;
const int maxn=30;
ll graph[maxn][maxn];
int cnt[maxn][2];
int n,msk;
ll ans;
int w[30];
inline void dfs(int num,int stat,int len,ll res,int p){
if(p){
for(int i=0;i<num-1;i++){
if(stat&(1<<i)) res-=graph[i][num-1];
else res+=graph[i][num-1];
}
for(int i=num;i<2*n;i++){
res+=graph[i][num-1];
}
}
if(len==n){
ans=max(ans,res);
return ;
}
if(num>=2*n) return ;
if(2*n-num<n-len) return ;
dfs(num+1,stat|w[num],len+1,res,1);
dfs(num+1,stat,len,res,0);
}
template<class T>
void read(T& ret)
{
ret = 0;
char c;
while ((c = getchar()) > '9' || c < '0');
while (c >= '0' && c <= '9')
{
ret = ret * 10 + c - '0';
c = getchar();
}
}
template<class T>
void write(T x)
{
if (x == 0)
{
putchar('0');
return;
}
if (x >= 10)
write(x / 10);
putchar('0' + x % 10);
}
int main(){
read(n);
for(int i=0;i<2*n;i++){
for(int j=0;j<2*n;j++)
read(graph[i][j]);
}
for (int i = 0; i <= 2 * n; i++)
w[i] = 1 << i;
ans=0;
dfs(0,0,0,0,0);
write(ans);
putchar('\n');
return 0;
}