Submission #1117890

#TimeUsernameProblemLanguageResultExecution timeMemory
1117890vjudge1Building Bridges (CEOI17_building)C++17
100 / 100
46 ms22088 KiB
//#pragma GCC optimize("O3,unroll-loops") //#pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt") #include<bits/stdc++.h> #include <ext/pb_ds/assoc_container.hpp> #define ll long long #define F first #define S second #define ull unsigned long long #define db double #define ldb long double #define pb push_back #define pf push_front #define ppb pop_back #define ppf pop_front #define yes cout << "YES\n" #define no cout << "NO\n" #define ordered_set tree < ll, null_type, less < ll > , rb_tree_tag, tree_order_statistics_node_update > #define all(x) x.begin(), x.end() const int mod = 1e9 + 7; const int N = 500001; using namespace std; using namespace __gnu_pbds; ll n, m, a, b, c, p[N], f[N], dp[N], pr[N]; 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) { assert(!empty()); auto l = *lower_bound(x); return l.k * x + l.m; } }; signed main() { ios_base::sync_with_stdio(0); cin.tie(0); cin >> n; for (int i = 1; i <= n; i++){ cin >> p[i]; } for (int i = 1; i <= n; i++){ cin >> f[i]; pr[i] = f[i] + pr[i - 1]; } LineContainer cht; dp[1] = 0; cht.add(p[1], -(p[1] * p[1] + dp[1] - pr[1])); // cout << -(dp[1] - pr[1] + p[1] * p[1]) << '\n'; for (int i = 2; i <= n; i++){ dp[i] = (p[1] - p[i]) * (p[1] - p[i]) + (pr[i - 1] - pr[1]); dp[i] = min(p[i] * p[i] - cht.query(2 * p[i]) + pr[i - 1] , dp[i]); cht.add (p[i], -(p[i] * p[i] + dp[i] - pr[i])); // cout << dp[i] << ' '; } cout << dp[n]; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...