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


    题目链接

    传送门

    题意

    总共有(2n)个人,任意两个人之间会有一个竞争值(w_{ij}),现在要你将其平分成两堆,使得(sumlimits_{i=1,iinmathbb{A}}^{n}sumlimits_{j=1,jinmathbb{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("*********
    ")
    #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
    ", ans);
        return 0;
    }
    
  • 相关阅读:
    自动安装rpm依赖
    goroutine上下文contxt语法
    goroutine 上下文用法
    Template Method 模式
    设计模式2--设计原则
    centos7关机自动进行远程服务器备份
    实用工具使用
    剑指offer python版 滑动窗口的最大值
    剑指offer python版 左旋转字符串
    剑指offer python版 翻转单词顺序
  • 原文地址:https://www.cnblogs.com/Dillonh/p/11219011.html
Copyright © 2020-2023  润新知