题解:AT_abc358_c [ABC358C] Popcorn

· · 题解

一道子集枚举题。

我们用 N 位二进制数来表示高桥买了哪些摊位的爆米花,然后检查一下,如果合法,答案取二进制数 1 的个数的最小值即可。

时间复杂度 O(2^N \times (N+M))。

代码如下:

int n, m;
string str[13];
int popcount(int x) {
    if (x == 0) return 0;
    return popcount(x >> 1) + (x & 1);
}
int main() {
    cin >> n >> m;
    FORL(i, 1, n, 1) {
        cin >> str[i];
    }
    int ans = 19;
    FORL(i, 1, (1 << n) - 1, 1) {
        string s = "";
        FORL(j, 1, m, 1) s += " ";
        FORL(j, 0, n - 1, 1) {
            if ((i >> j) & 1) {
                int now = n - j;
                FORL(k, 0, m - 1, 1) {
                    if (str[now][k] == 'o') {
                        s[k] = 'o';
                    }
                }
            }
        }
        int cnt = 0;
        FORL(j, 0, m - 1, 1) {
            if (s[j] == 'o') ++cnt;
        }
        if (cnt == m) {
            ans = min(ans, popcount(i));
        }
    }
    write(ans);
}