WA了几十次了,救救孩子

P4655 [CEOI2017] Building Bridges

FLAT_LCH @ 2022-05-17 23:01:30

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <vector>
#include <queue>
#include <algorithm>
#include <cstring>
#include <cmath>

#define inf 0x3f3f3f3f3f3f3f3f

using namespace std;

struct node
{
    long long s,w,f,h;
    long long id;
    node(){f=inf;}
}p[110000];

long long n;
long long q[110000];

inline long long rd()
{
    long long s=0,w=1;char x='x';
    while(x<'0'||x>'9'){x=getchar();if(x=='-')w=-1;}
    while(x>='0'&&x<='9'){s=s*10+(x^48);x=getchar();}
    return s*w;
}

inline void readd()
{
    n=rd();
    for(long long i=1;i<=n;i++)
        p[i].h=rd(),p[i].id=i;
    for(long long i=1;i<=n;i++)
    {
        p[i].w=rd();
        p[i].s=p[i-1].s+p[i].w;
    }
}

inline long long X(long long i){return p[i].h;}
inline long long K(long long i){return -2*p[i].h;}
inline long long Y(long long i){return p[i].s-p[i].f-(p[i].h*p[i].h);}
//f[i]=f[j]+s[i]-s[j]-w[i]+(h[i]-h[j])*(h[i]-h[j]);
//XIA_TU_KE

bool cmp1(node x,node y)
{
    return x.h==y.h?x.s-x.f-(x.h*x.h)>y.s-y.f-(y.h*y.h):x.h<y.h;
}
bool cmp3(node x,node y)
{return -x.h<-y.h;}
bool cmp4(node x,node y)
{return x.id<y.id;}

inline long long cnt(long long i,long long j)
{
    return p[j].f+p[i].s-p[j].s-p[i].w+(p[i].h-p[j].h)*(p[i].h-p[j].h);
}

void cdq(long long l,long long r)
{
    if(l>=r)return;
    long long mid=(l+r)/2;
    cdq(l,mid);
    sort(p+l,p+mid+1,cmp1);
    sort(p+mid+1,p+r+1,cmp3);

    long long head=1,tail=0;
    for(long long i=l;i<=mid;i++)
    {
        while(head<tail&&(Y(q[tail])-Y(q[tail-1]))*(X(i)-X(q[tail-1]))<(Y(i)-Y(q[tail-1]))*(X(q[tail])-X(q[tail-1])))tail--;
        q[++tail]=i;
    }
    //cout<<"__________"<<l<<' '<<mid<<' '<<r<<endl;

    //for(long long i=head;i<=tail;i++)cout<<p[q[i]].id<<' ';
    //cout<<endl;

    for(long long i=mid+1;i<=r;i++)
    {
        while(head<tail&&cnt(i,q[head+1])<cnt(i,q[head]))head++;
        //long long j=q[head];
        //if(p[i].f>p[j].f+p[i].s-p[j].s-p[i].w+(p[i].h-p[j].h)*(p[i].h-p[j].h))cout<<p[i].id<<' '<<p[j].id<<endl;
        //cout<<head<<' '<<'@'<<tail<<endl;
        p[i].f=min(p[i].f,cnt(i,q[head]));
    }
    sort(p+l,p+r+1,cmp4);
    cdq(mid+1,r);
}

inline void print()
{
    //cout<<endl;
    //for(long long i=1;i<=n;i++)
    //  cout<<p[i].id<<' '<<p[i].f<<endl;
    printf("%lld",p[n].f);
}

int main()
{
    readd();
    p[1].f=0; cdq(1,n);
    print();
    return 0;
}

by LJ07 @ 2022-06-22 18:51:38

怎么到哪都能看到卷王 @FLAT_LCH


by FLAT_LCH @ 2022-06-22 22:48:25

@LJ07 我是差生


|