【搜索】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对白队中所有人的对抗值.所以我们可以先 ...
随机推荐
- JavaEE项目开发所需要的包(Struts2+Spring5+Hibernate5)
在这里我只整理了轻量级JavaEE项目开发所需的包 @Auther MrZhangxd 2019-04-29 23:07:21 链接:https://pan.baidu.com/s/16I4KYah ...
- 黑马vue---28、vue中全局过滤器的基本使用
黑马vue---28.vue中全局过滤器的基本使用 一.总结 一句话总结: vue中的过滤器可以传递参数(根据参数来过滤),也可以用管道符拼接多个过滤器:例如<p>{{ msg | msg ...
- vue实现全选框效果
vue实现全选框效果 一.总结 一句话总结: 全选的checkbox点击的时候判断这个checkbox的状态,如果没选中,说明下一个操作是选中所有 下面的每个checkbox判断一下是否所有的chec ...
- arcpy workspace already in transaction mode
arcpy workspace already in transaction mode RuntimeError: workspace already in transaction mode 同一个工 ...
- 《maven实战》笔记(1)----maven的初识
刚入职公司用maven进行项目管理,于是昨天下午开始看<maven实战>的pdf,感觉很好,作者写的很有条理. 下面是笔记,看书做笔记还是很有必要的,加强自己的总结. 什么是maven? ...
- LC 988. Smallest String Starting From Leaf
Given the root of a binary tree, each node has a value from 0 to 25 representing the letters 'a' to ...
- kotlin泛型类型变异
在java泛型中中会有 ? extends E 可以解决类似于List<String> 赋给List<Object> 的问题,但是在kotlin泛型中并没有提供通配符,而是o ...
- 比特币区块的hash算法
Block hashing algorithm Bitcoin mining uses the hashcash proof of work function; the hashcash algori ...
- 读写Session
读写Session Session是保存在服务端的字典 Session与Cookie有些类似,都是通过字典管理key-value对,只不过Cookie是保存在客户端的字典,而Session是保存在服务 ...
- sed例子
以care.log这个log文件为例, care.log: 05:44:31,816 DEBUG RawAggregationWorker:70 - LTS is working on Raw Dat ...