题目链接

传送门

题意

总共有\(2n\)个人,任意两个人之间会有一个竞争值\(w_{ij}\),现在要你将其平分成两堆,使得\(\sum\limits_{i=1,i\in\mathbb{A}}^{n}\sum\limits_{j=1,j\in\mathbb{B}}^{n}w_{ij}\)最大。

思路

看到这一题第一想法是状态压缩然后枚举状态,然后人就没了。

其实这题就是个普通的\(dfs\),假设在枚举第\(i\)个人时,前面已经有\(tot1\)个人分进了\(\mathbb{A}\),\(tot2\)个人分进了\(\mathbb{B}\)中,则

  • 如果\(tot1<n\),那么\(i\)可以放进\(\mathbb{A}\)中,在放进去的时候将\(sum\)加上\(i\)与前\(tot2\)个人的竞争值;
  • 如果\(tot2<n\),那么\(i\)可以放进\(\mathbb{B}\)中,在放进去的时候将\(sum\)加上\(i\)与前\(tot1\)个人的竞争值。

在\(tot1,tot2\)都等于\(n\)时将\(sum\)与\(ans\)进行取\(max\)即可,复杂度为\(O(nC_{2n}^{n})\)。

代码实现如下

#include <set>
#include <map>
#include <deque>
#include <queue>
#include <stack>
#include <cmath>
#include <ctime>
#include <bitset>
#include <cstdio>
#include <string>
#include <vector>
#include <cassert>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <unordered_map>
using namespace std; typedef long long LL;
typedef pair<LL, LL> pLL;
typedef pair<LL, int> pLi;
typedef pair<int, LL> pil;;
typedef pair<int, int> pii;
typedef unsigned long long uLL; #define lson rt<<1
#define rson rt<<1|1
#define lowbit(x) x&(-x)
#define name2str(name) (#name)
#define bug printf("*********\n")
#define debug(x) cout<<#x"=["<<x<<"]" <<endl
#define FIN freopen("D://Code//in.txt","r",stdin)
#define IO ios::sync_with_stdio(false),cin.tie(0) const double eps = 1e-8;
const int mod = 1000000007;
const int maxn = 1e5 + 7;
const double pi = acos(-1);
const int inf = 0x3f3f3f3f;
const LL INF = 0x3f3f3f3f3f3f3f3fLL; LL ans;
int n, tot1, tot2;
int mp[30][30], a[30], b[30]; void dfs(int pos, LL sum) {
if(tot1 > n || tot2 > n) return;
if(tot1 == n && tot2 == n) {
ans = max(ans, sum);
return;
}
if(tot1 < n) {
a[++tot1] = pos;
for(int i = 1; i <= tot2; ++i) {
sum += mp[pos][b[i]];
}
dfs(pos + 1, sum);
for(int i = 1; i <= tot2; ++i) {
sum -= mp[pos][b[i]];
}
--tot1;
}
if(tot2 < n) {
b[++tot2] = pos;
for(int i = 1; i <= tot1; ++i) {
sum += mp[pos][a[i]];
}
dfs(pos + 1, sum);
for(int i = 1; i <= tot1; ++i) {
sum -= mp[pos][a[i]];
}
--tot2;
}
} int main() {
#ifndef ONLINE_JUDGE
FIN;
#endif // ONLINE_JUDGE
scanf("%d", &n);
for(int i = 1; i <= 2 * n; ++i) {
for(int j = 1; j <= 2 * n; ++j) {
scanf("%d", &mp[i][j]);
}
}
dfs(1, 0);
printf("%lld\n", ans);
return 0;
}

2019年牛客多校第二场 F题Partition problem 爆搜的更多相关文章

  1. 2019年牛客多校第二场 H题Second Large Rectangle

    题目链接 传送门 题意 求在\(n\times m\)的\(01\)子矩阵中找出面积第二大的内部全是\(1\)的子矩阵的面积大小. 思路 处理出每个位置往左连续有多少个\(1\),然后对每一列跑单调栈 ...

  2. MAZE(2019年牛客多校第二场E题+线段树+矩阵乘法)

    题目链接 传送门 题意 在一张\(n\times m\)的矩阵里面,你每次可以往左右和下三个方向移动(不能回到上一次所在的格子),\(1\)表示这个位置是墙,\(0\)为空地. 现在有\(q\)次操作 ...

  3. Kth Minimum Clique(2019年牛客多校第二场D题+k小团+bitset)

    目录 题目链接 题意 思路 代码 题目链接 传送门 题意 找第\(k\)小团. 思路 用\(bitset\)来标记每个结点与哪些结点直接有边,然后进行\(bfs\),在判断新加入的点与现在有的点是否都 ...

  4. 2019年牛客多校第一场B题Integration 数学

    2019年牛客多校第一场B题 Integration 题意 给出一个公式,求值 思路 明显的化简公式题,公式是分母连乘形式,这个时候要想到拆分,那如何拆分母呢,自然是裂项,此时有很多项裂项,我们不妨从 ...

  5. 2019牛客多校第二场H题(悬线法)

    把以前的题补补,用悬线求面积第二大的子矩形.我们先求出最大子矩阵的面积,并记录其行三个方向上的悬线长度.然后排除这个矩形,记得还得特判少一行或者少一列的情况 #include <bits/std ...

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

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

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

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

  8. Cutting Bamboos(2019年牛客多校第九场H题+二分+主席树)

    题目链接 传送门 题意 有\(n\)棵竹子,然后有\(q\)次操作,每次操作给你\(l,r,x,y\),表示对\([l,r]\)区间的竹子砍\(y\)次,每次砍伐的长度和相等(自己定砍伐的高度\(le ...

  9. 2019年牛客多校第一场 I题Points Division 线段树+DP

    题目链接 传送门 题意 给你\(n\)个点,每个点的坐标为\((x_i,y_i)\),有两个权值\(a_i,b_i\). 现在要你将它分成\(\mathbb{A},\mathbb{B}\)两部分,使得 ...

随机推荐

  1. java基础 面向对象 & 接口 & 抽象类

    从语法层面而言,接口和抽象类的区别如下: 1.抽象类可以提供成员方法的实现细节,而接口中只能存在抽象方法(默认 public abstract)2.抽象类中的成员变量可以是多种类型,而接口中的成员变量 ...

  2. Elasticsearch运维经验总结

    Elasticsearch运维经验总结 2018年12月10日 16:38:41 运小白 阅读数 3811   版本说明:5.6.4(要严格注意ES及其插件.第三方工具的版本匹配关系) 系统负载:(日 ...

  3. Mysql变量、存储过程、函数、流程控制

    一.系统变量 系统变量: 全局变量 会话变量 自定义变量: 用户变量 局部变量 说明:变量由系统定义,不是用户定义,属于服务器层面 注意:全局变量需要添加global关键字,会话变量需要添加sessi ...

  4. FreeSWITCH 总体架构

    [1]总体结构 [2]代码结构目录 [3]模块简介 Applications应用 mod_abstraction – 提供了一个抽象的API调用(未来有更多功能)Provides an abstrac ...

  5. 使用DbVisualizer 10.0.20 查询ES中的索引时需要注意的事项

    查询前5条数据 光标停在某一个查询结果框中,左下角会显示该字段的类型 查询类型是text的字段使用单引号,使用双引号查询会报错

  6. mysql 中的日期格式。date_format( ) 转换格式

    date_format( ) 转换格式 : 格式 描述 %a 缩写星期名 %b 缩写月名 %c 月,数值 %D 带有英文前缀的月中的天 %d 月的天,数值(00-31) %e 月的天,数值(0-31) ...

  7. C# 字符串和字节数组转换

    转自:http://blog.sina.com.cn/s/blog_683d60ff0100rhwk.html 定义string变量为str,内存流变量为ms,比特数组为bt 1.字符串转比特数组 ( ...

  8. MVC学习笔记(六)---遇到的小问题汇总

    一.MVC中Controller中返回两个对象的写法如下: , msg = "成功", user = user, userInfo = person }); 二.前台向后台传入带有 ...

  9. The underlying connection was closed: An unexpected error occurred on a receive

    解决方法 webRequest.KeepAlive = false; ServicePointManager.ServerCertificateValidationCallback += (s, ce ...

  10. 学习笔记之Python 3

    学习笔记之Python 3 教程 https://www.cnblogs.com/pegasus923/p/7624416.html 学习笔记之X分钟速成Python3 https://www.cnb ...