TLE求条
查看原帖
TLE求条
1623120
_username_楼主2024/12/28 13:01
#include <cstdio>
#include <vector>
#include <algorithm>
#include <string>

using namespace std;

int max_strength(int n, int m, int x, int y, const vector<string>& mat) {
    int max_strength = -1e9;
    vector<int> r(n, 0), c(m, 0);
    
    for (int i = 0; i < n; ++i)
        for (int j = 0; j < m; ++j)
            if (mat[i][j] == '1') r[i]++, c[j]++;
    
    for (int rm = 0; rm < (1 << n); ++rm)
        for (int cm = 0; cm < (1 << m); ++cm) {
            int rw = __builtin_popcount(rm), cl = __builtin_popcount(cm), w = 0;
            for (int i = 0; i < n; ++i)
                if (rm & (1 << i))
                    for (int j = 0; j < m; ++j)
                        if (cm & (1 << j)) if (mat[i][j] == '1') w++;
            max_strength = max(max_strength, w - x * rw - y * cl);
        }
    
    return max_strength;
}

int main() {
    int n, m, x, y;
    scanf("%d %d %d %d", &n, &m, &x, &y);
    
    vector<string> mat(n);
    for (int i = 0; i < n; ++i) {
        mat[i].resize(m);
        scanf("%s", &mat[i][0]);
    }
    
    printf("%d\n", max_strength(n, m, x, y, mat));
    
    return 0;
}

2024/12/28 13:01
加载中...