結果

問題 No.2458 Line Up Charged Balls
ユーザー SSRSSSRS
提出日時 2023-09-01 21:58:38
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 211 ms / 2,000 ms
コード長 2,034 bytes
コンパイル時間 2,617 ms
コンパイル使用メモリ 212,692 KB
実行使用メモリ 16,108 KB
最終ジャッジ日時 2023-09-01 21:58:44
合計ジャッジ時間 6,300 ms
ジャッジサーバーID
(参考情報)
judge16 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 2 ms
4,380 KB
testcase_04 AC 1 ms
4,380 KB
testcase_05 AC 107 ms
7,780 KB
testcase_06 AC 108 ms
7,928 KB
testcase_07 AC 2 ms
4,380 KB
testcase_08 AC 1 ms
4,376 KB
testcase_09 AC 189 ms
15,204 KB
testcase_10 AC 47 ms
6,208 KB
testcase_11 AC 3 ms
4,380 KB
testcase_12 AC 3 ms
4,376 KB
testcase_13 AC 92 ms
9,416 KB
testcase_14 AC 99 ms
9,272 KB
testcase_15 AC 27 ms
5,152 KB
testcase_16 AC 82 ms
7,836 KB
testcase_17 AC 96 ms
7,792 KB
testcase_18 AC 91 ms
7,864 KB
testcase_19 AC 127 ms
7,748 KB
testcase_20 AC 111 ms
8,368 KB
testcase_21 AC 92 ms
8,796 KB
testcase_22 AC 91 ms
8,436 KB
testcase_23 AC 182 ms
11,896 KB
testcase_24 AC 129 ms
8,324 KB
testcase_25 AC 211 ms
15,780 KB
testcase_26 AC 151 ms
11,752 KB
testcase_27 AC 204 ms
16,108 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
const long long INF = 1000000000000000000;
template <typename T>
struct li_chao_tree{
  struct line{
    T a, b;
    line(): a(0), b(INF){
    }
    line(T a, T b): a(a), b(b){
    }
    T get(T x){
      return a * x + b;
    }
  };
  int N;
  vector<T> x;
  vector<line> ST;
  li_chao_tree(){
  }
  li_chao_tree(const vector<T> &x2){
    x = x2;
    sort(x.begin(), x.end());
    x.erase(unique(x.begin(), x.end()), x.end());
    int N2 = x.size();
    N = 1;
    while (N < N2){
      N *= 2;
    }
    x.resize(N);
    for (int i = N2; i < N; i++){
      x[i] = x[N2 - 1];
    }
    ST = vector<line>(N * 2 - 1);
  }
  void line_add(line L, int i, int l, int r){
    T la = L.get(x[l]);
    T lb = ST[i].get(x[l]);
    T ra = L.get(x[r - 1]);
    T rb = ST[i].get(x[r - 1]);
    if (la >= lb && ra >= rb){
      return;
    } else if (la <= lb && ra <= rb){
      ST[i] = L;
    } else {
      int m = (l + r) / 2;
      T ma = L.get(x[m]);
      T mb = ST[i].get(x[m]);
      if (ma < mb){
        swap(L, ST[i]);
        swap(la, lb);
        swap(ra, rb);
      }
      if (la < lb){
        line_add(L, i * 2 + 1, l, m);
      }
      if (ra < rb){
        line_add(L, i * 2 + 2, m, r);
      }
    }
  }
  void line_add(T a, T b){
    line_add(line(a, b), 0, 0, N);
  }
  T get(T x2){
    int p = lower_bound(x.begin(), x.end(), x2) - x.begin();
    p += N - 1;
    T ans = INF;
    ans = min(ans, ST[p].get(x2));
    while (p > 0){
      p = (p - 1) / 2;
      ans = min(ans, ST[p].get(x2));
    }
    return ans;
  }
};
int main(){
  int N;
  cin >> N;
  vector<long long> Q(N);
  for (int i = 0; i < N; i++){
    cin >> Q[i];
  }
  li_chao_tree<long long> LCT(Q);
  long long mn = Q[0], mx = Q[0];
  long long ans = -INF;
  for (int i = 1; i < N; i++){
    long long dp = -LCT.get(Q[i]);
    dp = max(dp, mn * Q[i]);
    dp = max(dp, mx * Q[i]);
    ans = max(ans, dp);
    LCT.line_add(-Q[i], -dp);
    mn = min(mn, Q[i]);
    mx = max(mx, Q[i]);
  }
  cout << ans << endl;
}
0