結果

問題 No.409 ダイエット
ユーザー GrenacheGrenache
提出日時 2018-03-03 00:49:45
言語 Java21
(openjdk 21)
結果
AC  
実行時間 772 ms / 2,000 ms
コード長 9,024 bytes
コンパイル時間 4,654 ms
コンパイル使用メモリ 89,388 KB
実行使用メモリ 90,216 KB
最終ジャッジ日時 2023-09-08 17:44:08
合計ジャッジ時間 34,028 ms
ジャッジサーバーID
(参考情報)
judge11 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 53 ms
50,252 KB
testcase_01 AC 53 ms
50,024 KB
testcase_02 AC 52 ms
50,160 KB
testcase_03 AC 51 ms
50,544 KB
testcase_04 AC 50 ms
50,384 KB
testcase_05 AC 52 ms
48,576 KB
testcase_06 AC 51 ms
50,152 KB
testcase_07 AC 51 ms
50,236 KB
testcase_08 AC 51 ms
50,036 KB
testcase_09 AC 51 ms
50,416 KB
testcase_10 AC 51 ms
50,040 KB
testcase_11 AC 54 ms
50,164 KB
testcase_12 AC 51 ms
50,160 KB
testcase_13 AC 51 ms
50,140 KB
testcase_14 AC 52 ms
50,392 KB
testcase_15 AC 53 ms
50,120 KB
testcase_16 AC 52 ms
50,308 KB
testcase_17 AC 51 ms
50,240 KB
testcase_18 AC 52 ms
50,136 KB
testcase_19 AC 51 ms
50,388 KB
testcase_20 AC 53 ms
48,608 KB
testcase_21 AC 50 ms
50,120 KB
testcase_22 AC 50 ms
50,576 KB
testcase_23 AC 51 ms
50,748 KB
testcase_24 AC 50 ms
50,556 KB
testcase_25 AC 53 ms
50,140 KB
testcase_26 AC 51 ms
50,104 KB
testcase_27 AC 52 ms
50,132 KB
testcase_28 AC 52 ms
51,020 KB
testcase_29 AC 52 ms
50,140 KB
testcase_30 AC 54 ms
50,060 KB
testcase_31 AC 52 ms
50,044 KB
testcase_32 AC 52 ms
50,028 KB
testcase_33 AC 52 ms
50,300 KB
testcase_34 AC 52 ms
50,160 KB
testcase_35 AC 112 ms
54,024 KB
testcase_36 AC 113 ms
54,092 KB
testcase_37 AC 119 ms
53,592 KB
testcase_38 AC 119 ms
54,136 KB
testcase_39 AC 118 ms
54,380 KB
testcase_40 AC 123 ms
54,396 KB
testcase_41 AC 122 ms
53,916 KB
testcase_42 AC 126 ms
54,220 KB
testcase_43 AC 122 ms
54,264 KB
testcase_44 AC 119 ms
53,388 KB
testcase_45 AC 112 ms
54,072 KB
testcase_46 AC 120 ms
53,644 KB
testcase_47 AC 122 ms
54,124 KB
testcase_48 AC 124 ms
53,904 KB
testcase_49 AC 121 ms
53,796 KB
testcase_50 AC 125 ms
54,048 KB
testcase_51 AC 119 ms
54,040 KB
testcase_52 AC 123 ms
54,392 KB
testcase_53 AC 121 ms
54,336 KB
testcase_54 AC 119 ms
53,796 KB
testcase_55 AC 473 ms
71,252 KB
testcase_56 AC 305 ms
71,472 KB
testcase_57 AC 707 ms
90,216 KB
testcase_58 AC 455 ms
70,236 KB
testcase_59 AC 568 ms
77,472 KB
testcase_60 AC 412 ms
68,504 KB
testcase_61 AC 648 ms
83,172 KB
testcase_62 AC 697 ms
84,132 KB
testcase_63 AC 657 ms
84,180 KB
testcase_64 AC 455 ms
69,024 KB
testcase_65 AC 660 ms
79,656 KB
testcase_66 AC 671 ms
84,776 KB
testcase_67 AC 598 ms
80,444 KB
testcase_68 AC 528 ms
74,828 KB
testcase_69 AC 664 ms
82,368 KB
testcase_70 AC 688 ms
84,068 KB
testcase_71 AC 512 ms
69,564 KB
testcase_72 AC 772 ms
90,084 KB
testcase_73 AC 702 ms
82,492 KB
testcase_74 AC 569 ms
77,784 KB
testcase_75 AC 755 ms
83,572 KB
testcase_76 AC 603 ms
80,132 KB
testcase_77 AC 498 ms
68,756 KB
testcase_78 AC 548 ms
76,764 KB
testcase_79 AC 446 ms
68,988 KB
testcase_80 AC 750 ms
85,956 KB
testcase_81 AC 697 ms
82,888 KB
testcase_82 AC 607 ms
79,936 KB
testcase_83 AC 718 ms
79,976 KB
testcase_84 AC 599 ms
75,036 KB
testcase_85 AC 204 ms
58,292 KB
testcase_86 AC 551 ms
73,756 KB
testcase_87 AC 691 ms
84,648 KB
testcase_88 AC 417 ms
66,388 KB
testcase_89 AC 588 ms
68,748 KB
testcase_90 AC 409 ms
66,452 KB
testcase_91 AC 612 ms
70,592 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.io.*;
import java.util.*;


public class Main_yukicoder409 {

	private static Scanner sc;
	private static Printer pr;

	private static void solve() {
		int n = sc.nextInt();
		long a = sc.nextInt();
		long b = sc.nextInt();
		long w = sc.nextInt();

		int[] d = new int[n];
		for (int i = 0; i < n; i++) {
			d[i] = sc.nextInt();
		}

		long[] dp = new long[n + 1];
		ConvexHullTrick cht = new ConvexHullTrick(n);
		for (int i = 1; i <= n; i++) {
			int j = i - 1;
			cht.add(-b * j, dp[j] + a * j + b * j * (j + 1) / 2);
			dp[i] = cht.query(i) + d[i - 1] - a * (i - 1) + b * i * (i - 1) / 2;
		}

		long ans = Long.MAX_VALUE;
		for (int i = 0; i <= n; i++) {
			long tmp = dp[i] - a * (n - i) + b * (n - i) * (n - i + 1) / 2;
//			pr.printf("%d %d %d\n", i, tmp, ans);
			ans = Math.min(ans, tmp);
		}

		pr.println(ans + w);
	}

	private static class ConvexHullTrick {
//		int n;

		int cnt;
		long[] aa;
		long[] bb;
		long[] left;
		NavigableSet<Integer> f;
//		NavigableSet<Integer> d;
		// TreeSet で利用する Comparator 内で利用する。直線の傾きなら true、左端の座標なら false
		boolean cmpf;

		// add, query ともに特別な条件がない一般 ConvexHullTrick
		// 追加できる直線は n 本まで。
		ConvexHullTrick(int n) {
//			this.n = n;

			cnt = 0;
			aa = new long[n];
			bb = new long[n];
			// i番目の直線と1つ前の直線との交点のx座標。0番目は-INF
			left = new long[n + 1];

			cmpf = true;

			// 必要な直線を傾きの降順に保持する
			// cmpf によって、直線の左端のx座標の昇順で探索する
			f = new TreeSet<>((x, y) -> {
				if (cmpf) return aa[x] == aa[y] ? Long.compare(bb[x], bb[y]) : Long.compare(aa[y], aa[x]);
				else return left[x] == left[y] ? Integer.compare(x, y) : Long.compare(left[x], left[y]);
			});
		}

		// ax+b を追加。
		private void add(long a, long b) {
			aa[cnt] = a;
			bb[cnt] = b;

			int curr = cnt++;

			if (f.contains(curr)) {
				return;
			}

			Integer prev = f.lower(curr);
			Integer next = f.higher(curr);

			if (!check(prev, curr, next)) {
				return;
			}

			while (prev != null) {
				Integer pprev = f.lower(prev);
				if (check(pprev, prev, curr)) {
					break;
				}

				f.remove(prev);
				prev = pprev;
			}

			while (next != null) {
				Integer nnext = f.higher(next);
				if (check(curr, next, nnext)) {
					break;
				}

				f.remove(next);
				next = nnext;
			}

			if (prev == null) {
				left[curr] = Long.MIN_VALUE;
				f.add(curr);
			} else {
				long bbb = bb[curr] - bb[prev];
				long aaa = aa[prev] - aa[curr];
				left[curr] = bbb >= 0 ? (bbb + aaa - 1) / aaa : bbb / aaa;
				f.add(curr);
			}

			if (next != null) {
				long bbb = bb[next] - bb[curr];
				long aaa = aa[curr] - aa[next];
				left[next] = bbb >= 0 ? (bbb + aaa - 1) / aaa : bbb / aaa;
			}
		}

		// f(x)の最小値。
		private long query(long x) {
			left[cnt] = x;

			cmpf = false;
			Integer mini = f.lower(cnt);
			cmpf = true;

//			if (mini == null) {
//				pr.printf("%d %f\n", x, dd[cnt]);
//				pr.flush();
//			}

			return fx(mini, x);
		}

		// i 番目の直線を使った時の f(x) の値
		private long fx(int i, long x) {
			return aa[i] * x + bb[i];
		}

		// curr の直線が必要な場合 true
		private boolean check(Integer prev, Integer curr, Integer next) {
			// Comparator で aは降順、bは昇順なので
			if (prev == null) {
				return true;
			} else {
				if (aa[prev] == aa[curr]) {
					return false;
				}
			}

			if (next == null) {
				return true;
			}


			double b32 = bb[next] - bb[curr];
			double a21 = aa[curr] - aa[prev];
			double b21 = bb[curr] - bb[prev];
			double a32 = aa[next] - aa[curr];
//			if (Math.abs(b32 * a21) > (double)Long.MAX_VALUE || Math.abs(b21 * a32) > (double)Long.MAX_VALUE) {
//				throw new RuntimeException();
//			}

			return b32 * a21 < b21 * a32;

			// long でいける場合
//			return (bb[next] - bb[curr]) * (aa[curr] - aa[prev]) < (bb[curr] - bb[prev]) * (aa[next] - aa[curr]);
			/*
			BigInteger a1 = BigInteger.valueOf(aa[prev]);
			BigInteger a2 = BigInteger.valueOf(aa[curr]);
			BigInteger a3 = BigInteger.valueOf(aa[next]);
			BigInteger b1 = BigInteger.valueOf(bb[prev]);
			BigInteger b2 = BigInteger.valueOf(bb[curr]);
			BigInteger b3 = BigInteger.valueOf(bb[next]);

//			BigInteger LMAX = BigInteger.valueOf(Long.MAX_VALUE);
//			if (b3.multiply(a1).abs().compareTo(LMAX) > 0) {
//				pr.printf("Overflow : %s %s\n", b3, a1);
//			}
//			if (b1.multiply(a3).abs().compareTo(LMAX) > 0) {
//				pr.printf("Overflow : %s %s\n", b1, a3);
//			}
//			if (a2.multiply(b3).abs().compareTo(LMAX) > 0) {
//				pr.printf("Overflow : %s %s\n", a2, b3);
//			}
//			if (a2.multiply(b1).abs().compareTo(LMAX) > 0) {
//				pr.printf("Overflow : %s %s\n", a2, b1);
//			}
//			if (b2.multiply(a1).abs().compareTo(LMAX) > 0) {
//				pr.printf("Overflow : %s %s\n", b2, a1);
//			}
//			if (b2.multiply(a3).abs().compareTo(LMAX) > 0) {
//				pr.printf("Overflow : %s %s\n", b2, a3);
//			}
			return b3.multiply(a1).subtract(b1.multiply(a3)).compareTo(a2.multiply(b3).subtract(a2.multiply(b1)).add(b2.multiply(a1)).subtract(b2.multiply(a3))) > 0;
//			*/
		}
	}

	// ---------------------------------------------------
	public static void main(String[] args) {
		sc = new Scanner(INPUT == null ? System.in : new ByteArrayInputStream(INPUT.getBytes()));
		pr = new Printer(System.out);

		solve();

//		pr.close();
		pr.flush();
//		sc.close();
	}

	static String INPUT = null;

	@SuppressWarnings("unused")
	private static class Scanner {
		BufferedReader br;

		Scanner (InputStream in) {
			br = new BufferedReader(new InputStreamReader(in));
		}

		private boolean isPrintable(int ch) {
			return ch >= '!' && ch <= '~';
		}

		private boolean isCRLF(int ch) {
			return ch == '\n' || ch == '\r' || ch == -1;
		}

		private int nextPrintable() {
			try {
				int ch;
				while (!isPrintable(ch = br.read())) {
					if (ch == -1) {
						throw new NoSuchElementException();
					}
				}

				return ch;
			} catch (IOException e) {
				throw new NoSuchElementException();
			}
		}

		String next() {
			try {
				int ch = nextPrintable();
				StringBuilder sb = new StringBuilder();
				do {
					sb.appendCodePoint(ch);
				} while (isPrintable(ch = br.read()));

				return sb.toString();
			} catch (IOException e) {
				throw new NoSuchElementException();
			}
		}

		int nextInt() {
			try {
				// parseInt from Integer.parseInt()
				boolean negative = false;
				int res = 0;
				int limit = -Integer.MAX_VALUE;
				int radix = 10;

				int fc = nextPrintable();
				if (fc < '0') {
					if (fc == '-') {
						negative = true;
						limit = Integer.MIN_VALUE;
					} else if (fc != '+') {
						throw new NumberFormatException();
					}
					fc = br.read();
				}
				int multmin = limit / radix;

				int ch = fc;
				do {
					int digit = ch - '0';
					if (digit < 0 || digit >= radix) {
						throw new NumberFormatException();
					}
					if (res < multmin) {
						throw new NumberFormatException();
					}
					res *= radix;
					if (res < limit + digit) {
						throw new NumberFormatException();
					}
					res -= digit;

				} while (isPrintable(ch = br.read()));

				return negative ? res : -res;
			} catch (IOException e) {
				throw new NoSuchElementException();
			}
		}

		long nextLong() {
			try {
				// parseLong from Long.parseLong()
				boolean negative = false;
				long res = 0;
				long limit = -Long.MAX_VALUE;
				int radix = 10;

				int fc = nextPrintable();
				if (fc < '0') {
					if (fc == '-') {
						negative = true;
						limit = Long.MIN_VALUE;
					} else if (fc != '+') {
						throw new NumberFormatException();
					}
					fc = br.read();
				}
				long multmin = limit / radix;

				int ch = fc;
				do {
					int digit = ch - '0';
					if (digit < 0 || digit >= radix) {
						throw new NumberFormatException();
					}
					if (res < multmin) {
						throw new NumberFormatException();
					}
					res *= radix;
					if (res < limit + digit) {
						throw new NumberFormatException();
					}
					res -= digit;

				} while (isPrintable(ch = br.read()));

				return negative ? res : -res;
			} catch (IOException e) {
				throw new NoSuchElementException();
			}
		}

		float nextFloat() {
			return Float.parseFloat(next());
		}

		double nextDouble() {
			return Double.parseDouble(next());
		}

		String nextLine() {
			try {
				int ch;
				while (isCRLF(ch = br.read())) {
					if (ch == -1) {
						throw new NoSuchElementException();
					}
				}
				StringBuilder sb = new StringBuilder();
				do {
					sb.appendCodePoint(ch);
				} while (!isCRLF(ch = br.read()));

				return sb.toString();
			} catch (IOException e) {
				throw new NoSuchElementException();
			}
		}

		void close() {
			try {
				br.close();
			} catch (IOException e) {
//				throw new NoSuchElementException();
			}
		}
	}

	private static class Printer extends PrintWriter {
		Printer(OutputStream out) {
			super(out);
		}
	}
}
0