結果

問題 No.409 ダイエット
ユーザー GrenacheGrenache
提出日時 2018-03-02 14:59:11
言語 Java21
(openjdk 21)
結果
AC  
実行時間 266 ms / 2,000 ms
コード長 5,960 bytes
コンパイル時間 5,009 ms
コンパイル使用メモリ 79,000 KB
実行使用メモリ 60,368 KB
最終ジャッジ日時 2023-09-08 17:41:40
合計ジャッジ時間 18,411 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 45 ms
49,648 KB
testcase_01 AC 44 ms
49,532 KB
testcase_02 AC 44 ms
49,652 KB
testcase_03 AC 44 ms
49,564 KB
testcase_04 AC 45 ms
49,524 KB
testcase_05 AC 45 ms
49,536 KB
testcase_06 AC 44 ms
49,844 KB
testcase_07 AC 44 ms
49,680 KB
testcase_08 AC 45 ms
49,652 KB
testcase_09 AC 46 ms
49,912 KB
testcase_10 AC 44 ms
49,680 KB
testcase_11 AC 45 ms
49,752 KB
testcase_12 AC 44 ms
49,716 KB
testcase_13 AC 46 ms
49,708 KB
testcase_14 AC 46 ms
49,644 KB
testcase_15 AC 44 ms
49,588 KB
testcase_16 AC 43 ms
49,676 KB
testcase_17 AC 44 ms
49,764 KB
testcase_18 AC 44 ms
49,648 KB
testcase_19 AC 45 ms
49,712 KB
testcase_20 AC 48 ms
49,752 KB
testcase_21 AC 45 ms
49,708 KB
testcase_22 AC 45 ms
49,656 KB
testcase_23 AC 46 ms
49,488 KB
testcase_24 AC 45 ms
49,688 KB
testcase_25 AC 47 ms
49,776 KB
testcase_26 AC 49 ms
49,668 KB
testcase_27 AC 44 ms
49,564 KB
testcase_28 AC 44 ms
49,764 KB
testcase_29 AC 46 ms
49,712 KB
testcase_30 AC 47 ms
49,640 KB
testcase_31 AC 45 ms
49,828 KB
testcase_32 AC 44 ms
49,540 KB
testcase_33 AC 45 ms
49,672 KB
testcase_34 AC 45 ms
49,564 KB
testcase_35 AC 79 ms
52,644 KB
testcase_36 AC 82 ms
52,108 KB
testcase_37 AC 73 ms
52,444 KB
testcase_38 AC 86 ms
50,624 KB
testcase_39 AC 83 ms
52,372 KB
testcase_40 AC 84 ms
52,412 KB
testcase_41 AC 79 ms
51,740 KB
testcase_42 AC 86 ms
52,380 KB
testcase_43 AC 84 ms
52,540 KB
testcase_44 AC 78 ms
52,368 KB
testcase_45 AC 84 ms
52,108 KB
testcase_46 AC 86 ms
52,752 KB
testcase_47 AC 78 ms
52,364 KB
testcase_48 AC 85 ms
52,348 KB
testcase_49 AC 74 ms
52,352 KB
testcase_50 AC 73 ms
52,456 KB
testcase_51 AC 86 ms
52,372 KB
testcase_52 AC 88 ms
52,496 KB
testcase_53 AC 87 ms
52,060 KB
testcase_54 AC 76 ms
52,528 KB
testcase_55 AC 168 ms
55,180 KB
testcase_56 AC 224 ms
58,156 KB
testcase_57 AC 239 ms
58,288 KB
testcase_58 AC 170 ms
55,392 KB
testcase_59 AC 240 ms
58,836 KB
testcase_60 AC 159 ms
55,904 KB
testcase_61 AC 221 ms
58,316 KB
testcase_62 AC 219 ms
58,160 KB
testcase_63 AC 221 ms
56,308 KB
testcase_64 AC 157 ms
55,348 KB
testcase_65 AC 235 ms
57,676 KB
testcase_66 AC 233 ms
58,040 KB
testcase_67 AC 208 ms
57,192 KB
testcase_68 AC 222 ms
58,292 KB
testcase_69 AC 228 ms
58,596 KB
testcase_70 AC 266 ms
58,456 KB
testcase_71 AC 212 ms
56,860 KB
testcase_72 AC 263 ms
58,684 KB
testcase_73 AC 259 ms
58,132 KB
testcase_74 AC 228 ms
58,116 KB
testcase_75 AC 261 ms
60,368 KB
testcase_76 AC 231 ms
58,080 KB
testcase_77 AC 200 ms
55,312 KB
testcase_78 AC 211 ms
56,328 KB
testcase_79 AC 190 ms
55,408 KB
testcase_80 AC 257 ms
58,248 KB
testcase_81 AC 258 ms
58,556 KB
testcase_82 AC 237 ms
58,084 KB
testcase_83 AC 223 ms
58,352 KB
testcase_84 AC 235 ms
58,244 KB
testcase_85 AC 111 ms
53,240 KB
testcase_86 AC 237 ms
58,392 KB
testcase_87 AC 263 ms
58,436 KB
testcase_88 AC 179 ms
55,964 KB
testcase_89 AC 234 ms
58,528 KB
testcase_90 AC 179 ms
55,784 KB
testcase_91 AC 237 ms
58,116 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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


public class Main_yukicoder409_4 {

	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];
		ConvexHullTrickOpt cht = new ConvexHullTrickOpt(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 ConvexHullTrickOpt {
		LinkedList<Pair> q;

		// add における a が単調非増加、query における x が単調非減少である場合
		// add O(n), query O(n + q)
		ConvexHullTrickOpt(int n) {
			q = new LinkedList<>();
		}

		// ax+b を追加。a は単調非増加とする
		private void add(long a, long b) {
			Pair next = new Pair(a, b);

			while (q.size() >= 2) {
				Pair curr = q.get(q.size() - 1);
				Pair prev = q.get(q.size() - 2);

				if (check(prev, curr, next)) {
					break;
				}
				q.removeLast();
			}
			q.add(next);
		}

		// f(x)の最小値。x は単調非減少とする
		private long query(long x) {
			long fx0 = fx(q.get(0), x);
			while (q.size() > 1) {
				long fx1 = fx(q.get(1), x);
				if (fx0 < fx1) {
					return fx0;
				}

				q.removeFirst();
				fx0 = fx1;
			}

			return fx0;
		}

		// i 番目の直線を使った時の f(x) の値
		private long fx(Pair i, long x) {
			return i.a * x + i.b;
		}

		// curr の直線が必要な場合 true
		private boolean check(Pair prev, Pair curr, Pair next) {
			return (next.b - curr.b) * (curr.a - prev.a) < (curr.b - prev.b) * (next.a - curr.a);
		}

		private static class Pair {
			long a;
			long b;

			Pair(long a, long b) {
				this.a = a;
				this.b = b;
			}
		}
	}

	// ---------------------------------------------------
	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