結果

問題 No.703 ゴミ拾い Easy
ユーザー potoooooooopotoooooooo
提出日時 2018-07-16 03:55:22
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 2,450 bytes
コンパイル時間 3,130 ms
コンパイル使用メモリ 170,840 KB
実行使用メモリ 12,656 KB
最終ジャッジ日時 2023-08-07 21:09:20
合計ジャッジ時間 7,248 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 1 ms
4,380 KB
testcase_04 AC 1 ms
4,376 KB
testcase_05 AC 2 ms
4,376 KB
testcase_06 AC 2 ms
4,384 KB
testcase_07 AC 1 ms
4,376 KB
testcase_08 AC 1 ms
4,376 KB
testcase_09 AC 1 ms
4,380 KB
testcase_10 AC 2 ms
4,380 KB
testcase_11 AC 1 ms
4,380 KB
testcase_12 AC 1 ms
4,376 KB
testcase_13 AC 2 ms
4,380 KB
testcase_14 AC 2 ms
4,380 KB
testcase_15 AC 2 ms
4,376 KB
testcase_16 AC 2 ms
4,380 KB
testcase_17 AC 2 ms
4,376 KB
testcase_18 AC 2 ms
4,384 KB
testcase_19 AC 2 ms
4,380 KB
testcase_20 AC 2 ms
4,380 KB
testcase_21 AC 1 ms
4,380 KB
testcase_22 AC 2 ms
4,380 KB
testcase_23 AC 2 ms
4,376 KB
testcase_24 AC 92 ms
12,388 KB
testcase_25 AC 91 ms
12,464 KB
testcase_26 AC 92 ms
12,576 KB
testcase_27 AC 93 ms
12,516 KB
testcase_28 AC 92 ms
12,484 KB
testcase_29 AC 93 ms
12,448 KB
testcase_30 AC 92 ms
12,512 KB
testcase_31 AC 92 ms
12,380 KB
testcase_32 AC 93 ms
12,508 KB
testcase_33 AC 92 ms
12,448 KB
testcase_34 WA -
testcase_35 AC 81 ms
12,536 KB
testcase_36 AC 81 ms
12,528 KB
testcase_37 WA -
testcase_38 WA -
testcase_39 AC 82 ms
12,372 KB
testcase_40 AC 82 ms
12,440 KB
testcase_41 WA -
testcase_42 WA -
testcase_43 WA -
testcase_44 AC 1 ms
4,380 KB
testcase_45 AC 1 ms
4,376 KB
testcase_46 AC 93 ms
12,488 KB
testcase_47 AC 93 ms
12,616 KB
testcase_48 AC 2 ms
4,380 KB
testcase_49 AC 2 ms
4,376 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();
      if (size >= 1 && lines[size-1].a == line.a){
        if (!compare(lines[size-1].b, line.b)) return;
      }
      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(){
  cin.tie(0);
  ios::sync_with_stdio(false);
  int n; cin >> n;
  vector<long long> a(n), x(n), y(n);
  for (int i = 0; i < n; i++) {
    cin >> a[n-i-1];
  }
  for (int i = 0; i < n; i++) {
    cin >> x[n-i-1];
  }
  for (int i = 0; i < n; i++) {
    cin >> y[n-i-1];
  }
  ConvexHullTrick<long long> convexhulltrick(true);
  vector<long long> dp(n+1, 0);
  for (int i = 0; i < n; i++) {
    convexhulltrick.add(2*a[i], dp[i]+a[i]*a[i]);
    dp[i+1] = convexhulltrick.solve(-x[i]) + x[i]*x[i] + y[i] * y[i];
  }
  cout << dp[n] << endl;
  return 0;
}
0