結果

問題 No.703 ゴミ拾い Easy
ユーザー potoooooooopotoooooooo
提出日時 2018-07-16 03:54:09
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 2,502 bytes
コンパイル時間 1,510 ms
コンパイル使用メモリ 173,008 KB
実行使用メモリ 12,756 KB
最終ジャッジ日時 2023-08-07 21:06:00
合計ジャッジ時間 16,078 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 WA -
testcase_01 WA -
testcase_02 WA -
testcase_03 WA -
testcase_04 WA -
testcase_05 WA -
testcase_06 WA -
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
testcase_20 WA -
testcase_21 WA -
testcase_22 WA -
testcase_23 WA -
testcase_24 WA -
testcase_25 WA -
testcase_26 WA -
testcase_27 WA -
testcase_28 WA -
testcase_29 WA -
testcase_30 WA -
testcase_31 WA -
testcase_32 WA -
testcase_33 WA -
testcase_34 WA -
testcase_35 WA -
testcase_36 WA -
testcase_37 WA -
testcase_38 WA -
testcase_39 WA -
testcase_40 WA -
testcase_41 WA -
testcase_42 WA -
testcase_43 WA -
testcase_44 WA -
testcase_45 WA -
testcase_46 WA -
testcase_47 WA -
testcase_48 WA -
testcase_49 WA -
権限があれば一括ダウンロードができます

ソースコード

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]);
    cout << a[i] << " " << dp[i]+a[i]*a[i] << endl;
    dp[i+1] = convexhulltrick.solve(-x[i]) + x[i]*x[i] + y[i] * y[i];
  }
  cout << dp[n] << endl;
  return 0;
}
0