結果

問題 No.704 ゴミ拾い Medium
ユーザー 37zigen37zigen
提出日時 2018-06-16 18:46:02
言語 Java21
(openjdk 21)
結果
AC  
実行時間 386 ms / 1,500 ms
コード長 3,627 bytes
コンパイル時間 2,455 ms
コンパイル使用メモリ 78,316 KB
実行使用メモリ 62,632 KB
最終ジャッジ日時 2024-06-30 16:24:13
合計ジャッジ時間 14,273 ms
ジャッジサーバーID
(参考情報)
judge4 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 64 ms
54,444 KB
testcase_01 AC 64 ms
54,444 KB
testcase_02 AC 66 ms
54,376 KB
testcase_03 AC 66 ms
54,164 KB
testcase_04 AC 64 ms
54,400 KB
testcase_05 AC 64 ms
54,216 KB
testcase_06 AC 64 ms
54,580 KB
testcase_07 AC 65 ms
54,376 KB
testcase_08 AC 64 ms
54,512 KB
testcase_09 AC 64 ms
54,312 KB
testcase_10 AC 66 ms
54,164 KB
testcase_11 AC 65 ms
54,380 KB
testcase_12 AC 65 ms
54,344 KB
testcase_13 AC 63 ms
54,200 KB
testcase_14 AC 90 ms
55,340 KB
testcase_15 AC 90 ms
55,152 KB
testcase_16 AC 87 ms
55,412 KB
testcase_17 AC 88 ms
55,224 KB
testcase_18 AC 89 ms
55,368 KB
testcase_19 AC 80 ms
55,468 KB
testcase_20 AC 78 ms
55,268 KB
testcase_21 AC 81 ms
55,216 KB
testcase_22 AC 90 ms
55,560 KB
testcase_23 AC 89 ms
55,480 KB
testcase_24 AC 361 ms
62,416 KB
testcase_25 AC 355 ms
61,440 KB
testcase_26 AC 370 ms
61,236 KB
testcase_27 AC 367 ms
61,360 KB
testcase_28 AC 358 ms
62,572 KB
testcase_29 AC 362 ms
62,528 KB
testcase_30 AC 366 ms
61,424 KB
testcase_31 AC 371 ms
61,612 KB
testcase_32 AC 365 ms
61,568 KB
testcase_33 AC 386 ms
62,408 KB
testcase_34 AC 322 ms
61,328 KB
testcase_35 AC 322 ms
61,568 KB
testcase_36 AC 324 ms
61,272 KB
testcase_37 AC 328 ms
61,624 KB
testcase_38 AC 326 ms
61,528 KB
testcase_39 AC 319 ms
61,200 KB
testcase_40 AC 323 ms
61,432 KB
testcase_41 AC 325 ms
61,296 KB
testcase_42 AC 328 ms
61,320 KB
testcase_43 AC 321 ms
61,460 KB
testcase_44 AC 63 ms
54,284 KB
testcase_45 AC 64 ms
54,464 KB
testcase_46 AC 328 ms
61,072 KB
testcase_47 AC 324 ms
62,632 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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) {
		new Main().run();
	}

	class SegTree {
		int n;
		long[] val;

		public SegTree(int n_) {
			n = 1;
			while (n < n_)
				n *= 2;
			val = new long[2 * n - 1];
			Arrays.fill(val, Long.MAX_VALUE / 3);
		}

		void setVal(int k, long v) {
			k += n - 1;
			val[k] = Math.min(val[k], v);
			while (k > 0) {
				k = (k - 1) / 2;
				val[k] = Math.min(val[2 * k + 1], val[2 * k + 2]);
			}
		}

		void addVal(int k, long v) {
			k += n - 1;
			val[k] += v;
			while (k > 0) {
				k = (k - 1) / 2;
				val[k] = Math.min(val[2 * k + 1], val[2 * k + 2]);
			}
		}

		long query(int a, int b, int l, int r, int k) {
			if (a <= l && r <= b) {
				return val[k];
			} else if (r <= a || b <= l) {
				return Long.MAX_VALUE / 3;
			} else {
				return Math.min(query(a, b, l, (l + r) / 2, 2 * k + 1), query(a, b, (l + r) / 2, r, 2 * k + 2));
			}
		}

		long query(int a, int b) {
			return query(a, b, 0, n, 0);
		}
	}

	public void run() {
		Scanner sc = new Scanner();
		int n = sc.nextInt();
		int[] a = new int[n];
		int[] x = new int[n];
		int[] y = new int[n];
		for (int i = 0; i < n; ++i)
			a[i] = sc.nextInt();
		for (int i = 0; i < n; ++i)
			x[i] = sc.nextInt();
		for (int i = 0; i < n; ++i)
			y[i] = sc.nextInt();
		long[] d = new long[n];
		Arrays.fill(d, Long.MAX_VALUE / 3);
		SegTree plus = new SegTree((int) 1e5 + 1);
		SegTree minus = new SegTree((int) 1e5 + 1);
		for (int i = 0; i < n; ++i) {
			plus.setVal(x[i], (i > 0 ? d[i - 1] : 0) - x[i] + y[i]);
			minus.setVal(x[i], (i > 0 ? d[i - 1] : 0) + x[i] + y[i]);
			d[i] = Math.min(d[i], plus.query(0, a[i] + 1) + a[i]);
			d[i] = Math.min(d[i], minus.query(a[i], (int) 1e5 + 1) - a[i]);
		}
		System.out.println(d[n - 1]);
	}

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

	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();
			}
		}

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

}
0