# | 제출 시각 | 아이디 | 문제 | 언어 | 결과 | 실행 시간 | 메모리 |
---|---|---|---|---|---|---|---|
63156 | 2018-08-01T01:28:59 Z | khsoo01 | Building Bridges (CEOI17_building) | C++11 | 206 ms | 17336 KB |
#include<bits/stdc++.h> #define X first #define Y second using namespace std; typedef long long ll; typedef pair<ll,ll> pll; const ll N = 100005, L = 18, inf = 1e18; ll n, def, h[N], a[N], d[N]; ll val (pll &P, ll V) {return P.X*V+P.Y;} bool valid (pll &A, pll &B, pll &C) { return (B.Y-A.Y) * (B.X-C.X) < (C.Y-B.Y) * (A.X-B.X); } struct hull { vector<pll> v; void upd (pll P) { for(ll i=v.size();i--;) { if(v[i].X != P.X) break; else v.pop_back(); } for(ll i=v.size();i-->1;) { if(valid(v[i-1], v[i], P)) break; else v.pop_back(); } v.push_back(P); } ll get (ll P) { if(v.empty()) return inf; ll S = 0, E = (int)v.size()-1; while(S<E) { ll M = (S+E)/2; val(v[M], P) < val(v[M+1], P) ? E = M : S = M+1; } return val(v[S], P); } } x[L]; void Merge (hull &A, hull &B) { vector<pll> V; for(int i=0,j=0;i<A.v.size()||j<B.v.size();) { bool flag = false; if(i == A.v.size()) flag = true; else if(j == B.v.size()) flag = false; else if(A.v[i] < B.v[j]) flag = true; if(flag) {V.push_back(B.v[j]); j++;} else {V.push_back(A.v[i]); i++;} } A.v.clear(); B.v.clear(); for(auto &T : V) A.upd(T); } int main() { scanf("%lld",&n); for(ll i=1;i<=n;i++) scanf("%lld",&h[i]); for(ll i=1;i<=n;i++) {scanf("%lld",&a[i]); def += a[i];} x[0].upd(pll(-2*h[1], h[1]*h[1]-a[1])); for(ll i=2;i<=n;i++) { d[i] = inf; for(ll j=0;j<L;j++) { d[i] = min(d[i], x[j].get(h[i])-a[i]+h[i]*h[i]); } hull tmp; tmp.upd(pll(-2*h[i], h[i]*h[i]+d[i])); for(ll j=0;j<L;j++) { if(x[j].v.empty()) {swap(x[j], tmp); break;} else Merge(tmp, x[j]); } } printf("%lld\n",d[n]+def); }
Compilation message
# | 결과 | 실행 시간 | 메모리 | Grader output |
---|---|---|---|---|
1 | Correct | 2 ms | 376 KB | Output is correct |
2 | Correct | 2 ms | 496 KB | Output is correct |
3 | Correct | 2 ms | 496 KB | Output is correct |
4 | Correct | 4 ms | 532 KB | Output is correct |
5 | Correct | 4 ms | 628 KB | Output is correct |
# | 결과 | 실행 시간 | 메모리 | Grader output |
---|---|---|---|---|
1 | Correct | 183 ms | 4436 KB | Output is correct |
2 | Correct | 160 ms | 5404 KB | Output is correct |
3 | Correct | 206 ms | 6600 KB | Output is correct |
4 | Correct | 158 ms | 7240 KB | Output is correct |
5 | Correct | 95 ms | 9880 KB | Output is correct |
# | 결과 | 실행 시간 | 메모리 | Grader output |
---|---|---|---|---|
1 | Correct | 2 ms | 376 KB | Output is correct |
2 | Correct | 2 ms | 496 KB | Output is correct |
3 | Correct | 2 ms | 496 KB | Output is correct |
4 | Correct | 4 ms | 532 KB | Output is correct |
5 | Correct | 4 ms | 628 KB | Output is correct |
6 | Correct | 183 ms | 4436 KB | Output is correct |
7 | Correct | 160 ms | 5404 KB | Output is correct |
8 | Correct | 206 ms | 6600 KB | Output is correct |
9 | Correct | 158 ms | 7240 KB | Output is correct |
10 | Correct | 95 ms | 9880 KB | Output is correct |
11 | Correct | 164 ms | 9880 KB | Output is correct |
12 | Correct | 151 ms | 10516 KB | Output is correct |
13 | Correct | 94 ms | 11288 KB | Output is correct |
14 | Correct | 201 ms | 12736 KB | Output is correct |
15 | Correct | 117 ms | 17336 KB | Output is correct |
16 | Correct | 93 ms | 17336 KB | Output is correct |
17 | Correct | 54 ms | 17336 KB | Output is correct |
18 | Correct | 52 ms | 17336 KB | Output is correct |