【搜索】Partition problem
题目链接:传送门
题面:


【题意】
给定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的更多相关文章
- 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 ...
- 2019牛客多校第二场F Partition problem 暴力+复杂度计算+优化
Partition problem 暴力+复杂度计算+优化 题意 2n个人分成两组.给出一个矩阵,如果ab两个在同一个阵营,那么就可以得到值\(v_{ab}\)求如何分可以取得最大值 (n<14 ...
- 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 ...
- 2019牛客暑期多校训练营(第二场) - F - Partition problem - 枚举
https://ac.nowcoder.com/acm/contest/882/F 潘哥的代码才卡过去了,自己写的都卡不过去,估计跟评测机有关. #include<bits/stdc++.h&g ...
- 2019牛客暑期多校训练营(第二场)F.Partition problem
链接:https://ac.nowcoder.com/acm/contest/882/F来源:牛客网 Given 2N people, you need to assign each of them ...
- 2019年牛客多校第二场 F题Partition problem 爆搜
题目链接 传送门 题意 总共有\(2n\)个人,任意两个人之间会有一个竞争值\(w_{ij}\),现在要你将其平分成两堆,使得\(\sum\limits_{i=1,i\in\mathbb{A}}^{n ...
- 2019牛客多校2 F Partition problem(dfs)
题意: n<=28个人,分成人数相同的两组,给你2*n*2*n的矩阵,如果(i,j)在不同的组里,竞争力增加v[i][j],问你怎么分配竞争力最 4s 思路: 枚举C(28,14)的状态,更新答 ...
- 2019牛客多校第二场F Partition problem(暴搜)题解
题意:把2n个人分成相同两组,分完之后的价值是val(i, j),其中i属于组1, j属于组2,已知val表,n <= 14 思路:直接dfs暴力分组,新加的价值为当前新加的人与不同组所有人的价 ...
- 2019牛客多校第二场F-Partition problem(搜索+剪枝)
Partition problem 题目传送门 解题思路 假设当前两队的对抗值为s,如果把红队中的一个人a分配到白队,s+= a对红队中所有人的对抗值,s-= a对白队中所有人的对抗值.所以我们可以先 ...
随机推荐
- 使用axios请求的坑
配置axios在vue-cli中的使用: 在main.js中配置 import axios from "axios" Vue.config.productionTip = fals ...
- jquer绑定和获取属性
最近每天都在熬夜,今天感觉眼睛特别涩,我决定,今天早睡,哈哈哈,上次总结了jquery控制节点,今天总结jquery控制属性,学习完基础知识,看看下面的案例练习一下,掌握的会更好 属性绑定和获取 ...
- Jmeter Web 性能测试入门 (四):一个小实例带你学会 Jmeter 脚本编写
测试场景: 模拟并发100个user,在TesterHome 站内搜索VV00CC 添加线程组 添加HTTP信息头管理器 添加HTTP Sampler 填写HTTP Sampler中的信息 添加监听器 ...
- JS合并多个数组去重算法
var arr1 = ['a','b']; var arr2 = ['a','c','d']; var arr3 = [1,'d',undefined,true,null]; //合并两个数组,去重 ...
- vue+七牛云 截图工具
1.安装:npm install vue-cropper 2.引入:import VueCropper from 'vue-cropper' Vue.use(VueCropper) <templ ...
- linux高可用集群(HA)原理详解
高可用集群 一.什么是高可用集群 高可用集群就是当某一个节点或服务器发生故障时,另一个节点能够自动且立即向外提供服务,即将有故障节点上的资源转移到另一个节点上去,这样另一个节点有了资源既可以向外提供服 ...
- ubuntu18 faster-rcnn
luo@luo-All-Series:~/MyFile$ luo@luo-All-Series:~/MyFile$ luo@luo-All-Series:~/MyFile$ git clone htt ...
- DOS & UNIX文件格式转换
1.使用vi编辑器 vi xxxx :set fileformat=unix(or dos) :wq 2.使用 dos2unix 这个只能把DOS转换成UNIX文件 . sudo apt-get in ...
- OpenGL(5)——变换
学习三种变换:Scaling, Rotation和Translation. 上学期修了Kobbelt教授(男神!)的图形学基础课,这部分内容已经接触过. 添加GLM库,直接给出齐次坐标系下的变换矩阵和 ...
- 【文章存档】Local Git 如何部署分支
又来存档文章了,链接 https://docs.azure.cn/zh-cn/articles/azure-operations-guide/app-service-web/aog-app-servi ...