답안 #600262

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
600262 2022-07-20T15:48:48 Z Shin Building Bridges (CEOI17_building) C++14
0 / 100
79 ms 3520 KB
#include <bits/stdc++.h>
#define fi first
#define se second
#define mp make_pair
#define all(x) x.begin(), x.end()

using namespace std;
template <class X, class Y> bool minimize(X &a, Y b) {
    if (a > b) return a = b, true;
    return false;
}
template <class X, class Y> bool maximize(X &a, Y b) {
    if (a < b) return a = b, true;
    return false;
}


struct line_t {
  bool t;
  long long in;
  int a; long long b;
  line_t(bool _t = 0, long long _in = 0, int _m = 0, long long _c = 0) : t(_t), in(_in), a(_m), b(_c) {}
  long long f(int x) {
    return 1LL * a * x + b;
  }
  bool operator < (const line_t &oth) const {
    if (t || oth.t) {
      return in < oth.in;
    }
    return a > oth.a;
  }
};

struct ConvexHullTrick {
  set<line_t> lns;
  
  long long cdiv(long long a, long long b) {
    return a / b - ((a ^ b) < 0 && a % b);
  }

  long long inter(line_t ln1, line_t ln2) {
    return cdiv(ln1.b - ln2.b, ln2.a - ln1.a);
  }

  bool is_keep(set<line_t>::iterator it) {
    if (it == lns.begin() || next(it) == lns.end()) {
      return true;
    }
    return inter(*prev(it), *it) <= inter(*prev(it), *next(it));
  }

  void upd(set<line_t>::iterator it) {
    if (it == lns.begin()) {
      return;
    }
    line_t tmp = *it;
    tmp.in = inter(*prev(it), *it);
    lns.insert(lns.erase(it), tmp);
  }

  void add(int a, long long b) {
    auto it = lns.lower_bound(line_t(0, 0, a, b));
    if (it != lns.end() && it -> a == a) {
      if (it -> b <= b) {
        return;
      }
      lns.erase(it);
    }
    lns.insert(line_t(0, 0, a, b));
    it = lns.lower_bound(line_t(0, 0, a, b));
    if (!is_keep(it)) {
      lns.erase(it);
      return;
    }
    while (it != lns.begin() && !is_keep(prev(it))) {
      lns.erase(prev(it));
    }
    while (it != lns.end() && next(it) != lns.end() && !is_keep(next(it))) {
      lns.erase(next(it));
    }
    if (it != lns.end() && next(it) != lns.end()) {
      upd(next(it));
    }
    upd(it);
  }

  long long get(int x) {
    assert(lns.size() > 0);
    line_t res = *prev(lns.upper_bound(line_t(1, x, 0, 0)));
    return res.f(x);
  }
} cht;

const int N = 1e5 + 7;
int n;
int a[N];
long long dp[N];
long long pref[N];

signed main() {
  cin.tie(0)->sync_with_stdio(0);
  cin >> n;
  for (int i = 1; i <= n; i ++) {
    cin >> a[i];
  }
  for (int i = 1; i <= n; i ++) {
    int x; cin >> x;
    pref[i] = pref[i - 1] + x;
  }
  #define SQR(x) 1LL * (x) * (x)
  for (int i = 2; i <= n; i ++) {
    cht.add(-2 * a[i - 1], dp[i - 1] + SQR(a[i - 1]) - pref[i - 1]);
    dp[i] = cht.get(a[i]) + SQR(a[i]) + pref[i - 1];
  }
  cout << dp[n];
  return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Incorrect 1 ms 340 KB Output isn't correct
5 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 73 ms 3288 KB Output is correct
2 Correct 79 ms 3520 KB Output is correct
3 Incorrect 76 ms 3276 KB Output isn't correct
4 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Incorrect 1 ms 340 KB Output isn't correct
5 Halted 0 ms 0 KB -