답안 #976611

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
976611 2024-05-06T20:04:12 Z 0x34c Building Bridges (CEOI17_building) C++17
100 / 100
39 ms 9936 KB
#include <bits/stdc++.h>
#define ll long long
#define pii pair<int, int>
#define endl '\n'
#define int ll

using namespace std;

const int INF = 1e13;

struct Line {
	mutable ll k, m, p;
	bool operator<(const Line& o) const { return k < o.k; }
	bool operator<(ll x) const { return p < x; }
};

struct LineContainer : multiset<Line, less<>> {
	// (for doubles, use inf = 1/.0, div(a,b) = a/b)
	static const ll inf = LLONG_MAX;
	ll div(ll a, ll b) { // floored division
		return a / b - ((a ^ b) < 0 && a % b); }
	bool isect(iterator x, iterator y) {
		if (y == end()) return x->p = inf, 0;
		if (x->k == y->k) x->p = x->m > y->m ? inf : -inf;
		else x->p = div(y->m - x->m, x->k - y->k);
		return x->p >= y->p;
	}
	void add(ll k, ll m) {
        k *= -1; m *= -1;
		auto z = insert({k, m, 0}), y = z++, x = y;
		while (isect(y, z)) z = erase(z);
		if (x != begin() && isect(--x, y)) isect(x, y = erase(y));
		while ((y = x) != begin() && (--x)->p >= y->p)
			isect(x, erase(y));
	}
	ll query(ll x) {
		assert(!empty());
		auto l = *lower_bound(x);
		return -1*(l.k * x + l.m);
	}
};

signed main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);

    int N;
    cin >> N;
    
    vector<int> pillars(N);
    vector<int> psum(N + 1, 0);
    for(int i = 0; i < N; i++) {
        cin >> pillars[i];
    }

    for(int i = 1; i <= N; i++) {
        cin >> psum[i];
        psum[i] += psum[i - 1];
    }

    LineContainer cht;
    vector<int> dp(N);
    cht.add(-2*pillars[0], pillars[0]*pillars[0] - psum[1]);

    for(int i = 1; i < N; i++) {
        dp[i] = dp[0] + (pillars[i] - pillars[0])*(pillars[i] - pillars[0]) + psum[i] - psum[1];
        dp[i] = pillars[i]*pillars[i] + psum[i] + cht.query(pillars[i]);
        cht.add(-2*pillars[i], pillars[i]*pillars[i] - psum[i + 1] + dp[i]);
    }

    cout << dp[N - 1] << endl;
}
# 결과 실행 시간 메모리 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 480 KB Output is correct
5 Correct 1 ms 348 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 31 ms 3684 KB Output is correct
2 Correct 32 ms 3676 KB Output is correct
3 Correct 31 ms 3668 KB Output is correct
4 Correct 29 ms 3676 KB Output is correct
5 Correct 27 ms 4696 KB Output is correct
# 결과 실행 시간 메모리 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 480 KB Output is correct
5 Correct 1 ms 348 KB Output is correct
6 Correct 31 ms 3684 KB Output is correct
7 Correct 32 ms 3676 KB Output is correct
8 Correct 31 ms 3668 KB Output is correct
9 Correct 29 ms 3676 KB Output is correct
10 Correct 27 ms 4696 KB Output is correct
11 Correct 33 ms 3932 KB Output is correct
12 Correct 31 ms 3888 KB Output is correct
13 Correct 24 ms 3808 KB Output is correct
14 Correct 30 ms 3932 KB Output is correct
15 Correct 39 ms 9936 KB Output is correct
16 Correct 26 ms 4700 KB Output is correct
17 Correct 22 ms 3928 KB Output is correct
18 Correct 16 ms 3676 KB Output is correct