比赛 20191022轻松模拟测试 评测结果 AAAAAAAAAA
题目名称 Rotate Columns 最终得分 100
用户昵称 subaru 运行时间 0.000 s
代码语言 C++ 内存使用 0.00 MiB
提交时间 2019-10-22 17:27:13
显示代码纯文本
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>

using namespace std;

inline int read() {
	int op = 1, a = 0; char c = getchar();
	for (; c < '0' || c > '9'; c = getchar()) if (c == '-') op = -1;
	for (; c >= '0' && c <= '9'; c = getchar()) a = a * 10 + c - '0';
	return op * a;
}

const int maxm = 4e2 + 5;
int T, n, m;
int a[maxm][maxm];

int ans;

namespace NmorethanM {
	int fr[maxm];
	void calc() {
		int tmp_ans = 0;
		for (int i = 1; i <= n; i++) {
			int maxi = 0;
			for (int j = 1; j <= m; j++)
				maxi = max(maxi, a[(i + fr[j] - 1 - 1) % n + 1][j]);
			tmp_ans += maxi;
		}
		ans = max(ans, tmp_ans);
	}
	void dfs(int pos, int num) {
		fr[pos] = num;
		if (pos == m) { calc(); return; }
		for (int i = 1; i <= n; i++) dfs(pos + 1, i);
	}
	
	void main() {
		ans = 0;
		dfs(0, 0);
	}
}

namespace Nlessthan4 {
	int tmp[maxm], r;
	void main() {
		ans = 0, r = 0;
		for (int i = 1; i <= n; i++) {
			for (int j = 1; j <= m; j++) {
				tmp[++r] = a[i][j];
			}
		}
		sort(tmp + 1, tmp + r + 1);
		for (int i = r; i >= r - n + 1; i--) ans += tmp[i];
	}
}

namespace Nis4 {
	int opt = 0;
	// 0 为没有限制 
	// 1 为只能放相邻的 
	// 2 为不能放相邻的 
	int cnt = 0;
	struct node {
		int w, lin, col;
		bool operator < (const node &rhs) const {
			return w > rhs.w;
		}
	} s[maxm];
	int used[maxm];
	int cnt_col[maxm];

	void main() {
		ans = 0, opt = 0, cnt = 0;
		memset(used, 0, sizeof used);
		memset(cnt_col, 0, sizeof cnt_col);
		for (int i = 1; i <= n; i++)
			for (int j = 1; j <= m; j++)
				s[(i - 1) * m + j] = (node) {a[i][j], i, j};
		sort(s + 1, s + n * m + 1);
		for (int i = 1; i <= n * m; i++) {
			if (cnt == 4) break;
			if (!used[s[i].col]) { ans += s[i].w; used[s[i].col] = s[i].lin; cnt_col[s[i].col]++; cnt++; }
			else {
				if (opt == 1) {
					if (cnt_col[s[i].col] > 1) {
						cnt_col[s[i].col]++;
						ans += s[i].w;
						cnt++, opt = 0;
						continue;
					}
					else {
						int tem = abs(used[s[i].col] - s[i].lin);
						if (tem == 2) continue;
						else ans += s[i].w, cnt_col[s[i].col]++, cnt++;
					}
				}
				else if (opt == 2) {
					if (cnt_col[s[i].col] > 1) {
						cnt_col[s[i].col]++;
						ans += s[i].w;
						cnt++, opt = 0;
						continue;
					}
					else {
						int tem = abs(used[s[i].col] - s[i].lin);
						if (tem == 2) ans += s[i].w, cnt_col[s[i].col]++, cnt++;
						else continue;
					}
				}
				else {
					if (cnt_col[s[i].col] > 1) {
						ans += s[i].w; cnt++;
						cnt_col[s[i].col]++;
						continue;
					}
					else {
						int tem = abs(used[s[i].col] - s[i].lin);
						if (tem == 2) opt = 2, ans += s[i].w, cnt_col[s[i].col]++, cnt++;
						else opt = 1, ans += s[i].w, cnt_col[s[i].col]++, cnt++;
					}
				}
			}
		}
	}
}

int main() {
	freopen("happygameT1.in", "r", stdin);
	freopen("happygameT1.out", "w", stdout);
	T = read();
	for (; T; T--) {
		n = read(), m = read();
		for (int i = 1; i <= n; i++)
			for (int j = 1; j <= m; j++)
				a[i][j] = read();
		if (n >= m) NmorethanM::main();
		else if (n <= 3) Nlessthan4::main();
		else Nis4::main();
		cout << ans << '\n';
	}
	return 0;
}