大家好,又见面了,我是你们的朋友全栈君。
Description
麻烦你帮他
Input
Output
Sample Input
1 2 3 4 5
6 3 3 4 5
Sample Output
1
【样例解释】
需要将第一个手环的亮度增加1,第一个手环的亮度变为: 2 3 4 5 6 旋转一下第二个手环。对于该样例,是将第
二个手环的亮度6 3 3 4 5向左循环移动 2017-04-15 第 6 页,共 6 页 一个位置,使得第二手环的最终的亮度为
:3 3 4 5 6。 此时两个手环的亮度差异值为1。
Solution
感谢XY大爷对我的帮助
我以后FFT下标再也不从1开始了太难处理了QAQ
Code
1 #include<iostream> 2 #include<cstring> 3 #include<cstdio> 4 #include<cmath> 5 #define N (600000+100) 6 using namespace std; 7 8 double pi=acos(-1.0),F[N]; 9 int n,m,k,fn,l,r[N],A[N],B[N]; 10 int minn=0x7fffffff,squA,squB,squC,sumA,sumB; 11 struct complex 12 { 13 double x,y; 14 complex (double xx=0,double yy=0) 15 { 16 x=xx; y=yy; 17 } 18 }a[N],b[N]; 19 20 complex operator + (complex a,complex b){ return complex(a.x+b.x,a.y+b.y);} 21 complex operator - (complex a,complex b){ return complex(a.x-b.x,a.y-b.y);} 22 complex operator * (complex a,complex b){ return complex(a.x*b.x-a.y*b.y,a.x*b.y+a.y*b.x);} 23 complex operator / (complex a,double b){ return complex(a.x/b,a.y/b);} 24 25 void FFT(int n,complex *a,int opt) 26 { 27 for (int i=0; i<n; ++i) 28 if (i<r[i]) 29 swap(a[i],a[r[i]]); 30 for (int k=1; k<n; k<<=1) 31 { 32 complex wn=complex(cos(pi/k),opt*sin(pi/k)); 33 for (int i=0; i<n; i+=k<<1) 34 { 35 complex w=complex(1,0); 36 for (int j=0; j<k; ++j,w=w*wn) 37 { 38 complex x=a[i+j], y=w*a[i+j+k]; 39 a[i+j]=x+y; a[i+j+k]=x-y; 40 } 41 } 42 } 43 if (opt==-1) for (int i=0; i<n; ++i) a[i]=a[i]/n; 44 } 45 46 int Calc() 47 { 48 for (int i=0; i<n; ++i) a[i].x=A[i]; 49 for (int i=0; i<m; ++i) b[i].x=B[i]; 50 51 FFT(fn,a,1); FFT(fn,b,1); 52 for (int i=0; i<=fn; ++i) a[i]=a[i]*b[i]; 53 FFT(fn,a,-1); 54 55 for (int i=0; i<n; ++i) squA=squA+A[i]*A[i]; 56 for (int i=0; i<n; ++i) squB=squB+B[i]*B[i]; 57 for (int i=0; i<n; ++i) sumA=sumA+A[i]; 58 for (int i=0; i<n; ++i) sumB=sumB+B[i]; 59 60 for (int c=-k; c<=k; ++c) 61 for (int i=n-1; i<=m-1; ++i) 62 minn=min(minn,n*c*c+squA+2*c*sumA+squB-2*(int)(a[i].x+0.5)-2*c*sumB); 63 return minn; 64 } 65 66 int main() 67 { 68 scanf("%d%d",&n,&k); 69 for (int i=0; i<n; ++i) scanf("%d",&A[n-i-1]); 70 for (int i=0; i<n; ++i) scanf("%d",&B[i]),B[n+i]=B[i]; 71 m=2*n; 72 73 fn=1; 74 while (fn<=n+m) fn<<=1, l++; 75 for (int i=0; i<fn; ++i) 76 r[i]=(r[i>>1]>>1) | ((i&1)<<(l-1)); 77 78 printf("%d\n",Calc()); 79 }
转载于:https://www.cnblogs.com/refun/p/8871678.html
发布者:全栈程序员-用户IM,转载请注明出处:https://javaforall.cn/107572.html原文链接:https://javaforall.cn
【正版授权,激活自己账号】: Jetbrains全家桶Ide使用,1年售后保障,每天仅需1毛
【官方授权 正版激活】: 官方授权 正版激活 支持Jetbrains家族下所有IDE 使用个人JB账号...