結果

問題 No.409 ダイエット
ユーザー GrenacheGrenache
提出日時 2018-03-02 14:59:11
言語 Java21
(openjdk 21)
結果
AC  
実行時間 292 ms / 2,000 ms
コード長 5,960 bytes
コンパイル時間 3,985 ms
コンパイル使用メモリ 80,840 KB
実行使用メモリ 59,784 KB
最終ジャッジ日時 2024-06-26 10:30:02
合計ジャッジ時間 19,291 ms
ジャッジサーバーID
(参考情報)
judge4 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 56 ms
50,132 KB
testcase_01 AC 55 ms
50,680 KB
testcase_02 AC 55 ms
50,136 KB
testcase_03 AC 59 ms
50,600 KB
testcase_04 AC 58 ms
50,532 KB
testcase_05 AC 56 ms
52,496 KB
testcase_06 AC 56 ms
50,200 KB
testcase_07 AC 57 ms
50,620 KB
testcase_08 AC 56 ms
50,644 KB
testcase_09 AC 55 ms
50,476 KB
testcase_10 AC 55 ms
50,240 KB
testcase_11 AC 56 ms
50,616 KB
testcase_12 AC 56 ms
50,360 KB
testcase_13 AC 56 ms
50,132 KB
testcase_14 AC 56 ms
50,676 KB
testcase_15 AC 56 ms
50,476 KB
testcase_16 AC 56 ms
50,556 KB
testcase_17 AC 56 ms
50,512 KB
testcase_18 AC 59 ms
50,528 KB
testcase_19 AC 57 ms
50,532 KB
testcase_20 AC 56 ms
50,680 KB
testcase_21 AC 55 ms
50,632 KB
testcase_22 AC 56 ms
50,572 KB
testcase_23 AC 56 ms
50,616 KB
testcase_24 AC 55 ms
50,624 KB
testcase_25 AC 56 ms
50,524 KB
testcase_26 AC 55 ms
50,628 KB
testcase_27 AC 55 ms
50,704 KB
testcase_28 AC 58 ms
50,156 KB
testcase_29 AC 55 ms
50,168 KB
testcase_30 AC 57 ms
50,624 KB
testcase_31 AC 57 ms
50,636 KB
testcase_32 AC 57 ms
50,128 KB
testcase_33 AC 59 ms
50,444 KB
testcase_34 AC 56 ms
50,140 KB
testcase_35 AC 93 ms
51,708 KB
testcase_36 AC 84 ms
51,436 KB
testcase_37 AC 96 ms
52,052 KB
testcase_38 AC 97 ms
51,704 KB
testcase_39 AC 90 ms
51,292 KB
testcase_40 AC 98 ms
51,720 KB
testcase_41 AC 92 ms
51,444 KB
testcase_42 AC 98 ms
51,708 KB
testcase_43 AC 97 ms
51,512 KB
testcase_44 AC 85 ms
51,824 KB
testcase_45 AC 96 ms
51,540 KB
testcase_46 AC 95 ms
51,704 KB
testcase_47 AC 87 ms
51,548 KB
testcase_48 AC 94 ms
51,960 KB
testcase_49 AC 93 ms
51,444 KB
testcase_50 AC 96 ms
52,332 KB
testcase_51 AC 98 ms
52,188 KB
testcase_52 AC 100 ms
51,804 KB
testcase_53 AC 91 ms
51,480 KB
testcase_54 AC 98 ms
51,588 KB
testcase_55 AC 174 ms
55,692 KB
testcase_56 AC 242 ms
57,696 KB
testcase_57 AC 238 ms
57,752 KB
testcase_58 AC 177 ms
55,556 KB
testcase_59 AC 204 ms
57,572 KB
testcase_60 AC 175 ms
55,900 KB
testcase_61 AC 235 ms
57,424 KB
testcase_62 AC 250 ms
58,036 KB
testcase_63 AC 239 ms
58,100 KB
testcase_64 AC 191 ms
55,456 KB
testcase_65 AC 256 ms
58,116 KB
testcase_66 AC 252 ms
58,024 KB
testcase_67 AC 239 ms
57,756 KB
testcase_68 AC 208 ms
57,728 KB
testcase_69 AC 243 ms
58,080 KB
testcase_70 AC 271 ms
58,516 KB
testcase_71 AC 214 ms
55,664 KB
testcase_72 AC 287 ms
58,560 KB
testcase_73 AC 278 ms
58,656 KB
testcase_74 AC 246 ms
57,572 KB
testcase_75 AC 273 ms
58,488 KB
testcase_76 AC 257 ms
58,064 KB
testcase_77 AC 224 ms
55,740 KB
testcase_78 AC 222 ms
55,676 KB
testcase_79 AC 212 ms
55,396 KB
testcase_80 AC 292 ms
59,784 KB
testcase_81 AC 270 ms
58,524 KB
testcase_82 AC 261 ms
58,176 KB
testcase_83 AC 267 ms
57,948 KB
testcase_84 AC 266 ms
58,176 KB
testcase_85 AC 125 ms
53,176 KB
testcase_86 AC 271 ms
58,232 KB
testcase_87 AC 273 ms
58,264 KB
testcase_88 AC 206 ms
55,916 KB
testcase_89 AC 267 ms
58,332 KB
testcase_90 AC 200 ms
55,884 KB
testcase_91 AC 269 ms
58,332 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