2018 ACM-ICPC, Syrian Collegiate Programming Contest F - Pretests SOS dp

#include<bits/stdc++.h>
#define LL long long
#define fi first
#define se second
#define mk make_pair
#define PLL pair<LL, LL>
#define PLI pair<LL, int>
#define PII pair<int, int>
#define SZ(x) ((int)x.size())
#define ull unsigned long long
using namespace std;

const int N = 2e5 + 7;
const int inf = 0x3f3f3f3f;
const LL INF = 0x3f3f3f3f3f3f3f3f;
const int mod = 1e9 + 7;
const double eps = 1e-3;
const double PI = acos(-1);

int T, m, n, up, dp2[1<<20], dp[1<<20], pre[1<<20];
char s[100];
vector<int> ans;

int main() {
//    freopen("tests.in", "r", stdin);
    scanf("%d", &T);
    while(T--) {
        ans.clear();
        scanf("%d%d", &m, &n);
        up = 1 << m;
        for(int i = 0; i < up; i++) dp2[i] = 0, dp[i] = inf;
        for(int i = 0; i < n; i++) {
            scanf("%s", s); int x = 0;
            for(int j = 0; j < m; j++)
                if(s[j] == ‘1‘) x += 1 << j;
            dp2[x]++;
        }
        for(int i = 0; i < m; i++)
            for(int s = 0; s < up; s++)
                if(s>>i&1) dp2[s^(1<<i)] += dp2[s];
        dp[0] = 0;
        for(int s = 0; s < up; s++) {
            for(int i = 0; i < m; i++) {
                if(s>>i&1) continue;
                int nexs = s | (1<<i);
                if(dp[s]+dp2[s] < dp[nexs]) {
                    dp[nexs] = dp[s]+dp2[s];
                    pre[nexs] = s;
                }
            }
        }
        int nows = up-1, nexs;
        while(nows) {
            nexs = pre[nows];
            int xo = nexs ^ nows;
            for(int i = 0; i < m; i++) {
                if(1<<i == xo) {
                    ans.push_back(i+1);
                    break;
                }
            }
            nows = nexs;
        }
        reverse(ans.begin(), ans.end());
        printf("%d\n", dp[up-1]);
        for(int t : ans) printf("%d ", t);
        puts("");
    }
    return 0;
}
/*
*/

原文地址:https://www.cnblogs.com/CJLHY/p/10200426.html

时间: 2024-11-23 08:16:30

2018 ACM-ICPC, Syrian Collegiate Programming Contest F - Pretests SOS dp的相关文章

2018 ACM-ICPC, Syrian Collegiate Programming Contest

2018 ACM-ICPC, Syrian Collegiate Programming Contest A Hello SCPC 2018! 水题 B Binary Hamming 水题 C Portals 思路:并查集维护连通性 代码: //#pragma GCC optimize(3) //#pragma comment(linker, "/STACK:102400000,102400000") //c++ // #pragma GCC diagnostic error &quo

(寒假开黑gym)2018 ACM-ICPC, Syrian Collegiate Programming Contest

layout: post title: (寒假开黑gym)2018 ACM-ICPC, Syrian Collegiate Programming Contest author: "luowentaoaa" catalog: true tags: mathjax: true - codeforces 传送门 付队! 许老师! Hello SCPC 2018! (签到) #include<bits/stdc++.h> using namespace std; typedef

(寒假开黑gym)2018 ACM-ICPC, Syrian Collegiate Programming Contest(爽题)

layout: post title: (寒假开黑gym)2018 ACM-ICPC, Syrian Collegiate Programming Contest(爽题) author: "luowentaoaa" catalog: true tags: mathjax: true - codeforces - DP - 状态压缩 - LCA 传送门 付队! C - Greetings! (状态压缩) 题意 给N种信件,你可以任意选择K种信封装信件,问你最少的浪费是多少 不能大的信件装

ACM International Collegiate Programming Contest, Tishreen Collegiate Programming Contest (2018) Syria, Lattakia, Tishreen University, April, 30, 2018

ACM International Collegiate Programming Contest, Tishreen Collegiate Programming Contest (2018) Syria, Lattakia, Tishreen University, April, 30, 2018 Problem A. Can Shahhoud Solve it? Problem B. Defeat the Monsters Problem C. UCL Game Night Problem

ACM International Collegiate Programming Contest, JUST Collegiate Programming Contest (2018)

ACM International Collegiate Programming Contest, JUST Collegiate Programming Contest (2018) B. New Assignment 有n个人(1?≤?n?≤?104),有男有女,每个人都有一个id,现在这n个人分成学习互助小组,有三种组队模式,一个男人一组,一个女人一组,一男一女一组,如果要一男一女一组,那么这两人id的gcd要>1.保证任意三个人的gcd=1.求小组的组数最少是多少? 看起来是一个很裸的二

2018 German Collegiate Programming Contest (GCPC 18)

2018 German Collegiate Programming Contest (GCPC 18) Attack on Alpha-Zet 建树,求lca 代码: #include <algorithm> #include <iterator> #include <iostream> #include <cstring> #include <cstdlib> #include <iomanip> #include <bit

(寒假GYM开黑)2018 German Collegiate Programming Contest (GCPC 18)

layout: post title: 2018 German Collegiate Programming Contest (GCPC 18) author: "luowentaoaa" catalog: true tags: mathjax: true - codeforces 传送门 付队博客 C.Coolest Ski Route (记忆化搜索) 题意 给出一个有向图,求出一个权值最长的链, 题解 暴力dfs会超时,所以直接储存每个起点能走到的最远距离 #include<

(寒假GYM开黑)2018-2019 ACM-ICPC Nordic Collegiate Programming Contest (NCPC 2018)

layout: post title: 2018-2019 ACM-ICPC Nordic Collegiate Programming Contest (NCPC 2018) author: "luowentaoaa" catalog: true tags: mathjax: true - codeforces 传送门 付队! B.Baby Bites (签到模拟) 按照题意模拟就行了 int a[maxn]; string s; int main() { std::ios::syn

The 43rd ACM International Collegiate Programming Contest Asia Shenyang Regional Contest

The 43rd ACM International Collegiate Programming Contest Asia Shenyang Regional Contest 原文地址:https://www.cnblogs.com/Accpted/p/11298233.html