답안 #1020662

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1020662 2024-07-12T08:09:11 Z NValchanov Building Bridges (CEOI17_building) C++17
30 / 100
106 ms 67156 KB
#include <bits/stdc++.h>

#define endl '\n'

using namespace std;

typedef long long ll;

const ll MAXN = 1e5 + 10;
const ll MAXH = 1e6 + 10;
const ll MAXW = 1e6 + 10;
const ll INF = 1e9 + 10;

ll n;
ll w[MAXN];
ll h[MAXN];
ll prefw[MAXN];
ll dp[MAXN];

struct lichao
{
    struct line
    {
        ll a, b;
        line()
        {
            a = 0;
            b = 0;
        }
        line(ll _a, ll _b)
        {
            a = _a;
            b = _b;
        }
        ll get_val(ll x)
        {
            return a * x + b;
        }
    };

    line tree[4 * MAXH];

    void build(ll left, ll right, ll idx)
    {
        tree[idx] = {0, INF};

        if(left == right)
            return;

        ll mid = left + (right - left) / 2;
        build(left, mid, 2 * idx);
        build(mid + 1, right, 2 * idx + 1);
    }

    void insert(ll left, ll right, ll idx, line l)
    {
        if(left == right)
        {
            if(l.get_val(left) < tree[idx].get_val(left))
                tree[idx] = l;

            return;
        }

        ll mid = left + (right - left) / 2;
        if(tree[idx].a < l.a)
            swap(tree[idx], l);

        if(tree[idx].get_val(mid) > l.get_val(mid))
        {
            swap(tree[idx], l);
            insert(left, mid, 2 * idx, l);
        }
        else insert(mid + 1, right, 2 * idx + 1, l);
    }

    ll query(ll left, ll right, ll idx, ll x)
    {
        if(left == right)
            return tree[idx].get_val(x);

        ll mid = left + (right - left) / 2;

        if(x < mid)
            return min(tree[idx].get_val(x), query(left, mid, 2 * idx, x));
        
        return min(tree[idx].get_val(x), query(mid + 1, right, 2 * idx + 1, x));
    }

    void build()
    {
        build(1, MAXH, 1);
    }

    void insert(ll a, ll b)
    {
        insert(1, MAXH, 1, line(a, b));
    }

    ll query(ll x)
    {
        return query(1, MAXH, 1, x);
    }
};

lichao li;

void read()
{
    cin >> n;
    for(ll i = 1; i <= n; i++)
    {
        cin >> h[i];
    }
    for(ll i = 1; i <= n; i++)
    {
        cin >> w[i];
        prefw[i] = prefw[i - 1] + w[i];
    }
}

void solve()
{
    li.build();

    dp[1] = 0;
    li.insert(-2 * h[1], h[1] * h[1] + dp[1] - prefw[1]);

    for(ll i = 2; i <= n; i++)
    {
        dp[i] = li.query(h[i]) + prefw[i - 1] + h[i] * h[i];

        li.insert(-2 * h[i], h[i] * h[i] + dp[i] - prefw[i]);
    }

    cout << dp[n] << endl;
}

int main()
{
    read();
    solve();

    return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 29 ms 63024 KB Output is correct
2 Correct 27 ms 62932 KB Output is correct
3 Correct 27 ms 63064 KB Output is correct
4 Correct 32 ms 62868 KB Output is correct
5 Correct 30 ms 62916 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 75 ms 67152 KB Output is correct
2 Correct 93 ms 67028 KB Output is correct
3 Correct 84 ms 67156 KB Output is correct
4 Correct 72 ms 67024 KB Output is correct
5 Incorrect 106 ms 66908 KB Output isn't correct
6 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 29 ms 63024 KB Output is correct
2 Correct 27 ms 62932 KB Output is correct
3 Correct 27 ms 63064 KB Output is correct
4 Correct 32 ms 62868 KB Output is correct
5 Correct 30 ms 62916 KB Output is correct
6 Correct 75 ms 67152 KB Output is correct
7 Correct 93 ms 67028 KB Output is correct
8 Correct 84 ms 67156 KB Output is correct
9 Correct 72 ms 67024 KB Output is correct
10 Incorrect 106 ms 66908 KB Output isn't correct
11 Halted 0 ms 0 KB -