結果

問題 No.359 門松行列
ユーザー pekempeypekempey
提出日時 2016-04-18 00:13:36
言語 C++11
(gcc 11.4.0)
結果
TLE  
実行時間 -
コード長 1,901 bytes
コンパイル時間 1,524 ms
コンパイル使用メモリ 165,760 KB
実行使用メモリ 38,020 KB
最終ジャッジ日時 2024-04-15 02:37:49
合計ジャッジ時間 7,356 ms
ジャッジサーバーID
(参考情報)
judge2 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 6 ms
13,752 KB
testcase_01 AC 6 ms
6,940 KB
testcase_02 AC 22 ms
6,940 KB
testcase_03 AC 403 ms
6,944 KB
testcase_04 AC 157 ms
6,944 KB
testcase_05 AC 464 ms
6,940 KB
testcase_06 AC 95 ms
6,940 KB
testcase_07 AC 330 ms
6,944 KB
testcase_08 AC 1,036 ms
6,944 KB
testcase_09 TLE -
testcase_10 -- -
testcase_11 -- -
testcase_12 -- -
testcase_13 -- -
testcase_14 -- -
testcase_15 -- -
testcase_16 -- -
権限があれば一括ダウンロードができます

ソースコード

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 = 100000;

	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