結果

問題 No.704 ゴミ拾い Medium
ユーザー goodbatongoodbaton
提出日時 2019-10-29 01:49:43
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 475 ms / 1,500 ms
コード長 3,289 bytes
コンパイル時間 1,554 ms
コンパイル使用メモリ 116,528 KB
実行使用メモリ 26,668 KB
最終ジャッジ日時 2023-10-12 23:14:59
合計ジャッジ時間 13,228 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 3 ms
7,576 KB
testcase_01 AC 2 ms
7,564 KB
testcase_02 AC 2 ms
5,524 KB
testcase_03 AC 3 ms
7,516 KB
testcase_04 AC 3 ms
7,592 KB
testcase_05 AC 2 ms
5,528 KB
testcase_06 AC 3 ms
5,516 KB
testcase_07 AC 2 ms
5,460 KB
testcase_08 AC 3 ms
7,588 KB
testcase_09 AC 2 ms
5,552 KB
testcase_10 AC 2 ms
5,524 KB
testcase_11 AC 2 ms
7,564 KB
testcase_12 AC 2 ms
5,616 KB
testcase_13 AC 2 ms
5,800 KB
testcase_14 AC 4 ms
7,736 KB
testcase_15 AC 4 ms
5,744 KB
testcase_16 AC 4 ms
7,712 KB
testcase_17 AC 4 ms
7,840 KB
testcase_18 AC 4 ms
5,888 KB
testcase_19 AC 4 ms
7,800 KB
testcase_20 AC 4 ms
5,976 KB
testcase_21 AC 4 ms
5,676 KB
testcase_22 AC 3 ms
5,824 KB
testcase_23 AC 3 ms
5,676 KB
testcase_24 AC 470 ms
25,740 KB
testcase_25 AC 465 ms
25,340 KB
testcase_26 AC 472 ms
25,508 KB
testcase_27 AC 458 ms
25,408 KB
testcase_28 AC 460 ms
26,224 KB
testcase_29 AC 471 ms
26,668 KB
testcase_30 AC 467 ms
25,468 KB
testcase_31 AC 466 ms
25,728 KB
testcase_32 AC 463 ms
25,580 KB
testcase_33 AC 475 ms
25,120 KB
testcase_34 AC 113 ms
16,764 KB
testcase_35 AC 111 ms
17,052 KB
testcase_36 AC 111 ms
16,656 KB
testcase_37 AC 111 ms
15,300 KB
testcase_38 AC 111 ms
15,844 KB
testcase_39 AC 112 ms
17,004 KB
testcase_40 AC 113 ms
17,284 KB
testcase_41 AC 112 ms
16,372 KB
testcase_42 AC 112 ms
15,720 KB
testcase_43 AC 111 ms
16,592 KB
testcase_44 AC 2 ms
5,744 KB
testcase_45 AC 2 ms
7,520 KB
testcase_46 AC 123 ms
16,276 KB
testcase_47 AC 124 ms
15,916 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cstring>

#include <iostream>
#include <complex>
#include <string>
#include <algorithm>
#include <numeric>
#include <vector>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <unordered_map>
#include <unordered_set>

#include <functional>
#include <cassert>

typedef long long ll;
using namespace std;

#ifndef LOCAL
#define debug(x) ;
#else
#define debug(x) cerr << __LINE__ << " : " << #x << " = " << (x) << endl;

template <typename T1, typename T2>
ostream &operator<<(ostream &out, const pair<T1, T2> &p) {
  out << "{" << p.first << ", " << p.second << "}";
  return out;
}

template <typename T>
ostream &operator<<(ostream &out, const vector<T> &v) {
  out << '{';
  for (const T &item : v) out << item << ", ";
  out << "\b\b}";
  return out;
}
#endif

#define mod 1000000007 //1e9+7(prime number)
#define INF 1000000000 //1e9
#define LLINF 2000000000000000000LL //2e18
#define SIZE 300010

template<typename T>
struct LiChaoTree {
  using Line = pair<T, T>; // first * x + second

  int segn2;
  vector<Line> data;
  vector<T> X;

  template<typename S>
  LiChaoTree(vector<S> pos) {
    int N = pos.size();
    for (segn2 = 1; segn2 < N; segn2 *= 2);
    data.assign(segn2 * 2, {0, INF});
    X.assign(segn2, INF);
    for (int i=0; i<pos.size(); i++)
      X[i] = pos[i];
  }

  inline T f(Line line, T x) {
    return line.first * x + line.second;
  }

  void addLine(int a, int b, Line line, int l = 0, int r = -1, int k = 0) {
    if (r == -1) r = segn2;
    int m = (l + r) / 2;

    if (r <= a || b <= l) return;
    if (l < a || b < r) {
      addLine(a, b, line, l, m, k*2+1);
      addLine(a, b, line, m, r, k*2+2);
      return;
    }
    if (data[k].second == INF) { //isNull
      data[k] = line;
      return;
    }

    bool fl = f(line, X[l])   < f(data[k], X[l]);
    bool fm = f(line, X[m])   < f(data[k], X[m]);
    bool fr = f(line, X[r-1]) < f(data[k], X[r-1]);

    if (fl == fr) {
      if (fl) data[k] = line;
      return;
    }
    if (fm) swap(data[k], line);

    if (fl ^ fm) addLine(a, b, line, l, m, 2*k+1);
    else addLine(a, b, line, m, r, 2*k+2);
  }

  void addLine(Line line) { addLine(0, segn2, line); }

  T query(int k) {
    T x = X[k], res = LLINF;
    k += segn2-1;

    while(1){
      if (data[k].second != INF) //isNotNull
        res = min(res, f(data[k], x));
      if (!k) break;
      k = (k - 1) / 2;
    }

    return res;
  }
};


int main(){
  int N, A[SIZE], X[SIZE], Y[SIZE];
  ll dp[SIZE];

  scanf("%d", &N);

  for (int i=0; i<N; i++) scanf("%d", A+i);
  for (int i=0; i<N; i++) scanf("%d", X+i);
  for (int i=0; i<N; i++) scanf("%d", Y+i);

  vector<ll> vec;
  map<ll, int> dic;
  for (int i=0; i<N; i++) vec.push_back(X[i]);
  for (int i=0; i<N; i++) vec.push_back(A[i]);
  sort(vec.begin(), vec.end());
  vec.erase(unique(vec.begin(), vec.end()), vec.end());

  debug(vec);

  LiChaoTree<ll> tree(vec);
  for (int i=0; i<vec.size(); i++)
    dic[vec[i]] = i;

  dp[0] = 0;
  for (int i=0; i<N; i++) {
    tree.addLine(0, dic[X[i]], {-1, Y[i] + dp[i] + X[i]});
    tree.addLine(dic[X[i]], vec.size(), {1, Y[i] + dp[i] - X[i]});

    dp[i+1] = tree.query(dic[A[i]]);
    debug(dp[i+1]);
  }

  cout << dp[N] << endl;

  return 0;
}
0