比赛 20191022轻松模拟测试 评测结果 TTTTTTTTTT
题目名称 原谅 最终得分 0
用户昵称 氢氦 运行时间 30.000 s
代码语言 C++ 内存使用 13.66 MiB
提交时间 2019-10-22 17:41:02
显示代码纯文本
#include <bits/stdc++.h>

using namespace std;

inline int read() {
	int x = 0, f = 1; char ch = getchar();
	while (ch < '0' || ch > '9') {if (ch == '-') f = -1; ch = getchar();}
	while (ch >= '0' && ch <= '9') {x = x * 10 - 48 + ch; ch = getchar();}
	return x * f;
}

const int maxn = 105;
int t;
int n, m;
priority_queue<int>q;

struct P {
	int num, x, y;
}p[7];

void change(int a,int b) {
	p[a].num = p[b].num;
	p[a].x = p[b].x;
	p[a].y = p[b].y;
}

bool check() {
	int a1 = p[1].y, a2 = p[2].y, a3 = p[3].y, a4 = p[4].y;
	if ((a1 != a2 && a1 != a3 && a2 != a3) || (a1 != a2 && a1 != a4 && a2 != a4) || (a1 != a3 && a1 != a4 && a3 != a4) || (a2 != a3 && a2 != a4 && a3 != a4))return 1;
	int b1 = p[1].x, b2 = p[2].x, b3 = p[3].x, b4 = p[4].x;
	if (b1 == b2 && b1 == b3 && b1 == b4) return 1;
	if (abs(b1 - b2) == 1 && abs(b3 - b4) == 1) return 1;
	if (abs(b1 - b3) == 1 && abs(b2 - b4) == 1) return 1;
	if (abs(b1 - b4) == 1 && abs(b2 - b3) == 1) return 1;
	if ((b1 == b2 && b3 == b4) || (b1 == b3 && b2 == b4) || (b1 == b4 && b3 == b2)) return 1;
	return 0;
}
int main() {
	freopen("happygameT1.in","r",stdin);
	freopen("happygameT1.out","w",stdout);
	t = read();
	while(t--) {
		while (!q.empty()) q.pop();
		n = read(), m = read();
		if (n < 4) {
			int x, sum = 0;
			for(int i = 1; i <= n; i++)
				for(int j = 1; j <= m; j++)
					x = read(), q.push(x);	
			for(int i = 1; i <= n; i++)
				sum += q.top(), q.pop();
			cout << sum << '\n';
			continue;
		}
		else {
			memset(p, 0, sizeof(p));
			int x;
			for(int i = 1; i <= n; i++)
				for(int j = 1; j <= m; j++){
					x = read();
					if (x > p[1].num) {
						change(5,4); change(4,3);
						change(3,2); change(2,1);
						p[1].num = x; p[1].x = i; p[1].y = j;
					}
					else if (x > p[2].num) {
						change(5,4); change(4,3); change(3,2);
						p[2].num = x; p[2].x = i; p[2].y = j;
					}
					else if (x > p[3].num) {
						change(5,4); change(4,3);
						p[3].num = x; p[3].x = i; p[3].y = j;
					}
					else if (x > p[4].num) {
						change(5,4);
						p[4].num = x; p[4].x = i; p[4].y = j;
					}
					else if(x>p[5].num) {
						p[5].num = x; p[5].x = i; p[5].y = j;
					} 
				}
			if (check())
				cout << p[1].num + p[2].num + p[3].num + p[4].num << '\n';
			else 
				cout << p[1].num + p[2].num + p[3].num + p[5].num << '\n';
		}
	}
	return 0;
}