比赛 2024.5.23练习赛 评测结果 AAAAAAAAAA
题目名称 Rotate Columns 最终得分 100
用户昵称 darkMoon 运行时间 0.031 s
代码语言 C++ 内存使用 2.90 MiB
提交时间 2024-05-27 09:26:24
显示代码纯文本
#include<bits/stdc++.h>
#define int long long
using namespace std;
ifstream fin("happygameT1.in");
ofstream fout("happygameT1.out");
auto mread = [](){int x;fin >> x;return x;};
int t = mread(), a[105][10], f[105][30], s[105][30];
signed main(){
    while(t --){
        int n = mread(), m = mread();
        for(int i = 1; i <= n; i ++){
            for(int j = 1; j <= m; j ++)
            a[j][i] = mread();
        }
        int ma = (1 << n);
        // printf("***\n");
        for(int i = 1; i <= m; i ++){
            for(int j = 0; j < ma; j ++)
            f[i][j] = s[i][j] = 0;
        }
        for(int i = 1; i <= m; i ++){
            for(int now = 0; now < ma; now ++){
                for(int j = 0; j < n; j ++){
                    int sum = 0;
                    for(int k = 0; k < n; k ++){
                        int t = (k + j) % n + 1;
                        if(1 & (now >> t - 1))
                        sum += a[i][k + 1];
                    }
                    s[i][now] = max(s[i][now], sum);
                }
                // cout << "*** " << i << " " << (bitset<4>)now << " " << s[i][now] << "\n";
            }
        }
        for(int i = 1; i <= m; i ++){
            for(int x = 0; x < ma; x ++){
                for(int y = x; y; -- y &= x){
                    // cout << "*** " << i << " " << (bitset<4>)x << " " << (bitset<4>)y << "\n";
                    f[i][x] = max(f[i][x], f[i - 1][y] + s[i][x ^ y]);
                }
                f[i][x] = max(f[i][x], s[i][x]);
                // cout << "--- " << i << " " << (bitset<4>)x << " " << f[i][x] << "\n";
            }
        }
        fout << f[m][ma - 1] << "\n";
    }
    return 0;
}