题目链接

传送门

题意

总共有\(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. [记录]mscorlib recursive resource lookup bug解决方法

    [Content]Expression: [mscorlib recursive resource lookup bug]Description: Infinite recursion during ...

  2. 【转帖】nmap命令总结

    nmap命令总结 https://www.cnblogs.com/chenqionghe/p/10657722.html 一.nmap是什么 nmap是一款网络扫描和主机检测的非常有用的工具,不局限于 ...

  3. 使用Clion优雅的完全远程自动同步和远程调试c++

    摘要:在linux上用vim写C++的时候,通常用gdb进行调试,不能随心所欲的看代码和跳转代码以及加watch(也有可能是因为我还没有get正确的使用方法).为此我发现Clion可以做到自动同步本场 ...

  4. Dart面向对象编程(一)

    基本内容概述: 类与对象: 计算属性: void main(){ var rect = new Rectangle(); rect.width = 20; rect.height = 10; prin ...

  5. [SOJ #687]双生串(2019-11-6考试)/[hdu5431]AB String

    题目大意 把所有仅包含\(AB\)的字符串按字典序排列,给你一个仅包含\(AB\)的字符串\(S\),然后有\(Q\)个问题,第\(i\)个问题给你\(k_i\),求不是\(S\)的子串中,第\(k_ ...

  6. windows上 nginx 配置代理服务,配置多域名,以及最简单实现跨域配置

    Nginx,不用多说啦,大家都熟悉的不能再熟悉了,它是一款轻量级的高性能Web 服务器/反向代理服务器及电子邮件(IMAP/POP3)代理服务器,最近在本地研究将nginx和resin配合使用,使服务 ...

  7. (Manjaro)VirtualBox异常修复:RTR3InitEx failed with rc=-1912 (rc=-1912)

    引言 VirtualBox运行异常好几天,其中尝试一些操作都没有解决. 版本说明 系统版本:4.19.88-1-MANJARO Vbox镜像:kali-linux-2019.4-vbox-amd64. ...

  8. spring Boot 学习(八、Spring Boot与与监控管理)

    一.监控管理通过引入spring-boot-starter-actuator,可以使用Spring Boot为我们提供的准 生产环境下的应用监控和管理功能.我们可以通过HTTP,JMX,SSH协议来进 ...

  9. 象棋中“车”的攻击范围_Java

    代码如下: String[][] a = new String[8][8]; int h, l; Scanner scan = new Scanner(System.in); System.out.p ...

  10. Django--模型层进阶

    目录 QuerySet对象 可切片 可迭代 惰性查询 缓存机制 何时查询集不会被缓存? exists()与iterator()方法 exists() iterator() 中介模型 查询优化 表数据 ...