結果

問題 No.705 ゴミ拾い Hard
ユーザー 37zigen37zigen
提出日時 2018-07-01 06:23:10
言語 Java21
(openjdk 21)
結果
WA  
実行時間 -
コード長 3,842 bytes
コンパイル時間 3,118 ms
コンパイル使用メモリ 75,080 KB
実行使用メモリ 67,536 KB
最終ジャッジ日時 2023-09-13 16:31:41
合計ジャッジ時間 12,299 ms
ジャッジサーバーID
(参考情報)
judge11 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 41 ms
49,400 KB
testcase_01 AC 42 ms
49,092 KB
testcase_02 AC 42 ms
49,392 KB
testcase_03 AC 41 ms
49,412 KB
testcase_04 AC 42 ms
49,108 KB
testcase_05 AC 41 ms
49,096 KB
testcase_06 AC 41 ms
49,144 KB
testcase_07 AC 41 ms
49,304 KB
testcase_08 AC 41 ms
49,064 KB
testcase_09 AC 40 ms
49,564 KB
testcase_10 AC 42 ms
49,164 KB
testcase_11 AC 42 ms
49,320 KB
testcase_12 AC 41 ms
49,348 KB
testcase_13 AC 42 ms
49,444 KB
testcase_14 AC 57 ms
50,848 KB
testcase_15 AC 58 ms
50,128 KB
testcase_16 AC 56 ms
50,616 KB
testcase_17 AC 57 ms
51,148 KB
testcase_18 AC 57 ms
48,712 KB
testcase_19 AC 58 ms
50,888 KB
testcase_20 WA -
testcase_21 AC 56 ms
50,224 KB
testcase_22 AC 57 ms
50,256 KB
testcase_23 AC 57 ms
50,496 KB
testcase_24 AC 363 ms
63,452 KB
testcase_25 AC 361 ms
63,156 KB
testcase_26 AC 360 ms
63,344 KB
testcase_27 AC 410 ms
67,324 KB
testcase_28 AC 362 ms
63,552 KB
testcase_29 AC 302 ms
62,888 KB
testcase_30 AC 333 ms
66,612 KB
testcase_31 AC 333 ms
63,772 KB
testcase_32 AC 358 ms
63,588 KB
testcase_33 AC 160 ms
67,536 KB
testcase_34 AC 160 ms
65,720 KB
testcase_35 AC 273 ms
62,288 KB
testcase_36 AC 264 ms
62,380 KB
testcase_37 AC 296 ms
63,136 KB
testcase_38 AC 263 ms
62,544 KB
testcase_39 WA -
testcase_40 AC 41 ms
49,440 KB
testcase_41 AC 40 ms
49,040 KB
testcase_42 AC 41 ms
49,184 KB
testcase_43 AC 286 ms
62,736 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

class Main {

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

	int N;
	long[] A, X, Y;
	long[] D;

	void run() {
		Scanner sc = new Scanner();
		N = sc.nextInt();
		A = new long[N];
		X = new long[N];
		Y = new long[N];
		D = new long[N];
		Arrays.fill(D, Long.MAX_VALUE / 3);
		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();
		}
		// d[i]=min_{j<=i}(d[j-1]+cost[j,i])
		ArrayDeque<P> dq = new ArrayDeque<>();
		for (int i = 0; i < N; ++i) {
			// d[i]を求める
			while (dq.size() >= 2 && second(dq).i <= i) {
				dq.pollFirst();
			}
			if (!dq.isEmpty() && f(i, dq.peekLast().j) >= f(i, i)) {
				dq.pollLast();
			}
			if (dq.isEmpty()) {
				dq.addLast(new P(i, i));
				D[i] = f(i, i);
				continue;
			}
			D[i] = f(i, dq.peekFirst().j);
			while (true) {
				if (dq.isEmpty()) {
					throw new AssertionError();
				}
				int ok = N;
				int ng = i;
				while (ok - ng > 1) {
					int m = (ok + ng) / 2;
					if (f(m, i) <= f(m, dq.peekLast().j)) {
						ok = m;
					} else {
						ng = m;
					}
				}
				if (ok > dq.peekLast().i) {
					if (ok < N)
						dq.addLast(new P(ok, i));
					break;
				} else {
					dq.pollLast();
				}
			}
		}
		System.out.println(D[N - 1]);
	}

	P second(ArrayDeque<P> dq) {
		if (dq.size() < 2)
			throw new AssertionError();
		P tmp = dq.pollFirst();
		P ret = new P(dq.peekFirst().i, dq.peekFirst().j);
		dq.addFirst(tmp);
		return ret;
	}

	long cost(int from, int to) {
		long dx = Math.abs(X[from] - A[to]);
		long dy = Math.abs(Y[from]);
		return dx * dx * dx + dy * dy * dy;
	}

	long f(int i, int j) {
		if (i < j)
			throw new AssertionError();
		if (j > 0 && D[j - 1] == -1)
			throw new AssertionError();
		return (j > 0 ? D[j - 1] : 0) + cost(j, i);
	}

	class P {
		int i, j;

		public P(int i_, int j_) {
			i = i_;
			j = j_;
		}
	}

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

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