結果

問題 No.596 郵便配達
ユーザー 37zigen37zigen
提出日時 2017-08-30 03:06:43
言語 Java21
(openjdk 21)
結果
WA  
実行時間 -
コード長 6,031 bytes
コンパイル時間 2,451 ms
コンパイル使用メモリ 78,296 KB
実行使用メモリ 714,732 KB
最終ジャッジ日時 2024-11-17 14:11:38
合計ジャッジ時間 19,717 ms
ジャッジサーバーID
(参考情報)
judge2 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 612 ms
139,320 KB
testcase_01 WA -
testcase_02 AC 50 ms
36,684 KB
testcase_03 WA -
testcase_04 WA -
testcase_05 MLE -
testcase_06 WA -
testcase_07 AC 50 ms
36,964 KB
testcase_08 AC 805 ms
140,880 KB
testcase_09 WA -
testcase_10 AC 50 ms
36,800 KB
testcase_11 AC 51 ms
36,992 KB
testcase_12 WA -
testcase_13 WA -
testcase_14 AC 50 ms
37,084 KB
testcase_15 AC 52 ms
36,976 KB
testcase_16 AC 51 ms
37,024 KB
testcase_17 AC 48 ms
37,048 KB
testcase_18 AC 49 ms
36,708 KB
testcase_19 AC 51 ms
36,924 KB
testcase_20 MLE -
testcase_21 MLE -
testcase_22 MLE -
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.NoSuchElementException;

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[] 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[] 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);
		int[][] s = new int[MAX][2];
		for (int i = 0; i < s.length; ++i) {
			for (int j = 0; j < s[i].length; ++j) {
				s[i][j] = Integer.MAX_VALUE / 10;
			}
		}
		{

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

	void run() throws FileNotFoundException {
		Scanner sc = new Scanner();
		PrintWriter pw = new PrintWriter(System.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 < 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;
				}
			}
		}

		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 s = (MAX - 1) - i;
				int d = (MAX - 1) - l[i];
				invr[s] = d;
			}
			if (r[i] != -1) {
				int s = (MAX - 1) - i;
				int d = (MAX - 1) - r[i];
				invl[s] = d;
			}
		}
		long ans = Math.min(solve(l, r), solve(invl, invr));
		pw.println(ans);
		pw.close();
	}

	class Scanner {
		private final InputStream in = System.in;
		private final byte[] buffer = new byte[1024];
		private int ptr = 0;
		private int buflen = 0;

		private boolean hasNextByte() {
			if (ptr < buflen) {
				return true;
			} else {
				ptr = 0;
				try {
					buflen = in.read(buffer);
				} catch (IOException e) {
					e.printStackTrace();
				}
				if (buflen <= 0) {
					return false;
				}
			}
			return true;
		}

		private int readByte() {
			if (hasNextByte())
				return buffer[ptr++];
			else
				return -1;
		}

		private boolean isPrintableChar(int c) {
			return 33 <= c && c <= 126;
		}

		private void skipUnprintable() {
			while (hasNextByte() && !isPrintableChar(buffer[ptr]))
				ptr++;
		}

		public boolean hasNext() {
			skipUnprintable();
			return hasNextByte();
		}

		public String next() {
			if (!hasNext())
				throw new NoSuchElementException();
			StringBuilder sb = new StringBuilder();
			int b = readByte();
			while (isPrintableChar(b)) {
				sb.appendCodePoint(b);
				b = readByte();
			}
			return sb.toString();
		}

		public long nextLong() {
			if (!hasNext())
				throw new NoSuchElementException();
			long n = 0;
			boolean minus = false;
			int b = readByte();
			if (b == '-') {
				minus = true;
				b = readByte();
			}
			if (b < '0' || '9' < b) {
				throw new NumberFormatException();
			}
			while (true) {
				if ('0' <= b && b <= '9') {
					n *= 10;
					n += b - '0';
				} else if (b == -1 || !isPrintableChar(b)) {
					return minus ? -n : n;
				} else {
					throw new NumberFormatException();
				}
				b = readByte();
			}
		}

		public int nextInt() {
			return (int) nextLong();
		}
	}

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