結果

問題 No.703 ゴミ拾い Easy
ユーザー 37zigen37zigen
提出日時 2018-06-16 08:10:59
言語 Java21
(openjdk 21)
結果
AC  
実行時間 307 ms / 1,500 ms
コード長 3,293 bytes
コンパイル時間 3,215 ms
コンパイル使用メモリ 74,844 KB
実行使用メモリ 70,436 KB
最終ジャッジ日時 2023-08-30 18:57:52
合計ジャッジ時間 12,219 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 41 ms
48,908 KB
testcase_01 AC 42 ms
48,992 KB
testcase_02 AC 43 ms
48,824 KB
testcase_03 AC 40 ms
48,908 KB
testcase_04 AC 40 ms
48,800 KB
testcase_05 AC 42 ms
48,884 KB
testcase_06 AC 41 ms
48,872 KB
testcase_07 AC 40 ms
48,760 KB
testcase_08 AC 40 ms
48,880 KB
testcase_09 AC 39 ms
48,904 KB
testcase_10 AC 38 ms
48,744 KB
testcase_11 AC 39 ms
48,972 KB
testcase_12 AC 39 ms
48,816 KB
testcase_13 AC 41 ms
48,940 KB
testcase_14 AC 49 ms
48,964 KB
testcase_15 AC 49 ms
48,980 KB
testcase_16 AC 48 ms
47,288 KB
testcase_17 AC 48 ms
48,884 KB
testcase_18 AC 48 ms
49,004 KB
testcase_19 AC 48 ms
48,964 KB
testcase_20 AC 48 ms
48,896 KB
testcase_21 AC 48 ms
49,264 KB
testcase_22 AC 48 ms
49,276 KB
testcase_23 AC 48 ms
48,964 KB
testcase_24 AC 245 ms
69,612 KB
testcase_25 AC 237 ms
68,412 KB
testcase_26 AC 292 ms
70,268 KB
testcase_27 AC 258 ms
69,900 KB
testcase_28 AC 257 ms
69,324 KB
testcase_29 AC 260 ms
68,212 KB
testcase_30 AC 208 ms
68,316 KB
testcase_31 AC 220 ms
68,448 KB
testcase_32 AC 231 ms
68,684 KB
testcase_33 AC 231 ms
69,588 KB
testcase_34 AC 224 ms
69,476 KB
testcase_35 AC 271 ms
70,100 KB
testcase_36 AC 238 ms
69,508 KB
testcase_37 AC 250 ms
69,544 KB
testcase_38 AC 213 ms
69,120 KB
testcase_39 AC 252 ms
70,220 KB
testcase_40 AC 293 ms
70,168 KB
testcase_41 AC 302 ms
69,884 KB
testcase_42 AC 257 ms
70,436 KB
testcase_43 AC 304 ms
69,928 KB
testcase_44 AC 41 ms
48,964 KB
testcase_45 AC 40 ms
48,820 KB
testcase_46 AC 231 ms
69,104 KB
testcase_47 AC 307 ms
69,804 KB
testcase_48 AC 49 ms
48,932 KB
testcase_49 AC 48 ms
48,920 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayDeque;
import java.util.Arrays;
import java.util.NoSuchElementException;

public class Main {

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

	public void run() {
		Scanner sc = new Scanner();
		int n = sc.nextInt();
		long[] a = new long[n];
		long[] x = new long[n];
		long[] y = new long[n];
		for (int i = 0; i < n; ++i) {
			a[i] = sc.nextLong();
		}
		for (int i = 0; i < n; ++i) {
			x[i] = sc.nextLong();
		}
		for (int i = 0; i < n; ++i) {
			y[i] = sc.nextLong();
		}
		long[] d = new long[n];
		Arrays.fill(d, Long.MAX_VALUE / 3);
		ArrayDeque<long[]> dq = new ArrayDeque<>();
		for (int i = 0; i < n; ++i) {
			dq.addLast(new long[] { -2 * x[i], (i == 0 ? 0 : d[i - 1]) + x[i] * x[i] + y[i] * y[i] });
			while (dq.size() >= 3) {
				long[] l1 = dq.pollLast();
				long[] l2 = dq.pollLast();
				long[] l3 = dq.pollLast();
				if (-(l3[1] - l2[1]) * (l3[0] - l1[0]) >= -(l3[0] - l2[0]) * (l3[1] - l1[1])) {
					dq.addLast(l3);
					dq.addLast(l1);
					continue;
				} else {
					dq.addLast(l3);
					dq.addLast(l2);
					dq.addLast(l1);
					break;
				}
			}
			while (dq.size() >= 2) {
				long[] l1 = dq.pollFirst();
				long[] l2 = dq.pollFirst();
				if (-(l1[1] - l2[1]) <= a[i] * (l1[0] - l2[0])) {
					dq.addFirst(l2);
				} else {
					dq.addFirst(l2);
					dq.addFirst(l1);
					break;
				}
			}
			d[i] = Math.min(d[i], a[i] * a[i] + dq.peekFirst()[0] * a[i] + dq.peekFirst()[1]);
		}
		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