結果

問題 No.409 ダイエット
ユーザー potoooooooopotoooooooo
提出日時 2018-07-16 03:07:05
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 96 ms / 2,000 ms
コード長 2,326 bytes
コンパイル時間 1,527 ms
コンパイル使用メモリ 171,004 KB
実行使用メモリ 13,460 KB
最終ジャッジ日時 2023-08-07 19:37:22
合計ジャッジ時間 7,286 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 2 ms
4,376 KB
testcase_04 AC 1 ms
4,380 KB
testcase_05 AC 2 ms
4,380 KB
testcase_06 AC 2 ms
4,376 KB
testcase_07 AC 2 ms
4,380 KB
testcase_08 AC 2 ms
4,376 KB
testcase_09 AC 1 ms
4,376 KB
testcase_10 AC 2 ms
4,380 KB
testcase_11 AC 2 ms
4,376 KB
testcase_12 AC 1 ms
4,376 KB
testcase_13 AC 2 ms
4,376 KB
testcase_14 AC 1 ms
4,376 KB
testcase_15 AC 1 ms
4,376 KB
testcase_16 AC 2 ms
4,380 KB
testcase_17 AC 1 ms
4,376 KB
testcase_18 AC 1 ms
4,376 KB
testcase_19 AC 2 ms
4,384 KB
testcase_20 AC 2 ms
4,376 KB
testcase_21 AC 1 ms
4,380 KB
testcase_22 AC 1 ms
4,376 KB
testcase_23 AC 1 ms
4,380 KB
testcase_24 AC 1 ms
4,376 KB
testcase_25 AC 2 ms
4,376 KB
testcase_26 AC 1 ms
4,380 KB
testcase_27 AC 2 ms
4,376 KB
testcase_28 AC 1 ms
4,380 KB
testcase_29 AC 1 ms
4,376 KB
testcase_30 AC 2 ms
4,376 KB
testcase_31 AC 1 ms
4,376 KB
testcase_32 AC 1 ms
4,380 KB
testcase_33 AC 2 ms
4,380 KB
testcase_34 AC 1 ms
4,380 KB
testcase_35 AC 2 ms
4,380 KB
testcase_36 AC 2 ms
4,376 KB
testcase_37 AC 2 ms
4,376 KB
testcase_38 AC 3 ms
4,384 KB
testcase_39 AC 2 ms
4,380 KB
testcase_40 AC 3 ms
4,380 KB
testcase_41 AC 3 ms
4,380 KB
testcase_42 AC 2 ms
4,376 KB
testcase_43 AC 2 ms
4,376 KB
testcase_44 AC 3 ms
4,380 KB
testcase_45 AC 2 ms
4,376 KB
testcase_46 AC 2 ms
4,380 KB
testcase_47 AC 2 ms
4,376 KB
testcase_48 AC 3 ms
4,380 KB
testcase_49 AC 2 ms
4,376 KB
testcase_50 AC 2 ms
4,376 KB
testcase_51 AC 3 ms
4,380 KB
testcase_52 AC 2 ms
4,376 KB
testcase_53 AC 3 ms
4,380 KB
testcase_54 AC 2 ms
4,380 KB
testcase_55 AC 32 ms
7,312 KB
testcase_56 AC 55 ms
7,484 KB
testcase_57 AC 63 ms
13,164 KB
testcase_58 AC 28 ms
7,032 KB
testcase_59 AC 39 ms
7,824 KB
testcase_60 AC 25 ms
6,780 KB
testcase_61 AC 56 ms
11,560 KB
testcase_62 AC 59 ms
12,880 KB
testcase_63 AC 55 ms
12,012 KB
testcase_64 AC 30 ms
7,280 KB
testcase_65 AC 57 ms
9,376 KB
testcase_66 AC 62 ms
12,968 KB
testcase_67 AC 48 ms
11,296 KB
testcase_68 AC 39 ms
7,784 KB
testcase_69 AC 54 ms
11,920 KB
testcase_70 AC 79 ms
12,332 KB
testcase_71 AC 45 ms
7,464 KB
testcase_72 AC 96 ms
12,540 KB
testcase_73 AC 90 ms
12,700 KB
testcase_74 AC 59 ms
11,548 KB
testcase_75 AC 89 ms
11,448 KB
testcase_76 AC 66 ms
11,504 KB
testcase_77 AC 44 ms
6,056 KB
testcase_78 AC 52 ms
11,276 KB
testcase_79 AC 41 ms
6,996 KB
testcase_80 AC 94 ms
12,668 KB
testcase_81 AC 90 ms
13,460 KB
testcase_82 AC 67 ms
10,824 KB
testcase_83 AC 72 ms
11,552 KB
testcase_84 AC 59 ms
7,896 KB
testcase_85 AC 8 ms
4,384 KB
testcase_86 AC 58 ms
7,688 KB
testcase_87 AC 81 ms
12,532 KB
testcase_88 AC 33 ms
5,476 KB
testcase_89 AC 65 ms
6,688 KB
testcase_90 AC 30 ms
5,128 KB
testcase_91 AC 69 ms
6,884 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;

template<class Numeric> struct ConvexHullTrick {
  struct Line { // f: y=ax+b
    Numeric a; Numeric b;
    Line(Numeric _a, Numeric _b) : a(_a), b(_b) {}
    bool operator< (Line &l) {
      if (a != l.a) return a < l.a;
      else return b < l.b;
    }
  };

  private:
    vector<Line> lines;
    bool isMinimumQuery;
    bool isMonotonicQuery;
    bool compare(Numeric a, Numeric b) {
      if (isMinimumQuery) return b <= a;
      else return a <= b;
    }

  public:
    // constructor
    ConvexHullTrick(bool monotonic, bool minimum = true) :
      isMinimumQuery(minimum), isMonotonicQuery(monotonic) {}
    // check the necessity of line2
    bool check(Line l1, Line l2, Line l3) {
      if (l1 < l3) swap(l3, l1);
      return compare((l3.b-l2.b)*(l2.a-l1.a), (l2.b-l1.b)*(l3.a-l2.a));
    }
    // add the line: y=ax+b
    void add(Numeric a, Numeric b) {
      Line line(a, b);
      int size = (int) lines.size();
      while (size >= 2 && check(lines[size-2], lines[size-1], line)){
        lines.pop_back(); --size;
      }
      lines.emplace_back(line);
    }
    // get the value of line(x)
    Numeric f(Line line, Numeric x) {
      return line.a * x + line.b;
    }
    // solve
    Numeric solve(Numeric x) {
      if (isMonotonicQuery) {
        static int head = 0;
        while (lines.size() - head >= 2 && compare(f(lines[head], x), f(lines[head+1], x))) ++head;
        return f(lines[head], x);
      }
      else {
        int low = -1; int high = lines.size()-1;
        while (high - low > 1) {
          int mid = (high+low)/2;
          if (compare(f(lines[mid], x), f(lines[mid+1], x))) low = mid;
          else high = mid;
        }
        return f(lines[high], x);
      }
    }

};

int main(){
  long long n, a, b, w; cin >> n >> a >> b >> w;
  vector<long long> d(n);
  for (long long i = 0; i < n; i++) {
    cin >> d[i];
  }
  vector<long long> dp(n+1, 0);
  ConvexHullTrick<long long> convexhulltrick(true);
  for (long long i = 0; i < n; i++) {
    convexhulltrick.add(-i*b, dp[i]+i*a+(i+1)*i*b/2);
    dp[i+1] = convexhulltrick.solve(i+1) - a*i + (i+1)*i*b/2 + d[i];
  }
  long long ans = 1e18;
  for (long long i = 0; i <= n; i++) {
    ans = min(ans, w+dp[i]-(n-i)*a+(n-i)*(n-i+1)*b/2);
  }
  cout << ans << endl;
  return 0;
}
0