제출 #923796

#제출 시각아이디문제언어결과실행 시간메모리
923796KarootBuilding Bridges (CEOI17_building)C++17
100 / 100
38 ms11792 KiB
#include <iostream> #include <cmath> #include <unordered_map> #include <map> #include <set> #include <queue> #include <vector> #include <string> #include <iomanip> #include <algorithm> #include <climits> #define all(x) (x).begin(), (x).end() #define rall(x) (x).rbegin(), (x).rend() using namespace std; typedef long long ll; ll linf = 1e15+1; inline void scoobydoobydoo(){ ios::sync_with_stdio(false); ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); } struct Line { mutable ll k, m, p; bool operator<(const Line& o) const { return k < o.k; } bool operator<(ll x) const { return p < x; } }; struct LineContainer : multiset<Line, less<>> { // (for doubles, use inf = 1/.0, div(a,b) = a/b) static const ll inf = LLONG_MAX; ll div(ll a, ll b) { // floored division return a / b - ((a ^ b) < 0 && a % b); } bool isect(iterator x, iterator y) { if (y == end()) return x->p = inf, 0; if (x->k == y->k) x->p = x->m > y->m ? inf : -inf; else x->p = div(y->m - x->m, x->k - y->k); return x->p >= y->p; } void add(ll k, ll m) { auto z = insert({k, m, 0}), y = z++, x = y; while (isect(y, z)) z = erase(z); if (x != begin() && isect(--x, y)) isect(x, y = erase(y)); while ((y = x) != begin() && (--x)->p >= y->p) isect(x, erase(y)); } ll query(ll x) { if (empty())return -linf; auto l = *lower_bound(x); return l.k * x + l.m; } }; LineContainer LC; const int MAXN = 2e5+1; ll mem[MAXN]; ll pref[MAXN]; ll heights[MAXN]; int n; ll dp(){ mem[0] = 0; LC.add(2*heights[0], -pow(heights[0],2)+pref[0]); //cout << mem[0] << " "; for (int i = 1; i < n; i++){ mem[i] = pref[i-1]+pow(heights[i], 2)-LC.query(heights[i]); //cout << mem[i] << " "; LC.add(2*heights[i], -mem[i]-pow(heights[i], 2)+pref[i]); } //cout << endl; return mem[n-1]; } int main(){ scoobydoobydoo(); cin >> n; for (int i = 0; i < n; i++) cin >> heights[i]; for (int i = 0; i < n; i++){ cin >> pref[i]; pref[i] += (i == 0 ? 0 : pref[i-1]); } cout << dp() << endl; return 0; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...