답안 #578227

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
578227 2022-06-16T08:46:24 Z andrei_boaca Building Bridges (CEOI17_building) C++14
0 / 100
67 ms 81728 KB
#include <bits/stdc++.h>
#define MAXH 1000000
using namespace std;
typedef long long ll;
ll n;
ll h[100005],w[100005];
ll dp[100005];
ll s[100005];
struct func
{
    ll a,b;
};
func arb[5000005];
ll f(func e,ll x)
{
    return e.a*x+e.b;
}
ll intersect(func e, func g)
{
    if(g.a>0)
        return 1e9;
    return (g.b-e.b)/(e.a-g.a);
}
void update(ll nod,ll st,ll dr, func e)
{
    if(st==dr)
    {
        ll x1=f(arb[nod],st);
        ll x2=f(e,st);
        if(x2<x1)
            arb[nod]=e;
        return;
    }
    ll mij=(st+dr)/2;
    if(arb[nod].a==e.a)
    {
        if(arb[nod].b>e.b)
            arb[nod]=e;
        return;
    }
    func l1=e;
    func l2=arb[nod];
    if(l1.a>l2.a)
        swap(l1,l2);
    ll p=intersect(l1,l2);
    if(p<st)
    {
        arb[nod]=l2;
        return;
    }
    if(p>dr)
    {
        arb[nod]=l1;
        return;
    }
    if(p<=mij)
    {
        arb[nod]=l2;
        update(nod*2,st,mij,l1);
        return;
    }
    if(p>mij)
    {
        arb[nod]=l1;
        update(nod*2+1,mij+1,dr,l2);
        return;
    }
}
ll query(ll nod,ll st,ll dr, ll x)
{
    ll ans=f(arb[nod],x);
    if(st==dr)
        return ans;
    ll mij=(st+dr)/2;
    if(x<=mij)
        ans=min(ans,query(nod*2,st,mij,x));
    else
        ans=min(ans,query(nod*2+1,mij+1,dr,x));
    return ans;
}
int main()
{
    ios_base::sync_with_stdio(false);
    cin>>n;
    for(int i=1;i<=n;i++)
        cin>>h[i];
    for(int i=1;i<=n;i++)
    {
        cin>>w[i];
        s[i]=s[i-1]+w[i];
    }
    for(int i=1;i<=5*MAXH;i++)
        arb[i]={ll(1e11),ll(1e11)};
    dp[1]=0;
    func e={-2*h[1],dp[1]-s[1]+h[1]*h[1]};
    update(1,0,MAXH,e);
    for(int i=2;i<=n;i++)
    {
        dp[i]=query(1,0,MAXH,h[i])+s[i-1]+h[i]*h[i];
        e={-2*h[i],dp[i]-s[i]+h[i]*h[i]};
        update(1,0,MAXH,e);
    }
    cout<<dp[n];
    return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 33 ms 78548 KB Output is correct
2 Incorrect 34 ms 78604 KB Output isn't correct
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 67 ms 81728 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 33 ms 78548 KB Output is correct
2 Incorrect 34 ms 78604 KB Output isn't correct
3 Halted 0 ms 0 KB -