首页 技术 正文
技术 2022年11月9日
0 收藏 361 点赞 4,249 浏览 906 个字

Partition problem

题目传送门

解题思路

假设当前两队的对抗值为s,如果把红队中的一个人a分配到白队,s+= a对红队中所有人的对抗值,s-= a对白队中所有人的对抗值。所以我们可以先假设所有人都在红队中,把人一个一个分配到白队中,枚举所有的情况求最大值。

然后继续剪枝:

1.我们可以让1是固定在白队。

2.在搜索的过程中让序号递增,以此来避免重复枚举。

3.保证剩下的的人数足够选择,不够的必然不行。

代码如下

#include <bits/stdc++.h>
#define INF 0x3f3f3f3f
using namespace std;
typedef long long ll;inline int read(){
int res = 0, w = 0; char ch = 0;
while(!isdigit(ch)){
w |= ch == '-', ch = getchar();
}
while(isdigit(ch)){
res = (res << 3) + (res << 1) + (ch ^ 48);
ch = getchar();
}
return w ? -res : res;
}const int N = 30;ll a[N][N];
bool in[N];
ll ans;
int n;void dfs(int x, int c, ll sum)
{
if(c == n){ //已经选择了n个人到白队
ans = max(ans, sum);
return;
}
for(int i = x + 1; i <= n + c + 1; i ++){ //保证剩下的够选
ll temp = sum;
for(int j = 1; j <= 2 * n; j ++){
if(in[j])
sum -= a[i][j];
else
sum += a[i][j];
}
in[i] = true;
dfs(i, c + 1, sum);
in[i] = false; //回溯
sum = temp;
}
}int main()
{
cin >> n;
for(int i = 1; i <= 2 * n; i ++){
for(int j = 1; j <= 2 * n; j ++){
a[i][j] = read();
}
}
ll sum = 0;
in[1] = true; //1固定在白队
for(int i = 2; i <= 2 * n; i ++)
sum += a[1][i];
dfs(1, 1, sum);
cout << ans << endl;
return 0;
}
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,082
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,556
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,405
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,179
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,815
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,898