Submission #868294

# Submission time Handle Problem Language Result Execution time Memory
868294 2023-10-31T03:08:17 Z Truly_ Building Bridges (CEOI17_building) C++14
0 / 100
52 ms 65372 KB
#define nametask "bridgepol"
#include <bits/stdc++.h>
#define fi first
#define se second
#define mp make_pair
#define pb push_back
#define SZ(a) (int) a.size()
#define FOR(i, a, b) for (int i = a; i <= b; i++)
#define FOD(i, b, a) for (int i = b; i >= a; i--)
#define int long long

using namespace std;
typedef long long ll;
typedef pair <int, int> pi;
const int N = 1e5 + 5;
const int oo = 1e12 + 7;
const int MAX = 1e6;

struct line{
    int a, b;
    line(){
        a = b = oo;
    }
};

int n;
int h[N], w[N];
int s[N];
line t[4 * MAX + 5];

int calc(line y, int x){
    return (1LL * y.a * x + y.b);
}

void upd_it(int id, int l, int r, line y){
    int mid = (l + r) / 2;
    if (calc(t[id], l) <= calc(y, l) && calc(t[id], r) <= calc(y, r)) return;
    if (calc(t[id], l) >= calc(y, l) && calc(t[id], r) >= calc(y, r)){
        t[id] = y;
        return;
    }
    if (calc(t[id], l) <= calc(y, l) && calc(t[id], mid) <= calc(y, mid)){
        upd_it(id * 2 + 1, mid + 1, r, y);
        return;
    }
    if (calc(t[id], l) >= calc(y, l) && calc(t[id], mid) >= calc(y, mid)){
        upd_it(id * 2 + 1, mid + 1, r, t[id]);
        t[id] = y;
        return;
    }
    if (calc(t[id], mid + 1) <= calc(y, mid + 1) && calc(t[id], r) <= calc(y, r)){
        upd_it(id * 2, l, mid, y);
        return;
    }
    if (calc(t[id], mid + 1) >= calc(y, mid + 1) && calc(t[id], r) >= calc(y, r)){
        upd_it(id * 2, l, mid, t[id]);
        t[id] = y;
        return;
    }
    upd_it(id * 2, l, mid, y);
    upd_it(id * 2 + 1, mid + 1, r, y);
}

int get_it(int id, int l, int r, int u){
    if (l > u || r < u) return ((int)1e18);
    int mid = (l + r) / 2;
    int res = calc(t[id], u);
    if (l == r) return res;
    res = min(res, get_it(id * 2, l, mid, u));
    res = min(res, get_it(id * 2 + 1, mid + 1, r, u));
    return res;
}

void solve(){
    line tmp;
    tmp.a = -2 * h[1];
    tmp.b = -w[1] + 1LL * h[1] * h[1];
    upd_it(1, 1, MAX, tmp);
    FOR(i, 2, n - 1){
        int val = get_it(1, 1, MAX, h[i]) - w[i] + 1LL * h[i] * h[i];
        tmp.a = -2 * h[i];
        tmp.b = val + 1LL * h[i] * h[i];
        upd_it(1, 1, MAX, tmp);
    }
    cout << s[n] + get_it(1, 1, MAX, h[n]) - w[n] + 1LL * h[n] * h[n];
}

signed main()
{
    ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
    if (fopen(nametask".inp", "r")){
        freopen(nametask".inp", "r", stdin);
        freopen(nametask".out", "w", stdout);
    }
    cin >> n;
    FOR(i, 1, n) cin >> h[i];
    FOR(i, 1, n) cin >> w[i];
    FOR(i, 1, n) s[i] = s[i - 1] + w[i];
    solve();
    return 0;
}

Compilation message

building.cpp: In function 'int main()':
building.cpp:92:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   92 |         freopen(nametask".inp", "r", stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~
building.cpp:93:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   93 |         freopen(nametask".out", "w", stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 9 ms 64088 KB Output is correct
2 Correct 9 ms 64092 KB Output is correct
3 Incorrect 9 ms 64208 KB Output isn't correct
4 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 48 ms 65372 KB Output is correct
2 Correct 48 ms 65360 KB Output is correct
3 Correct 52 ms 65360 KB Output is correct
4 Correct 52 ms 65364 KB Output is correct
5 Incorrect 42 ms 65368 KB Output isn't correct
6 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 9 ms 64088 KB Output is correct
2 Correct 9 ms 64092 KB Output is correct
3 Incorrect 9 ms 64208 KB Output isn't correct
4 Halted 0 ms 0 KB -