제출 #543238

#제출 시각아이디문제언어결과실행 시간메모리
543238fcwBuilding Bridges (CEOI17_building)C++17
100 / 100
51 ms10588 KiB
#include <bits/stdc++.h> #define st first #define nd second using lint = int64_t; constexpr int mod = int(1e9) + 7; constexpr int inf = 0x3f3f3f3f; constexpr int ninf = 0xcfcfcfcf; constexpr lint linf = 0x3f3f3f3f3f3f3f3f; const long double pi = acosl(-1.0); // Returns -1 if a < b, 0 if a = b and 1 if a > b. int cmp_double(double a, double b = 0, double eps = 1e-9) { return a + eps > b ? b + eps > a ? 0 : 1 : -1; } using namespace std; struct Line { mutable lint k, m, p; bool operator<(const Line& o) const { return k < o.k; } bool operator<(lint x) const { return p < x; } }; struct LineContainer : multiset<Line, less<>> { // (for doubles, use inf = 1/.0, div(a,b) = a/b) static const lint inf = LLONG_MAX; lint div(lint a, lint b) { // floored division return a / b - ((a ^ b) < 0 && a % b); } bool isect(iterator x, iterator y) { if (y == end()) { x->p = inf; return false; } 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(lint k, lint 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)); } lint query(lint x) { assert(!empty()); auto l = *lower_bound(x); return l.k * x + l.m; } }; int main() { cin.tie(nullptr)->sync_with_stdio(false); int n; cin>>n; vector<lint>h(n), w(n); for(int i=0;i<n;i++) cin>>h[i]; for(int i=0;i<n;i++) cin>>w[i]; vector<lint>pref(n+1); for(int i=0;i<n;i++) pref[i+1] = pref[i] + w[i]; LineContainer g; g.add(2 * h[0], -h[0] * h[0] + pref[1]); vector<lint>dp(n); for(int i=1;i<n;i++){ dp[i] = h[i] * h[i] + pref[i] - g.query(h[i]); g.add(2 * h[i], -h[i] * h[i] + pref[i+1] - dp[i]); } cout<<dp[n-1]<<"\n"; return 0; } /* [ ]Leu o problema certo??? [ ]Ver se precisa de long long [ ]Viu o limite dos fors (é n? é m?) [ ]Tamanho do vetor, será que é 2e5 em vez de 1e5?? [ ]Testar sample [ ]Testar casos de borda [ ]1LL no 1LL << i [ ]Testar mod (é 1e9+7, mesmo?, será que o mod não ficou negativo?) */
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...