Submission #433490

# Submission time Handle Problem Language Result Execution time Memory
433490 2021-06-19T21:46:07 Z JovanK26 Building Bridges (CEOI17_building) C++14
30 / 100
87 ms 65476 KB
#include <bits/stdc++.h>

using namespace std;
int h[100001];
int w[100001];
long long f[100001];
struct line
{
    long long k,n;
};
long long get(line a,long long x)
{
    return a.k*x+a.n;
}
line tree[4000005];
void add_line(line nw,int start=0,long long l=0,long long r=1000001)
{
    long long m=(l+r)/2;
    bool left=get(nw,l)<get(tree[start],l);
    bool mid=get(nw,m)<get(tree[start],m);
    if(mid)
    {
        swap(tree[start],nw);
    }
    if(r-l==1)
    {
        return;
    }
    else if(left!=mid)
    {
        add_line(nw,2*start+1,l,m);
    }
    else
    {
        add_line(nw,2*start+2,m,r);
    }
}
long long query(int x,int start=0,long long l=0,long long r=1000001)
{
    long long m=(l+r)/2;
    if(r-l==1)
    {
        return get(tree[start],x);
    }
    else if(x<m)
    {
        return min(get(tree[start],x),query(x,2*start+1,l,m));
    }
    else
    {
        return min(get(tree[start],x),query(x,2*start+2,m,r));
    }
}
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    int n;
    cin >> n;
    for(int i=0;i<n;i++)
    {
        cin >> h[i];
        f[i]=1000000000000000000;
    }
    long long tot=0;
    for(int i=0;i<n;i++)
    {
        cin >> w[i];
        tot+=w[i];
    }
    for(int i=0;i<4000005;i++)
    {
        tree[i]={0,1000000000000000000};
    }
    f[0]=-w[0];
    add_line({-2*h[0],f[0]+h[0]*h[0]});
    for(int i=1;i<n;i++)
    {
        f[i]=query(h[i])+h[i]*h[i]-w[i];
        add_line({-2*h[i],f[i]+h[i]*h[i]});
    }
    cout << tot+f[n-1];
    return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 34 ms 62924 KB Output is correct
2 Correct 37 ms 62808 KB Output is correct
3 Correct 34 ms 62820 KB Output is correct
4 Correct 36 ms 63008 KB Output is correct
5 Correct 35 ms 62912 KB Output is correct
# Verdict Execution time Memory Grader output
1 Incorrect 87 ms 65476 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 34 ms 62924 KB Output is correct
2 Correct 37 ms 62808 KB Output is correct
3 Correct 34 ms 62820 KB Output is correct
4 Correct 36 ms 63008 KB Output is correct
5 Correct 35 ms 62912 KB Output is correct
6 Incorrect 87 ms 65476 KB Output isn't correct
7 Halted 0 ms 0 KB -