답안 #258032

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
258032 2020-08-05T08:37:01 Z dolphingarlic Building Bridges (CEOI17_building) C++14
0 / 100
72 ms 4600 KB
#include <bits/stdc++.h>
#define FOR(i, x, y) for (int i = x; i < y; i++)
typedef long long ll;
using namespace std;

struct Line {
    ll m, c;
    ll operator()(ll x) { return m * x + c; }
} segtree[4000000];

void insert(Line seg, int node = 1, int l = 0, int r = 1000000) {
    if (l == r) {
        if (seg(l) <= segtree[node](l)) segtree[node] = seg;
    } else {
        int mid = (l + r) / 2;
        if (segtree[node].m < seg.m) swap(segtree[node], seg);
        if (segtree[node](mid) >= seg(mid)) {
            swap(segtree[node], seg);
            insert(seg, node * 2, l, mid);
        } else insert(seg, node * 2 + 1, mid + 1, r);
    }
}

ll query(ll x, int node = 1, int l = 0, int r = 1000000) {
    if (l == r) return segtree[node](x);
    int mid = (l + r) / 2;
    if (x < mid) return min(segtree[node](x), query(x, node * 2, l, mid));
    return min(segtree[node](x), query(x, node * 2 + 1, mid + 1, r));
}

ll h[100001], w[100001], tot = 0, dp[100001];

int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    int n;
    cin >> n;
    FOR(i, 1, n + 1) cin >> h[i];
    FOR(i, 1, n + 1) {
        cin >> w[i];
        tot += w[i];
    }

    dp[1] = -w[1];
    FOR(i, 2, n + 1) {
        insert({-2 * h[i - 1], dp[i - 1] + h[i - 1] * h[i - 1]});
        dp[i] = query(h[i]) - w[i] + h[i] * h[i];
    }

    cout << tot + dp[n];
    return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 384 KB Output is correct
2 Correct 1 ms 640 KB Output is correct
3 Correct 1 ms 1408 KB Output is correct
4 Correct 3 ms 2048 KB Output is correct
5 Incorrect 2 ms 1792 KB Output isn't correct
# 결과 실행 시간 메모리 Grader output
1 Correct 65 ms 3832 KB Output is correct
2 Correct 50 ms 3704 KB Output is correct
3 Correct 72 ms 3704 KB Output is correct
4 Correct 47 ms 3448 KB Output is correct
5 Incorrect 45 ms 4600 KB Output isn't correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 384 KB Output is correct
2 Correct 1 ms 640 KB Output is correct
3 Correct 1 ms 1408 KB Output is correct
4 Correct 3 ms 2048 KB Output is correct
5 Incorrect 2 ms 1792 KB Output isn't correct