Submission #161616

# Submission time Handle Problem Language Result Execution time Memory
161616 2019-11-03T10:01:56 Z Akashi Building Bridges (CEOI17_building) C++14
100 / 100
92 ms 7800 KB
#include <bits/stdc++.h>
using namespace std;

const int N = 1e6 + 5;

int n;
int h[N];
long long w[N];
long long d[N];

struct node{
    long long a, b;
    node *left, *right;

    node(){
        a = b = 0;
        left = right = NULL;
    }
};

node *arb;
void update(long long a, long long b, int st = 1, int dr = 1e6, node *nod = arb){
    if(st == dr){
        if(a + 1LL * b * st < nod->a + 1LL * nod->b * st){
            nod->a = a; nod->b = b;
        }
        return ;
    }

    int mij = (st + dr) / 2;
    if(a + 1LL * b * mij < nod->a + 1LL * nod->b * mij) swap(a, nod->a), swap(b, nod->b);

    if(b == nod->b && a >= nod->a) return ;

    if(b < nod->b){
        if(nod->right == NULL){
            nod->right = new node;
            nod->right->a = a; nod->right->b = b;
            return ;
        }
        update(a, b, mij + 1, dr, nod->right);
    }
    else{
        if(nod->left == NULL){
            nod->left = new node;
            nod->left->a = a; nod->left->b = b;
        }
        update(a, b, st, mij, nod->left);
    }
}

void query(long long &val, long long x, int st = 1, int dr = 1e6, node *nod = arb){
    if(st > dr) return ;

    int mij = (st + dr) / 2;

    val = min(val, nod->a + 1LL * x * nod->b);

    if(x <= mij){
        if(nod->left == NULL) return ;
        query(val, x, st, mij, nod->left);
    }
    else{
        if(nod->right == NULL) return ;
        query(val, x, mij + 1, dr, nod->right);
    }
}

int main()
{
//    freopen("1.in", "r", stdin);

    scanf("%d", &n);
    for(int i = 1; i <= n ; ++i)
        scanf("%d", &h[i]), d[i] = -1e18;
    for(int i = 1; i <= n ; ++i)
        scanf("%lld", &w[i]);

    d[1] = 0;
    arb = new node;
    arb->a = 1LL * h[1] * h[1] - w[1]; arb->b = -2LL * h[1];

    long long aux;
    for(int i = 2; i <= n ; ++i){
        w[i] += w[i - 1];

        aux = 1e18;
        query(aux, h[i]);

        d[i] = 1LL * h[i] * h[i] + w[i - 1] + aux;

        update(1LL * h[i] * h[i] + d[i] - w[i], -2LL * h[i]);
    }

    printf("%lld", d[n]);

    return 0;
}

Compilation message

building.cpp: In function 'int main()':
building.cpp:73:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
     scanf("%d", &n);
     ~~~~~^~~~~~~~~~
building.cpp:75:27: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
         scanf("%d", &h[i]), d[i] = -1e18;
         ~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~
building.cpp:77: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 2 ms 376 KB Output is correct
2 Correct 2 ms 376 KB Output is correct
3 Correct 3 ms 376 KB Output is correct
4 Correct 3 ms 372 KB Output is correct
5 Correct 3 ms 376 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 68 ms 4088 KB Output is correct
2 Correct 65 ms 4088 KB Output is correct
3 Correct 64 ms 4060 KB Output is correct
4 Correct 55 ms 3416 KB Output is correct
5 Correct 47 ms 6904 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 376 KB Output is correct
2 Correct 2 ms 376 KB Output is correct
3 Correct 3 ms 376 KB Output is correct
4 Correct 3 ms 372 KB Output is correct
5 Correct 3 ms 376 KB Output is correct
6 Correct 68 ms 4088 KB Output is correct
7 Correct 65 ms 4088 KB Output is correct
8 Correct 64 ms 4060 KB Output is correct
9 Correct 55 ms 3416 KB Output is correct
10 Correct 47 ms 6904 KB Output is correct
11 Correct 92 ms 7556 KB Output is correct
12 Correct 83 ms 7376 KB Output is correct
13 Correct 62 ms 4216 KB Output is correct
14 Correct 87 ms 7416 KB Output is correct
15 Correct 48 ms 7800 KB Output is correct
16 Correct 51 ms 6900 KB Output is correct
17 Correct 27 ms 3448 KB Output is correct
18 Correct 27 ms 3320 KB Output is correct