結果

問題 No.359 門松行列
ユーザー pekempeypekempey
提出日時 2016-04-18 00:15:59
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 217 ms / 2,000 ms
コード長 2,083 bytes
コンパイル時間 1,527 ms
コンパイル使用メモリ 152,268 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-07-27 15:03:55
合計ジャッジ時間 3,378 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 9 ms
4,380 KB
testcase_04 AC 4 ms
4,376 KB
testcase_05 AC 8 ms
4,380 KB
testcase_06 AC 3 ms
4,376 KB
testcase_07 AC 11 ms
4,376 KB
testcase_08 AC 139 ms
4,380 KB
testcase_09 AC 189 ms
4,376 KB
testcase_10 AC 217 ms
4,376 KB
testcase_11 AC 167 ms
4,380 KB
testcase_12 AC 166 ms
4,380 KB
testcase_13 AC 102 ms
4,380 KB
testcase_14 AC 98 ms
4,376 KB
testcase_15 AC 23 ms
4,376 KB
testcase_16 AC 161 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;

bool isKadomatsu(long long x, long long y, long long z) {
	if (x == y || y == z || x == z) return false;
	if (x > y && y < z) return true;
	if (x < y && y > z) return true;
	return false;
}

int a[3][3];

bool check() {
	for (int i = 0; i < 3; i++) {
		for (int j = 0; j < 3; j++) {
			if (a[i][j] == 0) return false;
		}
	}
	for (int i = 0; i < 3; i++) {
		if (!isKadomatsu(a[i][0], a[i][1], a[i][2])) return false;
		if (!isKadomatsu(a[0][i], a[1][i], a[2][i])) return false;
	}
	if (!isKadomatsu(a[0][0], a[1][1], a[2][2])) return false;
	if (!isKadomatsu(a[2][0], a[1][1], a[0][2])) return false;
	return true;
}

void solve() {
	int L;
	cin >> L;

	int y1 = -1, x1 = -1;
	int y2 = -1, x2 = -1;

	vector<int> b;
	for (int i = 0; i < 3; i++) {
		for (int j = 0; j < 3; j++) {
			cin >> a[i][j];
			if (a[i][j] == 0) {
				if (y1 == -1) {
					y1 = i;
					x1 = j;
				} else {
					y2 = i;
					x2 = j;
				}
			} else {
				b.push_back(a[i][j]);
			}
		}
	}
	sort(b.begin(), b.end());

	vector<int> events;
	events.push_back(L);

	const int D = 1000;

	for (int j = 1; j <= D; j++) {
		if (0 < j && j < L) {
			events.push_back(j);
		}
	}

	for (int j = L - D; j <= L - 1; j++) {
		if (0 < j && j < L) {
			events.push_back(j);
		}
	}

	for (int j = L / 2 - D; j <= L / 2 + D; j++) { 
		if (0 < j && j < L) {
			events.push_back(j);
		}
	}

	for (int i = 0; i < b.size(); i++) {
		for (int j = b[i] - D; j <= b[i] + D; j++) {
			if (0 < j && j < L) {
				events.push_back(j);
			}
		}
	}
	for (int i = 0; i < b.size(); i++) {
		for (int j = L - b[i] - D; j <= L - b[i] + D; j++) {
			if (0 < j && j < L) {
				events.push_back(j);
			}
		}
	}
	sort(events.begin(), events.end());
	events.erase(unique(events.begin(), events.end()), events.end());

	int ans = 0;

	for (int i = 0; i < events.size() - 1; i++) {
		int x = events[i];
		int y = L - x;
		a[y1][x1] = x;
		a[y2][x2] = y;
		int len = events[i + 1] - events[i];
		if (check()) ans += len;
	}

	cout << ans << endl;
}

int main() {
	int T;
	cin >> T;
	while (T--) solve();
}
0