Submission #1014888

# Submission time Handle Problem Language Result Execution time Memory
1014888 2024-07-05T16:39:28 Z ByeWorld Building Bridges (CEOI17_building) C++14
100 / 100
88 ms 14192 KB
#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 time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 1 ms 348 KB Output is correct
5 Correct 1 ms 348 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 72 ms 10024 KB Output is correct
2 Correct 69 ms 10068 KB Output is correct
3 Correct 62 ms 9812 KB Output is correct
4 Correct 64 ms 8304 KB Output is correct
5 Correct 46 ms 12624 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 1 ms 348 KB Output is correct
5 Correct 1 ms 348 KB Output is correct
6 Correct 72 ms 10024 KB Output is correct
7 Correct 69 ms 10068 KB Output is correct
8 Correct 62 ms 9812 KB Output is correct
9 Correct 64 ms 8304 KB Output is correct
10 Correct 46 ms 12624 KB Output is correct
11 Correct 88 ms 13396 KB Output is correct
12 Correct 87 ms 13708 KB Output is correct
13 Correct 78 ms 12880 KB Output is correct
14 Correct 76 ms 14192 KB Output is correct
15 Correct 42 ms 13656 KB Output is correct
16 Correct 42 ms 13392 KB Output is correct
17 Correct 35 ms 4432 KB Output is correct
18 Correct 31 ms 4388 KB Output is correct