結果

問題 No.409 ダイエット
ユーザー potoooooooopotoooooooo
提出日時 2018-07-16 03:02:54
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 2,460 bytes
コンパイル時間 1,697 ms
コンパイル使用メモリ 170,856 KB
実行使用メモリ 13,004 KB
最終ジャッジ日時 2024-04-25 13:37:57
合計ジャッジ時間 8,874 ms
ジャッジサーバーID
(参考情報)
judge5 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,812 KB
testcase_01 AC 2 ms
6,944 KB
testcase_02 AC 1 ms
6,940 KB
testcase_03 AC 2 ms
6,940 KB
testcase_04 AC 2 ms
6,940 KB
testcase_05 AC 2 ms
6,944 KB
testcase_06 AC 2 ms
6,940 KB
testcase_07 AC 2 ms
6,944 KB
testcase_08 AC 1 ms
6,944 KB
testcase_09 AC 2 ms
6,944 KB
testcase_10 AC 2 ms
6,944 KB
testcase_11 AC 2 ms
6,940 KB
testcase_12 AC 1 ms
6,948 KB
testcase_13 AC 1 ms
6,940 KB
testcase_14 AC 2 ms
6,940 KB
testcase_15 AC 2 ms
6,940 KB
testcase_16 AC 1 ms
6,940 KB
testcase_17 AC 2 ms
6,940 KB
testcase_18 AC 2 ms
6,944 KB
testcase_19 AC 2 ms
6,944 KB
testcase_20 AC 2 ms
6,940 KB
testcase_21 AC 2 ms
6,944 KB
testcase_22 AC 2 ms
6,944 KB
testcase_23 AC 2 ms
6,940 KB
testcase_24 AC 2 ms
6,944 KB
testcase_25 AC 1 ms
6,940 KB
testcase_26 AC 2 ms
6,944 KB
testcase_27 AC 2 ms
6,940 KB
testcase_28 AC 2 ms
6,940 KB
testcase_29 AC 2 ms
6,940 KB
testcase_30 AC 2 ms
6,944 KB
testcase_31 AC 2 ms
6,944 KB
testcase_32 AC 2 ms
6,944 KB
testcase_33 AC 2 ms
6,940 KB
testcase_34 AC 2 ms
6,944 KB
testcase_35 AC 3 ms
6,944 KB
testcase_36 AC 3 ms
6,940 KB
testcase_37 AC 2 ms
6,944 KB
testcase_38 AC 3 ms
6,944 KB
testcase_39 AC 2 ms
6,944 KB
testcase_40 AC 2 ms
6,940 KB
testcase_41 AC 3 ms
6,940 KB
testcase_42 AC 3 ms
6,940 KB
testcase_43 AC 3 ms
6,940 KB
testcase_44 AC 2 ms
6,940 KB
testcase_45 AC 3 ms
6,940 KB
testcase_46 AC 3 ms
6,940 KB
testcase_47 AC 3 ms
6,940 KB
testcase_48 AC 3 ms
6,948 KB
testcase_49 AC 3 ms
6,944 KB
testcase_50 AC 3 ms
6,940 KB
testcase_51 AC 3 ms
6,944 KB
testcase_52 AC 3 ms
6,940 KB
testcase_53 AC 3 ms
6,940 KB
testcase_54 AC 3 ms
6,940 KB
testcase_55 WA -
testcase_56 AC 59 ms
7,680 KB
testcase_57 WA -
testcase_58 WA -
testcase_59 WA -
testcase_60 WA -
testcase_61 WA -
testcase_62 WA -
testcase_63 WA -
testcase_64 WA -
testcase_65 WA -
testcase_66 WA -
testcase_67 WA -
testcase_68 WA -
testcase_69 WA -
testcase_70 WA -
testcase_71 WA -
testcase_72 WA -
testcase_73 RE -
testcase_74 RE -
testcase_75 RE -
testcase_76 RE -
testcase_77 WA -
testcase_78 WA -
testcase_79 WA -
testcase_80 WA -
testcase_81 RE -
testcase_82 RE -
testcase_83 WA -
testcase_84 RE -
testcase_85 AC 9 ms
6,944 KB
testcase_86 RE -
testcase_87 RE -
testcase_88 WA -
testcase_89 WA -
testcase_90 WA -
testcase_91 WA -
権限があれば一括ダウンロードができます

ソースコード

diff #

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

// ConvexHullTrick in case of monotonic adding query
// monotonic adding query: isMinimum -> a1 >= a2 >= a3 ...
// isMonotonicQuery(x) ? O(N+Q) : O((N+Q)logN)
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 (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 (int i = 0; i < n; i++) {
    cin >> d[i];
  }
  vector<long long> dp(n+1, 0);
  ConvexHullTrick<long long> convexhulltrick(true);
  for (int 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 (int 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