Rotate Columns (easy version)

Rotate Columns (easy version)

Created by LXC on Tue Jan 9 19:20:12 2024

https://codeforces.com/problemset/problem/1209/E1

ranting: 2000

tag: bitmasks, brute force, dp, greedy, sortings

problem

给定一个 n×m 的矩阵,你可以对每一列进行若干次循环移位

求操作完成后每一行的最大值之和

solution

按照每列最大值进行排序,发现最多min(m,n)列是有用的,我们只需要暴力枚举最多4列的循环。

code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62

#include <bits/stdc++.h>
// #define SINGLE_INPUT
#define ll long long
#define ull unsigned long long
#define N 500005
#define MOD 998244353
using namespace std;


void sol() {
int n, m;
cin >> n >> m;
vector<vector<int>> a(m, vector<int>(n));
for (int i=0; i<n; i++) {
for (int j=0; j<m; j++) {
cin >> a[j][i];
}
}
sort(a.begin(), a.end(), [](auto& x, auto& y) {
return *max_element(x.begin(), x.end()) > *max_element(y.begin(), y.end());
});
int sz = min(n,m), ans = 0;
vector<int> c(sz);
auto dfs = [&](auto self, int x) {
if (x == sz) {
int s = 0;
for (int i=0; i<n; i++) {
int mx = 0;
for (int j=0; j<sz; j++) {
mx = max(mx, a[j][(i+c[j])%n]);
}
s += mx;
}
ans = max(ans, s);
return ;
}
for (int i=0; i<n; i++) {
c[x] = i;
self(self, x+1);
}
};
dfs(dfs, 0);
cout << ans << "\n";
}

int main() {
cout << setprecision(15) << fixed;
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
#ifndef SINGLE_INPUT
int t;
cin >> t;
while (t--) {
sol();
}
#else
sol();
#endif
return 0;
}