結果

問題 No.596 郵便配達
ユーザー 37zigen37zigen
提出日時 2017-09-28 22:56:26
言語 Java21
(openjdk 21)
結果
WA  
実行時間 -
コード長 5,053 bytes
コンパイル時間 1,954 ms
コンパイル使用メモリ 80,000 KB
実行使用メモリ 419,356 KB
最終ジャッジ日時 2024-11-17 14:18:32
合計ジャッジ時間 20,856 ms
ジャッジサーバーID
(参考情報)
judge5 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 379 ms
95,404 KB
testcase_01 AC 467 ms
94,892 KB
testcase_02 AC 107 ms
54,092 KB
testcase_03 WA -
testcase_04 WA -
testcase_05 WA -
testcase_06 WA -
testcase_07 AC 98 ms
53,140 KB
testcase_08 AC 2,112 ms
123,160 KB
testcase_09 AC 110 ms
54,044 KB
testcase_10 AC 98 ms
52,780 KB
testcase_11 AC 107 ms
54,232 KB
testcase_12 AC 96 ms
53,196 KB
testcase_13 AC 96 ms
53,116 KB
testcase_14 AC 106 ms
54,120 KB
testcase_15 AC 106 ms
53,928 KB
testcase_16 AC 105 ms
54,008 KB
testcase_17 AC 109 ms
54,156 KB
testcase_18 AC 96 ms
53,108 KB
testcase_19 AC 98 ms
52,908 KB
testcase_20 AC 2,426 ms
419,280 KB
testcase_21 AC 2,506 ms
419,356 KB
testcase_22 AC 2,669 ms
419,328 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.io.FileNotFoundException;
import java.util.Arrays;
import java.util.Scanner;

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

	int MAX = 0;
	int INF = Integer.MAX_VALUE / 10;

	int solve(int[] l, int[] r) {
		int rEdge1 = INF;
		int rEdge2 = -INF;
		int lEdge1 = INF;
		int lEdge2 = -INF;
		for (int i = 0; i < MAX; ++i) {
			if (r[i] != -1 && rEdge1 == INF) {
				rEdge1 = i;
			}
			if (l[i] != -1 && lEdge1 == INF) {
				lEdge1 = l[i];
			}
		}
		for (int i = MAX - 1; i >= 0; --i) {
			if (r[i] != -1 && rEdge2 == -INF) {
				rEdge2 = r[i];
			}
			if (l[i] != -1 && lEdge2 == -INF) {
				lEdge2 = i;
			}
		}
		for (int i = Math.min(lEdge1, rEdge1) + 1; i < Math.max(lEdge2, rEdge2); ++i) {
			if (l[i] == i) {
				l[i] = -1;
			}
			if (r[i] == i) {
				r[i] = -1;
			}
		}

		int[] from = new int[MAX];
		Arrays.fill(from, -1);
		for (int i = 0; i < l.length; ++i) {
			if (l[i] != -1) {
				from[l[i]] = i;
			}
		}
		int[] middle = new int[MAX];
		for (int i = MAX - 1; i >= 0; --i) {
			if (l[i] != i && l[i] != -1) {
				++middle[i - 1];
				if (l[i] > 0) {
					--middle[l[i] - 1];
				}
			}
			if (i + 1 < MAX) {
				middle[i] = middle[i + 1] + middle[i];
			}
		}
		int[][] s = new int[2][MAX];
		for (int i = 0; i < s.length; ++i) {
			for (int j = 0; j < s[i].length; ++j) {
				s[i][j] = INF;
			}
		}

		for (int i = Math.min(rEdge1, lEdge1); i <= Math.max(rEdge2, lEdge2); ++i) {
			if (rEdge1 <= i && i < lEdge1) {
				s[0][i] = i - rEdge1;
			}
			if (i < lEdge1 || (l[i] != -1 && middle[i] == 0)) {
				s[0][i] = Math.min(s[0][i], 2 * Math.abs(i - Math.min(lEdge1, rEdge1)));
			}
			if (l[i] == i || (middle[i] == 0 && l[i] == -1 && from[i] == -1)) {
				if (i == 0) {
					s[1][i] = 0;
					s[0][i] = 0;
				} else {
					s[1][i] = Math.min(s[1][i], Math.min(s[1][i - 1] + 3, s[0][i - 1] + 1));
					s[0][i] = Math.min(s[0][i], Math.min(s[1][i - 1] + 3, s[0][i - 1] + 1));
				}
			} else if (from[i] != -1 && i != 0) {
				s[1][i] = Math.min(s[1][i], Math.min(s[0][i - 1] + 1, s[1][i - 1] + 3));
			} else if (l[i] != -1) {
				s[1][i] = Math.min(s[1][i], s[1][i - 1] + 3);
				s[0][i] = Math.min(s[0][i], s[1][i - 1] + 3);
			} else if ((middle[i] > 0 || from[i] != -1 || l[i] != -1) && i != 0) {
				s[1][i] = Math.min(s[1][i], s[1][i - 1] + 3);
			}
		}
		
		int ans = 2 * (Math.max(lEdge2, rEdge2) - Math.min(lEdge1, rEdge1));
		for (int i = 0; i < MAX; ++i) {
			ans = Math.min(ans,
					s[0][i] + 2 * Math.abs(i - Math.max(lEdge2, rEdge2)) - (i == Math.max(lEdge2, rEdge2) ? 0 : 1));
		}
		return ans;
	}

	void run() {
		Scanner sc = new Scanner(System.in);
		int MAX_ = sc.nextInt();
		if (!(1 <= MAX_ && MAX_ <= 1e7)) {
			throw new AssertionError();
		}
		int m = sc.nextInt();
		if (!(1 <= m && m <= 5 * 1e5)) {
			throw new AssertionError();
		}
		int[] x = new int[m];
		int[] d = new int[m];
		int[][] y = new int[m][];
		int sumd = 0;
		for (int i = 0; i < m; ++i) {
			x[i] = sc.nextInt();
			d[i] = sc.nextInt();
			if (!(d[i] > 0)) {
				throw new AssertionError();
			}
			if (!(0 <= x[i] && x[i] < MAX_))
				throw new AssertionError();
			sumd += d[i];
			y[i] = new int[d[i]];
			for (int j = 0; j < d[i]; ++j) {
				y[i][j] = sc.nextInt();
				if (!(0 <= y[i][j] && y[i][j] < MAX_))
					throw new AssertionError();
			}
		}
		if (!(1 <= sumd && sumd <= 1e6)) {
			throw new AssertionError();
		}
		System.out.println(solver(MAX_, m, x, d, y));
	}

	int solver(int MAX_, int m, int[] x, int[] d, int[][] y) {
		MAX = MAX_;
		int[] r = lr(MAX_, m, x, d, y)[1];
		int[] l = lr(MAX_, m, x, d, y)[0];
		int[] invl = new int[MAX];
		int[] invr = new int[MAX];
		Arrays.fill(invl, -1);
		Arrays.fill(invr, -1);
		for (int i = 0; i < MAX; ++i) {
			if (l[i] != -1) {
				int src = (MAX - 1) - i;
				int dst = (MAX - 1) - l[i];
				invr[src] = dst;
			}
			if (r[i] != -1) {
				int src = (MAX - 1) - i;
				int dst = (MAX - 1) - r[i];
				invl[src] = dst;
			}
		}
		return Math.min(solve(l, r), solve(invl, invr));
	}

	int[][] lr(int MAX_, int m, int[] x, int[] d, int[][] y) {
		MAX = MAX_;
		int[] r = new int[MAX];
		int[] l = new int[MAX];

		Arrays.fill(r, -1);
		Arrays.fill(l, -1);
		for (int i = 0; i < m; ++i) {
			int ma = r[x[i]] != -1 ? r[x[i]] : x[i] - 1;
			int mi = l[x[i]] != -1 ? l[x[i]] : x[i] + 1;
			for (int j = 0; j < d[i]; ++j) {
				ma = Math.max(ma, y[i][j]);
				mi = Math.min(mi, y[i][j]);
			}
			if (ma >= x[i]) {
				r[x[i]] = ma;
			}
			if (mi <= x[i]) {
				l[x[i]] = mi;
			}
		}
		{
			int ma = -1;
			for (int i = 0; i < MAX; ++i) {
				if (r[i] == -1)
					continue;
				if (ma < r[i]) {
					ma = r[i];
				} else if (ma >= r[i]) {
					r[i] = -1;
				}
			}
		}
		{
			int mi = MAX * 10;
			for (int i = MAX - 1; i >= 0; --i) {
				if (l[i] == -1)
					continue;
				if (mi > l[i]) {
					mi = l[i];
				} else if (mi <= l[i]) {
					l[i] = -1;
				}
			}
		}
		return new int[][] { l, r };
	}


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