2019年牛客多校第二场 F题Partition problem 爆搜
题目链接
题意
总共有\(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 爆搜的更多相关文章
- 2019年牛客多校第二场 H题Second Large Rectangle
题目链接 传送门 题意 求在\(n\times m\)的\(01\)子矩阵中找出面积第二大的内部全是\(1\)的子矩阵的面积大小. 思路 处理出每个位置往左连续有多少个\(1\),然后对每一列跑单调栈 ...
- MAZE(2019年牛客多校第二场E题+线段树+矩阵乘法)
题目链接 传送门 题意 在一张\(n\times m\)的矩阵里面,你每次可以往左右和下三个方向移动(不能回到上一次所在的格子),\(1\)表示这个位置是墙,\(0\)为空地. 现在有\(q\)次操作 ...
- Kth Minimum Clique(2019年牛客多校第二场D题+k小团+bitset)
目录 题目链接 题意 思路 代码 题目链接 传送门 题意 找第\(k\)小团. 思路 用\(bitset\)来标记每个结点与哪些结点直接有边,然后进行\(bfs\),在判断新加入的点与现在有的点是否都 ...
- 2019年牛客多校第一场B题Integration 数学
2019年牛客多校第一场B题 Integration 题意 给出一个公式,求值 思路 明显的化简公式题,公式是分母连乘形式,这个时候要想到拆分,那如何拆分母呢,自然是裂项,此时有很多项裂项,我们不妨从 ...
- 2019牛客多校第二场H题(悬线法)
把以前的题补补,用悬线求面积第二大的子矩形.我们先求出最大子矩阵的面积,并记录其行三个方向上的悬线长度.然后排除这个矩形,记得还得特判少一行或者少一列的情况 #include <bits/std ...
- 2019牛客多校第二场F Partition problem 暴力+复杂度计算+优化
Partition problem 暴力+复杂度计算+优化 题意 2n个人分成两组.给出一个矩阵,如果ab两个在同一个阵营,那么就可以得到值\(v_{ab}\)求如何分可以取得最大值 (n<14 ...
- 2019牛客多校第二场F Partition problem(暴搜)题解
题意:把2n个人分成相同两组,分完之后的价值是val(i, j),其中i属于组1, j属于组2,已知val表,n <= 14 思路:直接dfs暴力分组,新加的价值为当前新加的人与不同组所有人的价 ...
- Cutting Bamboos(2019年牛客多校第九场H题+二分+主席树)
题目链接 传送门 题意 有\(n\)棵竹子,然后有\(q\)次操作,每次操作给你\(l,r,x,y\),表示对\([l,r]\)区间的竹子砍\(y\)次,每次砍伐的长度和相等(自己定砍伐的高度\(le ...
- 2019年牛客多校第一场 I题Points Division 线段树+DP
题目链接 传送门 题意 给你\(n\)个点,每个点的坐标为\((x_i,y_i)\),有两个权值\(a_i,b_i\). 现在要你将它分成\(\mathbb{A},\mathbb{B}\)两部分,使得 ...
随机推荐
- 记遇到的Release和Debug下有些不同
平常开发用Debug,但是发布的时候用Release,应该是很多单位都会用的,但是有的时候你发现Debug下好使,Release下不好使,这就遇到坑了. 我也是这两天连续遇到了两次,在此记录一下,如果 ...
- 使用Redis搭建电商秒杀系统
背景 秒杀活动是绝大部分电商选择的低价促销.推广品牌的方式.不仅可以给平台带来用户量,还可以提高平台知名度.一个好的秒杀系统,可以提高平台系统的稳定性和公平性,获得更好的用户体验,提升平台的口碑,从而 ...
- 【技术博客】利用Python将markdown文档转为html文档
利用Python将markdown文档转为html文档 v1.0 作者:FZK 元素简单的md文件 Python中自带有一个markdown库,你可以直接这样使用 md_file = open(&qu ...
- 把ubuntu自带的高gcc版本降到低版本(如gcc 3.4)的方法
转载自: 博客1.博客2 .博客3 步骤 第一步: 下载所需gcc安装包(.deb格式) 手动:老版本gcc下载地址:http://old-releases.ubuntu.com/ubuntu/poo ...
- 解决 ImportError: cannot import name 'initializations' from 'keras' (C:\Users\admin\AppData\Roaming\Python\Python37\site-packages\keras\__init__.py)
解决 ImportError: cannot import name 'initializations' from 'keras' : [原因剖析] 上述代码用的是 Keras version: '1 ...
- 读《PMI 分析手册》
目录 读<PMI 分析手册> 官方 PMI 基本概况 官方制造业 PMI 官方非制造业 PMI 综合 PMI 产出指数 PMI 分析框架 PMI 与经济周期 官方 PMI 分析 参考研报 ...
- etcd备份与恢复
目录 备份 恢复 备份 通常而言,etcd都是集群部署,其实并不需要额外备份,但实在是架不住猪队友误操作. 写一个简单的etcd备份脚本如下: #!/bin/bash set -e exec > ...
- Springboot Actuator之八:actuator的执行原理
本文接着<Springboot Actuator之七:actuator 中原生endpoint源码解析1>,前面主要分析了原生endpoint的作用. 现在着重了解actuator的执行原 ...
- spring 事件使用
1.事件定义 import lombok.Data; import org.springframework.context.ApplicationEvent; /** * 事件定义,这里监听MsgMe ...
- 百度前端技术学院task14源代码
刚开始理解错误,以为是读取对象,结果后面才发现是二维数组. 另外对于数组排序,创建新的节点啊,输入到doM中啊,都不是很熟悉. <!DOCTYPE html> <html> & ...