답안 #476197

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
476197 2021-09-25T10:12:51 Z kakayoshi Building Bridges (CEOI17_building) C++14
0 / 100
3000 ms 4268 KB
#include <bits/stdc++.h>
using namespace std;
#define forw(i,a,b) for(ll i=a;i<=b;i++)
#define forb(i,a,b) for(ll i=a;i>=b;i--)
typedef long long int ll;
ll res,n,h[100005],w[100005],max1[100005],pre[100005],dp[100005];
void sub1()
{
    for (int i=2;i<=n;i++)
    {
        max1[i]=1e18;
        res=0;
        for (int j=i-1;1;j--)
            {
                ll p=(h[i]-h[j])*(h[i]-h[j])+res+max1[j];
                if (p<max1[i]) max1[i]=p;
                res+=w[j];
            }
    }
    cout<<max1[n];
}
void sub2()
{
    ll ans=1e18;
    forw(i,1,n) pre[i]=pre[i-1]+w[i];
    ans=(h[n]-h[1])*(h[n]-h[1])+pre[n-1]-pre[1];
    forw(i,2,n-1)
        ans=min(ans,(h[i]-h[1])*(h[i]-h[1])+(h[i]-h[n])*(h[i]-h[n])+pre[i-1]-pre[1]+pre[n-1]-pre[i]);
    map<int, set<int> > save;
    //save[w[1]].insert(h[1]);
    forb(i,n-1,2)
    {
        dp[i]=(h[n]-h[i])*(h[n]-h[i])+pre[n-1]-pre[i];
        //cout<<dp[i]<<" "<<h[n]-h[i]<<" "<<pre[n-1]-pre[i]<<endl;
    }
    forw(i,2,n-1)
    {
        forw(j,-20,20)
        if (save[j].size())
        {
            auto it =lower_bound(save[j].begin(),save[j].end(),(h[i]+h[1])/2);
            if (it!=save[j].end())
            {
                //cout<<i<<" "<<j<<" "<<(h[i]-(*it))*(h[i]-(*it))<<" "<<pre[i-1]-j-pre[1]<<" "<<dp[i]<<" "<<(h[i]-(*it))*(h[i]-(*it))+pre[i]-j-pre[1]+dp[i]<<endl;
                ans=min(ans,(h[i]-(*it))*(h[i]-(*it))+pre[i-1]-j-pre[1]+dp[i]+((*it)-h[1])*((*it)-h[1]));
            }
            it--;
            if (it!=save[j].end())
            {
                //cout<<i<<" "<<j<<" "<<(h[i]-(*it))*(h[i]-(*it))<<" "<<pre[i-1]-j-pre[1]<<" "<<dp[i]<<" "<<(h[i]-(*it))*(h[i]-(*it))+pre[i]-j-pre[1]+dp[i]<<endl;
                ans=min(ans,(h[i]-(*it))*(h[i]-(*it))+pre[i-1]-j-pre[1]+dp[i]+((*it)-h[1])*((*it)-h[1]));
            }
        }
        save[w[i]].insert(h[i]);
    }
    cout<<ans<<endl;
}
int main()
{
    //freopen("test.INP","r",stdin);
    //freopen("test.OUT","w",stdout);
    cin>>n;
    for (int i=1;i<=n;i++)
        cin>>h[i];
    for (int i=1;i<=n;i++)
        cin>>w[i];
    if (n<=1000) sub1();
    else sub2();
    //sub2();
    return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Execution timed out 3065 ms 204 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Execution timed out 3074 ms 4268 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Execution timed out 3065 ms 204 KB Time limit exceeded
2 Halted 0 ms 0 KB -