2019年牛客多校第二场 F题Partition problem 爆搜

题目链接

传送门

题意

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

思路

看到这一题第一想法是状态压缩然后枚举状态,然后人就没了。
其实这题就是个普通的 d f s dfs dfs,假设在枚举第 i i i个人时,前面已经有 t o t 1 tot1 tot1个人分进了 A \mathbb{A} A t o t 2 tot2 tot2个人分进了 B \mathbb{B} B中,则

  • 如果 t o t 1 &lt; n tot1&lt;n tot1<n,那么 i i i可以放进 A \mathbb{A} A中,在放进去的时候将 s u m sum sum加上 i i i与前 t o t 2 tot2 tot2个人的竞争值;
  • 如果 t o t 2 &lt; n tot2&lt;n tot2<n,那么 i i i可以放进 B \mathbb{B} B中,在放进去的时候将 s u m sum sum加上 i i i与前 t o t 1 tot1 tot1个人的竞争值。

t o t 1 , t o t 2 tot1,tot2 tot1,tot2都等于 n n n时将 s u m sum sum a n s ans ans进行取 m a x max max即可,复杂度为 O ( n C 2 n n ) O(nC_{2n}^{n}) O(nC2nn)

代码实现如下

#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;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值