結果
問題 | No.596 郵便配達 |
ユーザー | 37zigen |
提出日時 | 2017-10-14 02:42:40 |
言語 | Java21 (openjdk 21) |
結果 |
AC
|
実行時間 | 1,462 ms / 5,000 ms |
コード長 | 6,943 bytes |
コンパイル時間 | 2,568 ms |
コンパイル使用メモリ | 79,936 KB |
実行使用メモリ | 403,724 KB |
最終ジャッジ日時 | 2024-11-17 14:22:07 |
合計ジャッジ時間 | 13,822 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge4 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 445 ms
93,048 KB |
testcase_01 | AC | 434 ms
93,008 KB |
testcase_02 | AC | 56 ms
50,048 KB |
testcase_03 | AC | 57 ms
49,952 KB |
testcase_04 | AC | 962 ms
172,900 KB |
testcase_05 | AC | 1,284 ms
332,612 KB |
testcase_06 | AC | 415 ms
70,348 KB |
testcase_07 | AC | 55 ms
50,144 KB |
testcase_08 | AC | 846 ms
113,672 KB |
testcase_09 | AC | 56 ms
49,952 KB |
testcase_10 | AC | 56 ms
49,632 KB |
testcase_11 | AC | 55 ms
49,732 KB |
testcase_12 | AC | 56 ms
49,688 KB |
testcase_13 | AC | 56 ms
50,180 KB |
testcase_14 | AC | 56 ms
50,044 KB |
testcase_15 | AC | 57 ms
50,156 KB |
testcase_16 | AC | 57 ms
49,792 KB |
testcase_17 | AC | 57 ms
49,972 KB |
testcase_18 | AC | 57 ms
49,896 KB |
testcase_19 | AC | 55 ms
49,960 KB |
testcase_20 | AC | 1,462 ms
403,724 KB |
testcase_21 | AC | 1,374 ms
403,464 KB |
testcase_22 | AC | 1,367 ms
403,556 KB |
ソースコード
import java.io.FileNotFoundException; import java.io.IOException; import java.io.InputStream; import java.util.Arrays; import java.util.NoSuchElementException; public class Main { public static void main(String[] args) throws FileNotFoundException { new Main().run(); } void run() { Scanner sc = new Scanner(); 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 MAX = 0; int INF = Integer.MAX_VALUE / 10; int solve(int[] l, int[] r) { int r1 = INF; int r2 = -INF; int l1 = INF; int l2 = -INF; // r1 -> r[r1]が最左 // l1 <- (arg l=l1)が最左 for (int i = 0; i < MAX; ++i) { if (r[i] != -1 && r1 == INF) { r1 = i; } if (l[i] != -1 && l1 == INF) { l1 = l[i]; } } for (int i = MAX - 1; i >= 0; --i) { if (r[i] != -1 && r2 == -INF) { r2 = r[i]; } if (l[i] != -1 && l2 == -INF) { l2 = i; } } for (int i = Math.min(l1, r1) + 1; i < Math.max(l2, r2); ++i) { if (l[i] == i) { l[i] = -1; } if (r[i] == i) { r[i] = -1; } } int[] arg_l = new int[MAX]; Arrays.fill(arg_l, -1); for (int i = 0; i < l.length; ++i) { if (l[i] != -1) { arg_l[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]; --middle[l[i]]; } 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(r1, l1); i <= Math.max(r2, l2); ++i) { if (r1 <= i && i < l1) { s[0][i] = i - r1; } if (l[i] == i || (middle[i] == 0 && l[i] == -1 && arg_l[i] == -1)) {// 何もなし s[0][i] = Math.min(s[0][i], 2 * Math.abs(i - Math.min(l1, r1))); 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 (l[i] != -1 && l[i] != i && arg_l[i] != -1 && arg_l[i] != i) {// 配達元かつ配達先 s[1][i] = Math.min(s[1][i], s[1][i - 1] + 3); } else if (middle[i] > 0) {// 配達途中 s[1][i] = Math.min(s[1][i], s[1][i - 1] + 3); } else if (arg_l[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); s[0][i] = Math.min(s[0][i], 2 * Math.abs(i - Math.min(l1, r1))); } else { if (i == 0) { } else throw new AssertionError(); } } int ans = 2 * (Math.max(l2, r2) - Math.min(l1, r1)); for (int i = 0; i < MAX; ++i) { ans = Math.min(ans, s[0][i] + 2 * Math.abs(i - Math.max(l2, r2)) - (i == Math.max(l2, r2) ? 0 : 1)); } return ans; } int solver(int MAX_, int m, int[] x, int[] d, int[][] y) { ++MAX_; 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.max(0, 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 }; } 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)); } }