답안 #1066622

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1066622 2024-08-20T03:29:51 Z HiepVu217 Building Bridges (CEOI17_building) C++17
100 / 100
43 ms 9300 KB
#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 17, oo = 1e18;
int n, h[N];
long long s[N], ans;
int mode;
struct line
{
    long long a, b;
    mutable long double x;
    int operator < (const line& other) const
    {
        return (!mode ? a < other.a : x < other.x);
    }
};
struct convex: multiset <line>
{
    bool del (iterator u, iterator v)
    {
        if (v == end())
        {
            u -> x = oo;
            return 0;
        }
        if (u -> a == v -> a)
        {
            if (u -> b > v -> b)
            {
                u -> x = oo;
            }
            else
            {
                u -> x = -oo;
            }
        }
        else
        {
            u -> x = (long double)(u -> b - v -> b) / (v -> a - u -> a);
        }
        return (u -> x >= v -> x);
    }
    void add (line l)
    {
        auto w = insert(l);
        auto u = w, v = w; ++w;
        while (del(u, w))
        {
            w = erase(w);
        }
        if (u != begin() && del((--u), v))
        {
            v = erase(v);
            del(u, v);
        }
        while ((v = u) != begin() && del((--u), v))
        {
            v = erase(v);
            del(u, v);
        }
    }
    long long get (long long x)
    {
        if (empty()) return 0;
        mode = 1;
        auto l = *lower_bound({0, 0, x});
        mode = 0;
        return l.a * x + l.b;
    }
} cht;
int main()
{
    ios::sync_with_stdio(0);
    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 >> s[i];
        s[i] += s[i - 1];
    }
    cht.add ({2 * h[1], s[1] - 1LL * h[1] * h[1]});
    for (int i = 2; i <= n; ++i)
    {
        ans = -cht.get(h[i]) + 1LL * h[i] * h[i] + s[i - 1];
        cht.add ({h[i] * 2, -ans + s[i] - 1LL * h[i] * h[i]});
    }
    cout << ans;
}

Compilation message

building.cpp:3:30: warning: overflow in conversion from 'double' to 'int' changes value from '1.0e+18' to '2147483647' [-Woverflow]
    3 | const int N = 1e5 + 17, oo = 1e18;
      |                              ^~~~
building.cpp: In member function 'long long int convex::get(long long int)':
building.cpp:65:38: warning: narrowing conversion of 'x' from 'long long int' to 'long double' [-Wnarrowing]
   65 |         auto l = *lower_bound({0, 0, x});
      |                                      ^
# 결과 실행 시간 메모리 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 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 34 ms 1688 KB Output is correct
2 Correct 33 ms 1628 KB Output is correct
3 Correct 33 ms 1612 KB Output is correct
4 Correct 31 ms 1628 KB Output is correct
5 Correct 28 ms 3164 KB Output is correct
# 결과 실행 시간 메모리 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 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 34 ms 1688 KB Output is correct
7 Correct 33 ms 1628 KB Output is correct
8 Correct 33 ms 1612 KB Output is correct
9 Correct 31 ms 1628 KB Output is correct
10 Correct 28 ms 3164 KB Output is correct
11 Correct 31 ms 1628 KB Output is correct
12 Correct 33 ms 1628 KB Output is correct
13 Correct 24 ms 1628 KB Output is correct
14 Correct 33 ms 1628 KB Output is correct
15 Correct 43 ms 9300 KB Output is correct
16 Correct 28 ms 3164 KB Output is correct
17 Correct 18 ms 1624 KB Output is correct
18 Correct 14 ms 1628 KB Output is correct