結果

問題 No.608 God's Maze
ユーザー 37zigen37zigen
提出日時 2017-12-23 09:23:52
言語 Java21
(openjdk 21)
結果
TLE  
実行時間 -
コード長 1,716 bytes
コンパイル時間 2,238 ms
コンパイル使用メモリ 76,840 KB
実行使用メモリ 72,860 KB
最終ジャッジ日時 2023-08-22 16:21:50
合計ジャッジ時間 10,096 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 117 ms
58,112 KB
testcase_01 AC 117 ms
56,404 KB
testcase_02 AC 116 ms
55,992 KB
testcase_03 AC 118 ms
55,776 KB
testcase_04 AC 117 ms
55,532 KB
testcase_05 AC 117 ms
56,152 KB
testcase_06 AC 119 ms
56,248 KB
testcase_07 AC 116 ms
55,748 KB
testcase_08 AC 119 ms
58,292 KB
testcase_09 AC 118 ms
55,892 KB
testcase_10 AC 121 ms
55,812 KB
testcase_11 AC 118 ms
56,000 KB
testcase_12 AC 118 ms
56,288 KB
testcase_13 AC 118 ms
56,304 KB
testcase_14 AC 119 ms
55,760 KB
testcase_15 AC 118 ms
56,052 KB
testcase_16 AC 119 ms
55,760 KB
testcase_17 AC 119 ms
56,228 KB
testcase_18 AC 117 ms
55,720 KB
testcase_19 AC 118 ms
56,156 KB
testcase_20 AC 118 ms
55,772 KB
testcase_21 AC 121 ms
55,828 KB
testcase_22 AC 119 ms
56,340 KB
testcase_23 AC 118 ms
56,176 KB
testcase_24 AC 119 ms
55,880 KB
testcase_25 AC 119 ms
56,124 KB
testcase_26 AC 119 ms
56,228 KB
testcase_27 AC 119 ms
56,032 KB
testcase_28 AC 120 ms
55,864 KB
testcase_29 TLE -
testcase_30 -- -
testcase_31 -- -
testcase_32 -- -
testcase_33 -- -
testcase_34 -- -
testcase_35 -- -
testcase_36 -- -
testcase_37 -- -
testcase_38 -- -
testcase_39 -- -
testcase_40 -- -
testcase_41 -- -
testcase_42 -- -
testcase_43 -- -
testcase_44 -- -
testcase_45 -- -
testcase_46 -- -
testcase_47 -- -
testcase_48 -- -
testcase_49 -- -
testcase_50 -- -
testcase_51 -- -
testcase_52 -- -
testcase_53 -- -
testcase_54 -- -
testcase_55 -- -
testcase_56 -- -
testcase_57 -- -
testcase_58 -- -
testcase_59 -- -
testcase_60 -- -
testcase_61 -- -
testcase_62 -- -
testcase_63 -- -
testcase_64 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.ArrayDeque;
import java.util.Arrays;
import java.util.Scanner;

class Main {

	void run() {
		Scanner sc = new Scanner(System.in);
		int n = sc.nextInt();
		String S = sc.next();
		boolean[] a = new boolean[S.length()];
		for (int i = 0; i < S.length(); ++i) {
			a[i] = S.charAt(i) == '.';
		}
		int ans = solve(Arrays.copyOf(a, a.length), n);
		{
			int s = 0;
			int t = a.length - 1;
			while (s < t) {
				boolean tmp = a[s];
				a[s] = a[t];
				a[t] = tmp;
				++s;
				--t;
			}
			n = S.length() - n + 1;
		}
		ans = Math.min(ans, solve(a, n));
		System.out.println(ans);
	}
	ArrayDeque<Integer> dq = new ArrayDeque<>();
	
	int solve(boolean[] b, int n) {
		int ret = Integer.MAX_VALUE / 3;
		int t = b.length - 1;
		int s = 0;
		while (b[t])
			--t;
		while (b[s])
			++s;
		if (t < n - 1)
			return ret;
		for (int j = t; j >= 0; --j) {
			int cur = 0;
			boolean[] a = Arrays.copyOf(b, b.length);
			for (int i = j; i < t; ++i) {
				a[i] = !a[i];
				++cur;
			}
			for (int i = 0; i < a.length; ++i) {
				//s->t
				if (s < i && i <= t) {
					a[i] = !a[i];
					++cur;
				}
				if ((s <= i && i < n - 1) || (n - 1 < i && i <= s)) {
					a[i] = !a[i];
					++cur;
				}
			}
			for (int i = 0; i < a.length; ++i) {
				if (!a[i])
					dq.addLast(i);
			}
			while (!dq.isEmpty()) {
				if (dq.size() >= 2) {
					int u = dq.pollFirst();
					int v = dq.pollFirst();
					cur += (v - u) * 2;
				} else {
					cur = Integer.MAX_VALUE / 3;
					dq.clear();
					break;
				}
			}
			ret = Math.min(ret, cur);
		}
		return ret;
	}

	void tr(Object... objects) {
		System.out.println(Arrays.deepToString(objects));
	}

	public static void main(String[] args) {
		new Main().run();
	}

}
0