結果

問題 No.596 郵便配達
ユーザー 37zigen37zigen
提出日時 2017-06-18 03:59:45
言語 Java21
(openjdk 21)
結果
WA  
実行時間 -
コード長 3,365 bytes
コンパイル時間 2,357 ms
コンパイル使用メモリ 78,792 KB
実行使用メモリ 219,472 KB
最終ジャッジ日時 2024-11-17 14:09:00
合計ジャッジ時間 17,333 ms
ジャッジサーバーID
(参考情報)
judge2 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 WA -
testcase_01 WA -
testcase_02 WA -
testcase_03 AC 111 ms
41,156 KB
testcase_04 WA -
testcase_05 WA -
testcase_06 WA -
testcase_07 WA -
testcase_08 AC 1,697 ms
77,780 KB
testcase_09 WA -
testcase_10 AC 106 ms
40,308 KB
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 AC 110 ms
41,404 KB
testcase_15 WA -
testcase_16 WA -
testcase_17 AC 100 ms
40,332 KB
testcase_18 AC 106 ms
41,212 KB
testcase_19 AC 103 ms
41,328 KB
testcase_20 WA -
testcase_21 WA -
testcase_22 WA -
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
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 solve(int[] l, int[] r) {
		int[] r2 = new int[MAX];
		// rEdge1->rEdge2の時
		int rEdge1 = Integer.MAX_VALUE >> 4;
		int rEdge2 = -(Integer.MAX_VALUE >> 4);
		int lEdge1 = Integer.MAX_VALUE >> 4;
		int lEdge2 = -(Integer.MAX_VALUE >> 4);
		for (int i = 0; i < MAX; ++i) {
			if (r[i] != -1) {
				rEdge1 = i;
				break;
			}
		}
		for (int i = MAX - 1; i >= 0; --i) {
			if (r[i] != -1) {
				rEdge2 = r[i];
				break;
			}
		}
		for (int i = MAX - 1; i >= 0; --i) {
			if (l[i] != -1) {
				lEdge2 = i;
				break;
			}
		}
		for (int i = 0; i < MAX; ++i) {
			if (l[i] != -1) {
				lEdge1 = l[i];
				break;
			}
		}

		int[] pile = new int[MAX];
		for (int i = MAX - 1; i >= 0; --i) {
			if (l[i] != -1) {
				++pile[i];
				if (l[i] > 0) {
					--pile[l[i] - 1];
				}
			}
			if (i + 1 < MAX) {
				pile[i] = pile[i + 1] + pile[i];
			}
		}
		for (int i = 0; i < MAX; ++i) {
			if (pile[i] > 0) {
				pile[i] = 1;
			}
		}
		Arrays.fill(r2, Integer.MAX_VALUE >> 4);
		{
			for (int i = rEdge1; i < MAX; ++i) {
				if (pile[i] == 0 || i + 1 == MAX || pile[i + 1] == 0) {
					r2[i] = 2 * Math.abs(i - Math.min(lEdge1, rEdge1));
				}
				if ((i == 0 || pile[i - 1] == 0) && pile[i] != 0) {
					r2[i] = Math.min(r2[i], (1 + 2 * (pile[i] - 1)) + (i > 0 ? r2[i - 1] : 0));
				} else {
					r2[i] = Math.min(r2[i], (1 + 2 * pile[i]) + (i > 0 ? r2[i - 1] : 0));
				}
			}
		}
		int ans = Integer.MAX_VALUE >> 4;
		for (int i = 0; i < MAX; ++i) {
			if (pile[i] == 0 || i + 1 == MAX || pile[i + 1] == 0) {
				int tmp = r2[i] + 2 * Math.abs(i - Math.max(lEdge2, rEdge2));
				ans = Math.min(ans, tmp);
			}
		}
		return ans;

	}

	void run() throws FileNotFoundException {
		// gen();
		Scanner sc = new Scanner(System.in);
		PrintWriter pw = new PrintWriter(System.out);
		// Scanner sc = new Scanner(in);
		// PrintWriter pw = new PrintWriter(out);
		MAX = sc.nextInt();
		int m = sc.nextInt();
		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 x = sc.nextInt();
			int d = sc.nextInt();
			int ma = x - 1;
			int mi = x + 1;
			for (int j = 0; j < d; ++j) {
				int y = sc.nextInt();
				ma = Math.max(ma, y);
				mi = Math.min(mi, y);
			}
			if (ma >= x) {
				r[x] = ma;
			}
			if (mi <= x) {
				l[x] = mi;
			}
		}

		{
			int ma = -1;
			for (int i = 0; i < MAX; ++i) {
				if (r[i] == -1)
					continue;
				if (ma < 0 || 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 >= MAX || mi > l[i]) {
					mi = l[i];
				} else if (mi <= l[i]) {
					l[i] = -1;
				}
			}
		}

		int[] nl = new int[MAX];
		int[] nr = new int[MAX];
		for (int i = 0; i < MAX; ++i) {
			if (l[i] != -1) {
				int s = MAX - 1 - i;
				int d = MAX - 1 - l[i];
				nr[s] = d;
			}
			if (r[i] != -1) {
				int s = MAX - 1 - i;
				int d = MAX - 1 - r[i];
				nl[s] = d;
			}
		}
		long ans = Math.min(solve(l, r), solve(nl, nr));
		pw.println(ans);
		pw.close();
	}
}
0