[AH2017/HNOI2017] 礼物

版权声明:本文为博主原创文章,想转载就转吧! https://blog.csdn.net/qq_35914587/article/details/79971164

题目描述:

没有.

题目分析:

先不考虑C
那么 Ans=mini=1n(xi+kyi)2=i=1n(xi2+yi2)2maxi=1nxi+kyi
后面那个式子如何快速统计呢?
Yi=yni+1
Ansi=1n(xi2+yi2)2maxi=1nxi+kYni+1
就是个卷积的形式了,上FFT即可统计
考虑把C加上去
nc2+2ci=1n(xiyi)
只要从-m到m枚举C求最小值即可

题目链接:

BZOJ 4827
Luogu 3723

Ac 代码:

#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cmath>
#include <cstring>
const int maxm=51000*12;
const double PI=std::acos(-1); 
struct complex{
    double real,imag;
    complex(){};
    complex(double _real,double _imag):real(_real),imag(_imag){}
};
inline complex operator + (complex x,complex y)
{
    return (complex){x.real+y.real,x.imag+y.imag};
}
inline complex operator - (complex x,complex y)
{
    return (complex){x.real-y.real,x.imag-y.imag};
} 
inline complex operator * (complex x,complex y)
{
    return (complex){x.real*y.real-x.imag*y.imag,x.real*y.imag+x.imag*y.real};
}
int rev[maxm];
inline void FFT(complex *a,int n,int f)
{
    for(int i=0;i<n;i++) if(i<rev[i]) std::swap(a[i],a[rev[i]]);
    for(int i=1;i<n;i<<=1)
    {
        complex wn=(complex){std::cos(PI/i),f*std::sin(PI/i)};
        for(int j=0;j<n;j+=(i<<1))
        {
            complex w=(complex){1,0};
            for(int k=0;k<i;k++,w=(w*wn))
            {
                complex x=a[j+k],y=a[i+j+k]*w;
                a[j+k]=x+y,a[i+j+k]=x-y;
            }
        }
    }
    if(f==-1) for(int i=0;i<n;i++) a[i].real/=n;
}
complex A[maxm],B[maxm];
int n,m,c,len;
int main()
{
    int suma,sumb,s1,s2;
    suma=sumb=s1=s2=0;
    scanf("%d%d",&n,&c);
    for(int i=0,x;i<n;i++)
    {
        scanf("%d",&x);
        s1+=x*x,suma+=x;
        A[i].real=A[i+n].real=(double)x;
    }
    for(int i=n-1,x;i>=0;i--)
    {
        scanf("%d",&x);
        s2+=x*x,sumb+=x;
        B[i].real=(double)x;    
    }
    for(m=1;m<=3*n;m<<=1) len++;
    for(int i=0;i<m;i++) rev[i]=(rev[i>>1]>>1)|((i&1)<<(len-1));
    FFT(A,m,1),FFT(B,m,1);
    for(int i=0;i<m;i++) A[i]=A[i]*B[i];
    FFT(A,m,-1);
    int ans=-1e9+7;
    for(int i=0;i<n;i++)
     ans=std::max((int)round(A[n-1+i].real),ans);
    int Ans=1e9+7;
    for(int i=-c;i<=c;i++)
     Ans=std::min(Ans,s1+s2+n*i*i-2*i*(suma-sumb)-2*ans);
    printf("%d\n",Ans);
    return 0;
}
阅读更多 登录后自动展开
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页