답안 #1066587

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1066587 2024-08-20T02:48:13 Z vjudge1 Building Bridges (CEOI17_building) C++17
30 / 100
16 ms 4616 KB
#include <bits/stdc++.h>
#define ll long long
using namespace std;

const int N = 1e5 + 16;
const ll INF = 1e18;
int n;
ll h[N], w[N], prefix[N];

ll dp[N];

ll getPrefix(int i, int j) {
    if (i > j) {
        return 0;
    }
    return prefix[j] - prefix[i - 1];
}

void sub1() {
    for (int i = 2; i <= n; i++) {
        dp[i] = INF;
        for (int j = 1; j < i; j++) {
            dp[i] = min(dp[i], dp[j] + getPrefix(j + 1, i - 1) + (h[i] - h[j]) * (h[i] - h[j]));
        }
    }
    cout << dp[n];
}

struct geometry {
    ll x, y;
};

int hullSize;
geometry hull[N];

bool bad(geometry a, geometry b, geometry c) {
    return -1.0 * (b.y - a.y) / (b.x - a.x) >= -1.0 * (c.y - a.y) / (c.x - a.x);
}

ll get(int k) {
    int l = 0, r = hullSize - 1, mid;
    ll ans = hull[l].x * k + hull[l].y;
    while (l < r) {
        mid = l + (r - l) / 2;
        ll x = hull[mid].x * k + hull[mid].y;
        ll y = hull[mid + 1].x * k + hull[mid + 1].y;
        if (x > y) {
            l = mid + 1;
        }
        else {
            r = mid - 1;
        }
        ans = min({ans, x, y});
    }
    return ans;
}

void addLine(geometry k) {
    int l = 1, r = hullSize - 1, mid, id = hullSize;
    while (l <= r) {
        mid = l + (r - l) / 2;
        if (bad(hull[mid - 1], hull[mid], k)) {
            r = mid - 1;
            id = mid;
        }
        else {
            l = mid + 1;
        }
    }
    hullSize = id + 1;
    hull[id] = k;
}

void sub2() {
    addLine({-2 * h[1], dp[1] - prefix[1] + h[1] * h[1]});
    for (int i = 2; i <= n; i++) {
        dp[i] = get(h[i]) + prefix[i - 1] + h[i] * h[i];
        addLine({-2 * h[i], dp[i] - prefix[i] + h[i] * h[i]});
    }
    cout << dp[n];
}

int main() {
    ios_base :: sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);

    cin >> n;
    for (int i = 1; i <= n; i++) {
        cin >> h[i];
    }
    for (int i = 1; i <= n; i++) {
        cin >> w[i];
    }

    for (int i = 1; i <= n; i++) {
        prefix[i] = prefix[i - 1] + w[i];
    }
    if (n <= 5000) {
        sub1();
    }
    else {
        sub2();
    }

    return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 344 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
# 결과 실행 시간 메모리 Grader output
1 Correct 15 ms 4616 KB Output is correct
2 Incorrect 16 ms 4440 KB Output isn't correct
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 344 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 15 ms 4616 KB Output is correct
7 Incorrect 16 ms 4440 KB Output isn't correct
8 Halted 0 ms 0 KB -