結果
問題 | No.703 ゴミ拾い Easy |
ユーザー |
|
提出日時 | 2018-06-16 08:10:59 |
言語 | Java (openjdk 23) |
結果 |
AC
|
実行時間 | 300 ms / 1,500 ms |
コード長 | 3,293 bytes |
コンパイル時間 | 3,038 ms |
コンパイル使用メモリ | 81,492 KB |
実行使用メモリ | 66,160 KB |
最終ジャッジ日時 | 2025-01-02 13:54:43 |
合計ジャッジ時間 | 12,395 ms |
ジャッジサーバーID (参考情報) |
judge5 / judge4 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 4 |
other | AC * 46 |
ソースコード
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++];elsereturn -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();}}}