Submission #1014888

#TimeUsernameProblemLanguageResultExecution timeMemory
1014888ByeWorldBuilding Bridges (CEOI17_building)C++14
100 / 100
88 ms14192 KiB
#include <bits/stdc++.h> #pragma GCC optimize("O3", "unroll-loops") #define ll long long #define int long long #define pb push_back #define fi first #define se second #define lf (id<<1) #define rg ((id<<1)|1) #define md ((l+r)/2) #define ld long double using namespace std; typedef pair<int,int> pii; typedef pair<pii, int> ipii; const int MAXN = 1e5+10; const int MAXA = 1e9+10; const int INF = 1e18+10; const int INF2 = 1e18+10; const int LOG = 19; const int MOD = 1e9+7; const int SQRT = 450; const vector<int> NOL = {}; mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); const vector<int> dx = {1, -1, 0, 0}; const vector<int> dy = {0, 0, 1, -1}; void chmn(int &a, int b){ a = min(a, b); } int n; int h[MAXN], w[MAXN], pr[MAXN], dp[MAXN]; struct line { int m, c; int y(int x){ return m*x+c; } }; struct seg { seg *le, *ri; line val; int l, r, mid; void bd(int x, int y){ l = x; r = y; mid = md; val.m = 0; val.c = INF2; le = NULL; ri = NULL; } void ADD(line add){ if(add.c==INF2) return; if(val.y(mid) > add.y(mid)) swap(val, add); if(val.m == add.m) return; // kalah if(val.m < add.m){ if(le==NULL){ le = new seg(); le->bd(l, mid); } le->ADD(add); return; } if(ri==NULL){ ri = new seg(); ri->bd(mid+1, r); } ri->ADD(add); return; } void upd(int x, int y, line add){ if(x<=l && r<=y){ ADD(add); return; } if(r<x || y<l) return; if(le==NULL){ le = new seg(); le->bd(l, mid); } le->upd(x, y, add); if(ri==NULL){ ri = new seg(); ri->bd(mid+1, r); } ri->upd(x, y, add); } int que(int x){ int te = val.y(x); if(x < mid){ if(le!=NULL) chmn(te, le->que(x)); } else { if(ri!=NULL) chmn(te, ri->que(x)); } return te; } } *A; signed main(){ cin >> n; for(int i=1; i<=n; i++) cin >> h[i]; for(int i=1; i<=n; i++){ cin >> w[i]; pr[i] = pr[i-1]+w[i]; } A = new seg(); A->bd(-MAXA, MAXA); dp[1] = 0; int i = 1; line nw; nw.m = -2*h[i]; nw.c = dp[i] - pr[i] + h[i]*h[i]; // cout << "pp\n"; A->upd(-MAXA, MAXA, nw); for(int i=2; i<=n; i++){ dp[i] = h[i]*h[i] + pr[i-1]; dp[i] += A->que(h[i]); line nw; nw.m = -2*h[i]; nw.c = dp[i] - pr[i] + h[i]*h[i]; A->upd(-MAXA, MAXA, nw); // cout << i<< ' ' << dp[i] << " pp\n"; // for(int j=1; j<=i-1; j++){ // int te = dp[j] + pr[i-1]-pr[j] + (h[j]-h[i])*(h[j]-h[i]); // chmn(dp[i], te); // } } cout << dp[n] << '\n'; } /* Rumus: dp[i] = min: (dp[j]-pr[j]+hj^2 + hi -2hj) + hi^2 + pr[i-1] ( c x m ) + k m gk monotonic -> dynamic CHT 6 3 8 7 1 6 6 0 -1 9 1 2 0 ->17 */
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...