比赛 2022级DP专题练习赛7 评测结果 AAAAAAAAAA
题目名称 Building Bridges 最终得分 100
用户昵称 op_组撒头屯 运行时间 0.857 s
代码语言 C++ 内存使用 9.16 MiB
提交时间 2023-02-27 19:20:47
显示代码纯文本
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ull unsigned long long
#define ld double
#define pii pair<int,int>
#define vi vector<int>
#define si set<int>
#define unsi unordered_set<int>
#define qi queue<int>
#define sti stack<int>
#define pqi priority_queue<int>
#define mii map<int,int>
#define unmii unordered_map<int,int>
#define fi first
#define se second
#define pb push_back
#define clr(f,n) memset(f,0,sizeof(int)*(n))
#define cpy(f,g,n) memcpy(f,g,sizeof(int)*(n))
const int N=100000+5;
const ll inf=0x3f3f3f3f3f3f;
struct sdf{
    ll x,y,k;
}p[N];
int n;
ll m,f[N],h[N],w[N];
int q[N],id[N],tmp[N],tp=0;
vi b;
inline ld slope(int i,int j){
    if (p[j].x==p[i].x){
        if (f[j]+p[j].y>f[i]+p[i].y)return 1e18;
        else return -1e18;
    }
    return (ld)1.0*(f[j]+p[j].y-f[i]-p[i].y)/(p[j].x-p[i].x);
}
void cdq(int l,int r){
    if (l==r)return ;
    int mid=(l+r)/2;
    cdq(l,mid);tp=0;
    for (int i=l;i<=mid;i++){
        while(tp>1&&slope(q[tp-1],q[tp])>=slope(q[tp],id[i]))q[tp--]=0;
        q[++tp]=id[i];
    }
    for (int i=mid+1;i<=r;i++){
        int L=1,R=tp;
        while(L<R){
            int Mid=(L+R)/2;
            if (slope(q[Mid],q[Mid+1])>p[i].k)R=Mid;
            else L=Mid+1;
        }
        int j=q[L];
        f[i]=min(f[i],-p[i].k*p[j].x+p[j].y+f[j]+w[i-1]+h[i]*h[i]);
    }
    cdq(mid+1,r);
    for (int k=l,i=l,j=mid+1;k<=r;k++){
        if (j>r||(i<=mid&&p[id[i]].x<p[id[j]].x))tmp[k]=id[i++];
        else tmp[k]=id[j++];
    }
    for (int i=l;i<=r;i++)id[i]=tmp[i];
}
inline ll read(){
    ll ans=0;char ch=getchar();
    while(!isdigit(ch))ch=getchar();
    while(isdigit(ch))ans=(ans<<1)+(ans<<3)+(ch^48),ch=getchar();
    return ans;
}
int main(){
	freopen ("jiaqiao.in","r",stdin);
	freopen ("jiaqiao.out","w",stdout);
	scanf("%d",&n);
	for (int i=1;i<=n;i++)scanf("%lld",&h[i]);
	for (int i=1;i<=n;i++){
        f[i]=inf;id[i]=i;
	    scanf("%lld",&w[i]),w[i]+=w[i-1];
	    p[i].x=h[i]*2;p[i].y=h[i]*h[i]-w[i];p[i].k=h[i];
    }f[1]=0;
    cdq(1,n);
    printf("%lld\n",f[n]);
}