# | Submission time | Handle | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
148116 | 2019-08-31T14:01:50 Z | WhipppedCream | Building Bridges (CEOI17_building) | C++17 | 84 ms | 9848 KB |
#include <bits/stdc++.h> #pragma GCC optimize ("O3") #pragma GCC target ("sse4") using namespace std; #define X first #define Y second #define pb push_back typedef pair<int, int> ii; typedef long long ll; struct Line { mutable ll m, b, p; bool operator < (const Line &o) const { return m< o.m; } bool operator < (ll o) const { return p< o; } }; struct LineContainer : multiset<Line, less<>> { const ll inf = LLONG_MAX; ll div(ll a, ll b) { return a/b - ((a^b)< 0 && a%b); } bool isect(iterator x, iterator y) { if(y == end()){ x->p = inf; return false; } if(x->m == y->m) x->p = x->b> y->b ? inf : -inf; else x->p = div(x->b - y->b, y->m - x->m); return x->p >= y->p; } void add(ll m, ll b) { auto z = insert({m, b, 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, y = erase(y)); } ll query(ll x) { assert(!empty()); auto l = *lower_bound(x); return l.m*x+l.b; } }; LineContainer foo; const int maxn = 1e5+5; ll h[maxn]; ll qs[maxn]; ll dp[maxn]; int n; int main() { scanf("%d", &n); for(int i = 1; i<= n; i++) scanf("%lld", h+i); for(int i = 1; i<= n; i++) { scanf("%lld", qs+i); qs[i] += qs[i-1]; } dp[1] = 0; foo.add(2*h[1], -dp[1]+qs[1]-h[1]*h[1]); for(int i = 2; i<= n; i++) { dp[i] = qs[i-1]+h[i]*h[i]-foo.query(h[i]); foo.add(2*h[i], -dp[i]+qs[i]-h[i]*h[i]); } printf("%lld\n", dp[n]); }
Compilation message
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
1 | Correct | 2 ms | 376 KB | Output is correct |
2 | Correct | 2 ms | 376 KB | Output is correct |
3 | Correct | 2 ms | 376 KB | Output is correct |
4 | Correct | 2 ms | 376 KB | Output is correct |
5 | Correct | 3 ms | 376 KB | Output is correct |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
1 | Correct | 62 ms | 3780 KB | Output is correct |
2 | Correct | 62 ms | 3704 KB | Output is correct |
3 | Correct | 63 ms | 3832 KB | Output is correct |
4 | Correct | 58 ms | 3576 KB | Output is correct |
5 | Correct | 56 ms | 4728 KB | Output is correct |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
1 | Correct | 2 ms | 376 KB | Output is correct |
2 | Correct | 2 ms | 376 KB | Output is correct |
3 | Correct | 2 ms | 376 KB | Output is correct |
4 | Correct | 2 ms | 376 KB | Output is correct |
5 | Correct | 3 ms | 376 KB | Output is correct |
6 | Correct | 62 ms | 3780 KB | Output is correct |
7 | Correct | 62 ms | 3704 KB | Output is correct |
8 | Correct | 63 ms | 3832 KB | Output is correct |
9 | Correct | 58 ms | 3576 KB | Output is correct |
10 | Correct | 56 ms | 4728 KB | Output is correct |
11 | Correct | 63 ms | 3832 KB | Output is correct |
12 | Correct | 65 ms | 3820 KB | Output is correct |
13 | Correct | 52 ms | 3704 KB | Output is correct |
14 | Correct | 64 ms | 3980 KB | Output is correct |
15 | Correct | 84 ms | 9848 KB | Output is correct |
16 | Correct | 56 ms | 4728 KB | Output is correct |
17 | Correct | 33 ms | 3704 KB | Output is correct |
18 | Correct | 33 ms | 3704 KB | Output is correct |