题目链接:传送门

题面:

【题意】

  给定2×n个人的相互竞争值,请把他们分到两个队伍里,如果是队友,那么竞争值为0,否则就为v[i][j]。

【题解】

  爆搜,C(28,14)*28,其实可以稍加优化,因为分到两个队伍,所以第一个人肯定会分到一个队伍中,搜索可以有,C(27.13)*28,其实可以稍加剪枝,其实这个剪枝有点模糊,就是统计第i个人能产生的最大竞争值,当在过程中这个最大竞争值+当前值 < 暂存答案时,即为剪枝,【最优化剪枝】。

【代码】

 #include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = ;
int a[N],b[N];
int A,B,n;
ll v[N][N],ans;
void dfs(int pos,ll sum){
if ( A > n || B > n ){
return ;
}
if ( A == n && B == n ){
ans = max(ans,sum);
return ;
}
if( A < n ){
a[++A] = pos ;
for(int i=;i<=B;i++){
sum += v[pos][b[i]] ;
}
dfs( pos+ , sum );
for(int i=;i<=B;i++){
sum -= v[pos][b[i]];
}
A--;
}
if( B < n ){
b[++B] = pos ;
for(int i=;i<=A;i++){
sum += v[pos][a[i]] ;
}
dfs( pos+ , sum );
for(int i=;i<=A;i++){
sum -= v[pos][a[i]];
}
B--;
}
}
int main()
{
scanf("%d",&n);
for(int i=;i<=*n;i++){
for(int j=;j<=*n;j++){
scanf("%lld",&v[i][j]);
}
}
dfs(,);
printf("%lld\n",ans);
return ;
}

爆搜写法

 #include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = ;
int a[N],b[N],A,B,n;
ll v[N][N],p[N],Maxn[N],sum[N];
ll res , ans ;
void dfs(int pos ){
if ( A == n && B == n ){
res = max( res , ans );
return ;
}
if( ans + sum[pos] < res ) //当前答案加上后缀 < 已存答案
return ;
if( A < n ){
ll tmp = ans ;
a[++A] = pos ;
for(int i=;i<=B;i++)
ans += v[pos][b[i]];
dfs( pos+ ) ;
A--;
ans = tmp ;
}
if( B < n ){
ll tmp = ans ;
b[++B] = pos ;
for(int i=;i<=A;i++)
ans += v[pos][a[i]];
dfs( pos+);
B--;
ans = tmp ;
}
}
int main()
{
scanf("%d",&n);
for(int i=;i<=*n;i++){
for(int j=;j<=*n;j++){
scanf("%lld",&v[i][j]);
}
}
for(int i=;i<=*n;i++){
for(int j=;j<=*n;j++){
p[j] = v[i][j];
}
sort( p+,p++*n);
for(int j=n+;j<=*n;j++){
Maxn[i] += p[j] ;
}
}
//预处理sum后缀和,可以在过程中加入进行剪枝
for(int i=*n;i>=;i--){
sum[i] = sum[i+] + Maxn[i] ;
}
A = ;
a[] = ;
dfs();
printf("%lld\n",res);
return ;
}

稍加剪枝写法

【搜索】Partition problem的更多相关文章

  1. The Painter's Partition Problem Part II

    (http://leetcode.com/2011/04/the-painters-partition-problem-part-ii.html) This is Part II of the art ...

  2. 2019牛客多校第二场F Partition problem 暴力+复杂度计算+优化

    Partition problem 暴力+复杂度计算+优化 题意 2n个人分成两组.给出一个矩阵,如果ab两个在同一个阵营,那么就可以得到值\(v_{ab}\)求如何分可以取得最大值 (n<14 ...

  3. The Painter's Partition Problem Part I

    (http://leetcode.com/2011/04/the-painters-partition-problem.html) You have to paint N boards of leng ...

  4. 2019牛客暑期多校训练营(第二场) - F - Partition problem - 枚举

    https://ac.nowcoder.com/acm/contest/882/F 潘哥的代码才卡过去了,自己写的都卡不过去,估计跟评测机有关. #include<bits/stdc++.h&g ...

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

    链接:https://ac.nowcoder.com/acm/contest/882/F来源:牛客网 Given 2N people, you need to assign each of them ...

  6. 2019年牛客多校第二场 F题Partition problem 爆搜

    题目链接 传送门 题意 总共有\(2n\)个人,任意两个人之间会有一个竞争值\(w_{ij}\),现在要你将其平分成两堆,使得\(\sum\limits_{i=1,i\in\mathbb{A}}^{n ...

  7. 2019牛客多校2 F Partition problem(dfs)

    题意: n<=28个人,分成人数相同的两组,给你2*n*2*n的矩阵,如果(i,j)在不同的组里,竞争力增加v[i][j],问你怎么分配竞争力最 4s 思路: 枚举C(28,14)的状态,更新答 ...

  8. 2019牛客多校第二场F Partition problem(暴搜)题解

    题意:把2n个人分成相同两组,分完之后的价值是val(i, j),其中i属于组1, j属于组2,已知val表,n <= 14 思路:直接dfs暴力分组,新加的价值为当前新加的人与不同组所有人的价 ...

  9. 2019牛客多校第二场F-Partition problem(搜索+剪枝)

    Partition problem 题目传送门 解题思路 假设当前两队的对抗值为s,如果把红队中的一个人a分配到白队,s+= a对红队中所有人的对抗值,s-= a对白队中所有人的对抗值.所以我们可以先 ...

随机推荐

  1. jenkins的任务卡住

    今天做jenkins任务的时候,发现一个启动后,一直卡住,在那转圈圈,其实这个时候,任务已经执行完了. 经过分析,因为这个任务是启动一个web服务,直接在机器上执行时,直接占用一个终端. 解决办法,放 ...

  2. 互联网IT当线上出现 bug 时,是怎么处理的?

    线上BUG说处理方法:1.关于线上BUG问题,目前公司有一整套线上故障流程规范,包括故障定义.定级.处理流程.故障处理超时升级机制.故障处理小组.故障处罚(与故障存在时长有关)等:2.最主要的是,线上 ...

  3. SCRIPT438: 对象不支持“trim”属性或方法

    关于ie9以下不支持trim()方法 可以在自己封装的框架中加入如下.或直接调用也行. if(!String.prototype.trim) { String.prototype.trim = fun ...

  4. python 库 imgaug数据增强

    安装及详细使用方法介绍: https://blog.csdn.net/qq_38451119/article/details/82428612 pip install imgaug 失败解决方法: 提 ...

  5. BOM相关方法及属性

    browser objec tmodel浏览器对象模型 BOM里面的方法大多在window对象底下,window代表窗口,也就是说,在BOM里面大多调用window下面的东西. 1.open方法是wi ...

  6. 论好的代码习惯的养成/做一个优雅的coder

    1.先说一下以前被滴滴大佬教育的事情: 以前写代码的时候,因为只需要取特定的几个字段,所以经常这么写 //Request $request for example $parameters = $req ...

  7. CentOS源码安装 Tomcat/8.0.24

    依个人的习惯,喜欢将源码安装在/usr/local这个目录下面: 第一步:下载源码 wget http://archive.apache.org/dist/tomcat/tomcat-8/v8.0.2 ...

  8. protobuf ubuntu 18.04环境下安装

    (t20190518) luo@luo-All-Series:~/MyFile$ (t20190518) luo@luo-All-Series:~/MyFile$ (t20190518) luo@lu ...

  9. Data - 数据挖掘的基础概念

    主要内容来自于<微信公众号:程SIR说> 1 数据挖掘 数据挖掘(Data Mining,简称DM),是指从大量的数据中,挖掘出未知的且有价值的信息和知识的过程. 数据挖掘是一门交叉学科, ...

  10. Eureka报错: Connect to localhost:8761 timed out

    最近整理配置Eureka时, 注册服务后, Eureka服务一直报出如下错误: 如下是我的单台eureka的 application.yml 配置: spring: application: name ...