結果

問題 No.409 ダイエット
ユーザー GrenacheGrenache
提出日時 2018-03-01 17:44:45
言語 Java21
(openjdk 21)
結果
WA  
(最新)
AC  
(最初)
実行時間 -
コード長 7,783 bytes
コンパイル時間 4,638 ms
コンパイル使用メモリ 92,344 KB
実行使用メモリ 115,328 KB
最終ジャッジ日時 2024-06-26 10:31:35
合計ジャッジ時間 47,206 ms
ジャッジサーバーID
(参考情報)
judge4 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 64 ms
50,900 KB
testcase_01 AC 64 ms
50,680 KB
testcase_02 AC 68 ms
50,860 KB
testcase_03 AC 63 ms
50,692 KB
testcase_04 AC 63 ms
50,764 KB
testcase_05 AC 62 ms
50,800 KB
testcase_06 AC 63 ms
50,704 KB
testcase_07 AC 65 ms
50,700 KB
testcase_08 AC 61 ms
50,472 KB
testcase_09 AC 62 ms
50,772 KB
testcase_10 AC 63 ms
50,724 KB
testcase_11 AC 68 ms
50,708 KB
testcase_12 AC 65 ms
50,500 KB
testcase_13 AC 72 ms
50,816 KB
testcase_14 AC 62 ms
50,820 KB
testcase_15 AC 62 ms
50,912 KB
testcase_16 AC 72 ms
50,800 KB
testcase_17 AC 63 ms
50,280 KB
testcase_18 AC 66 ms
50,504 KB
testcase_19 AC 63 ms
50,324 KB
testcase_20 AC 68 ms
50,964 KB
testcase_21 AC 64 ms
50,488 KB
testcase_22 AC 62 ms
50,744 KB
testcase_23 AC 63 ms
50,508 KB
testcase_24 AC 62 ms
50,676 KB
testcase_25 AC 63 ms
50,800 KB
testcase_26 AC 64 ms
50,416 KB
testcase_27 AC 64 ms
50,832 KB
testcase_28 AC 62 ms
50,780 KB
testcase_29 AC 64 ms
50,524 KB
testcase_30 AC 65 ms
50,680 KB
testcase_31 AC 64 ms
50,716 KB
testcase_32 AC 64 ms
50,520 KB
testcase_33 AC 64 ms
50,412 KB
testcase_34 AC 64 ms
50,844 KB
testcase_35 AC 141 ms
54,124 KB
testcase_36 AC 142 ms
53,504 KB
testcase_37 AC 143 ms
53,848 KB
testcase_38 AC 137 ms
53,452 KB
testcase_39 AC 140 ms
53,588 KB
testcase_40 AC 150 ms
54,044 KB
testcase_41 AC 146 ms
53,340 KB
testcase_42 AC 144 ms
53,304 KB
testcase_43 AC 140 ms
53,516 KB
testcase_44 AC 141 ms
53,240 KB
testcase_45 AC 141 ms
53,564 KB
testcase_46 AC 147 ms
53,464 KB
testcase_47 AC 141 ms
53,308 KB
testcase_48 AC 129 ms
53,272 KB
testcase_49 AC 144 ms
53,176 KB
testcase_50 AC 140 ms
53,744 KB
testcase_51 AC 145 ms
53,920 KB
testcase_52 AC 154 ms
53,432 KB
testcase_53 AC 146 ms
53,784 KB
testcase_54 AC 141 ms
54,036 KB
testcase_55 AC 723 ms
83,304 KB
testcase_56 AC 342 ms
70,092 KB
testcase_57 AC 1,099 ms
114,432 KB
testcase_58 AC 610 ms
81,596 KB
testcase_59 AC 827 ms
84,188 KB
testcase_60 AC 605 ms
80,588 KB
testcase_61 AC 928 ms
100,152 KB
testcase_62 AC 1,110 ms
115,328 KB
testcase_63 AC 1,054 ms
102,872 KB
testcase_64 AC 694 ms
80,712 KB
testcase_65 AC 1,153 ms
89,480 KB
testcase_66 AC 1,125 ms
104,880 KB
testcase_67 AC 937 ms
93,868 KB
testcase_68 AC 852 ms
84,532 KB
testcase_69 AC 1,168 ms
94,336 KB
testcase_70 AC 1,041 ms
104,560 KB
testcase_71 AC 712 ms
80,168 KB
testcase_72 AC 1,124 ms
113,148 KB
testcase_73 AC 1,098 ms
101,096 KB
testcase_74 AC 845 ms
89,072 KB
testcase_75 AC 1,056 ms
102,944 KB
testcase_76 AC 953 ms
92,308 KB
testcase_77 AC 998 ms
78,660 KB
testcase_78 AC 710 ms
83,208 KB
testcase_79 AC 890 ms
83,976 KB
testcase_80 AC 1,108 ms
110,428 KB
testcase_81 AC 1,124 ms
99,760 KB
testcase_82 AC 978 ms
91,428 KB
testcase_83 AC 995 ms
98,408 KB
testcase_84 AC 841 ms
81,708 KB
testcase_85 AC 309 ms
56,484 KB
testcase_86 AC 855 ms
82,628 KB
testcase_87 WA -
testcase_88 AC 882 ms
73,764 KB
testcase_89 AC 928 ms
77,300 KB
testcase_90 AC 571 ms
70,376 KB
testcase_91 AC 868 ms
79,880 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.io.*;
import java.math.*;
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[] dn;
//		long[] dd;
		double[] dd;
		NavigableSet<Integer> f;
		NavigableSet<Integer> d;

		ConvexHullTrick(int n) {
//			this.n = n;

			cnt = 0;
			aa = new long[n];
			bb = new long[n];
//			dn = new long[n + 1];
//			dd = new long[n + 1];
			dd = new double[n + 1];
			f = new TreeSet<>((x, y) -> {return aa[x] == aa[y] ? Long.compare(bb[x], bb[y]) : Long.compare(aa[y], aa[x]);});
			d = new TreeSet<>((x, y) -> {return Double.compare(dd[x], dd[y]);});
		}

		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);
				d.remove(prev);
				prev = pprev;
			}

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

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

			if (prev == null) {
				dd[curr] = Double.NEGATIVE_INFINITY;
				f.add(curr);
				d.add(curr);
			} else {
				dd[curr] = (double)(bb[curr] - bb[prev]) / (aa[prev] - aa[curr]);
				f.add(curr);
				d.add(curr);
			}

			if (next != null) {
				d.remove(next);
				dd[next] = (double)(bb[next] - bb[curr]) / (aa[curr] - aa[next]);
				d.add(next);
			}
		}

		private long query(long x) {
			dd[cnt] = (double)x;

			Integer mini = d.lower(cnt);

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

			return fx(mini, x);
		}

		private long fx(int i, long x) {
			return aa[i] * x + bb[i];
		}

		private boolean check(Integer prev, Integer curr, Integer next) {
			if (prev == null || next == null) {
				return true;
			}

			return bb[next] * aa[prev] - bb[prev] * aa[next] > aa[curr] * bb[next] - aa[curr] * bb[prev] + bb[curr] * aa[prev] - bb[curr] * aa[next];
			/*
			BigInteger LMAX = BigInteger.valueOf(Long.MAX_VALUE);
			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]);
			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(System.in);
		pr = new Printer(System.out);

		solve();

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

	@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(PrintStream out) {
			super(out);
		}
	}
}
0