Submission #158583

# Submission time Handle Problem Language Result Execution time Memory
158583 2019-10-18T02:43:48 Z PeppaPig Building Bridges (CEOI17_building) C++14
30 / 100
71 ms 8308 KB
#include <bits/stdc++.h>

#define long long long
#define pii pair<long, long>
#define x first
#define y second

using namespace std;

const int N = 1 << 17;

vector<int> coord;
pii t[N << 1];

long f(long x, pii l) { return l.x * x + l.y; }

void add(pii k, int p = 1, int l = 1, int r = coord.size()) {
    int m = (l + r) >> 1;
    int L = coord[l-1], M = coord[m-1], R = coord[r-1];
    bool lef = f(L, k) < f(L, t[p]);
    bool mid = f(M, k) < f(M, t[p]);
    if(mid) swap(k, t[p]);
    if(l == r) return;
    else if(lef != mid) add(k, p<<1, l, m);
    else add(k, p<<1|1, m+1, r);
}

long get(long x, int p = 1, int l = 1, int r = coord.size()) {
    if(l == r) return f(x, t[p]);
    int m = (l + r) >> 1;
    int L = coord[l-1], M = coord[m-1], R = coord[r-1];
    if(x <= M) return min(f(x, t[p]), get(x, p<<1, l, m));
    else return min(f(x, t[p]), get(x, p<<1|1, m+1, r));
}

int n;
long h[N], w[N], dp[N];

int main() {
    fill_n(t, N<<1, pii(1e9, 1e9));
    scanf("%d", &n);
    for(int i = 1; i <= n; i++) {
        scanf("%lld", h+i);
        coord.emplace_back(h[i]);
    }
    for(int i = 1; i <= n; i++) {
        scanf("%lld", w+i);
        w[i] += w[i-1];
    }
    sort(coord.begin(), coord.end());
    coord.resize(unique(coord.begin(), coord.end()) - coord.begin());

    add(pii(-2ll * h[1], h[1] * h[1] - w[1]));
    for(int i = 2; i <= n; i++) {
        dp[i] = get(h[i]) + h[i] * h[i] + w[i-1];
        add(pii(-2ll * h[i], h[i] * h[i] - w[i] + dp[i]));
    }
    printf("%lld\n", dp[n]);

    return 0;
}

Compilation message

building.cpp: In function 'void add(std::pair<long long int, long long int>, int, int, int)':
building.cpp:19:41: warning: unused variable 'R' [-Wunused-variable]
     int L = coord[l-1], M = coord[m-1], R = coord[r-1];
                                         ^
building.cpp: In function 'long long int get(long long int, int, int, int)':
building.cpp:31:9: warning: unused variable 'L' [-Wunused-variable]
     int L = coord[l-1], M = coord[m-1], R = coord[r-1];
         ^
building.cpp:31:41: warning: unused variable 'R' [-Wunused-variable]
     int L = coord[l-1], M = coord[m-1], R = coord[r-1];
                                         ^
building.cpp: In function 'int main()':
building.cpp:41:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
     scanf("%d", &n);
     ~~~~~^~~~~~~~~~
building.cpp:43:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
         scanf("%lld", h+i);
         ~~~~~^~~~~~~~~~~~~
building.cpp:47:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
         scanf("%lld", w+i);
         ~~~~~^~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 5 ms 4472 KB Output is correct
2 Correct 5 ms 4472 KB Output is correct
3 Correct 6 ms 4472 KB Output is correct
4 Correct 6 ms 4472 KB Output is correct
5 Correct 6 ms 4472 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 71 ms 8300 KB Output is correct
2 Correct 69 ms 8308 KB Output is correct
3 Correct 68 ms 8308 KB Output is correct
4 Correct 62 ms 8176 KB Output is correct
5 Incorrect 56 ms 8176 KB Output isn't correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 4472 KB Output is correct
2 Correct 5 ms 4472 KB Output is correct
3 Correct 6 ms 4472 KB Output is correct
4 Correct 6 ms 4472 KB Output is correct
5 Correct 6 ms 4472 KB Output is correct
6 Correct 71 ms 8300 KB Output is correct
7 Correct 69 ms 8308 KB Output is correct
8 Correct 68 ms 8308 KB Output is correct
9 Correct 62 ms 8176 KB Output is correct
10 Incorrect 56 ms 8176 KB Output isn't correct