• [AH/HNOI2017]礼物


    [推推公式,即求Sigma^{n}_{i=1} (x_{i+k}-y_i+c)^2最小,c范围为[-m, m] ]

    [拆开,就是Sigma x_i^2 + Sigma y_i^2 + n * c^2 + 2*c*Sigma(x_{i+k}-y_i) - 2*Sigma^{n}_{i=1} x_{i+k}y_i ]

    [即求2*Sigma^{n}_{i=1} x_{i+k}y_i最大,再枚举c即可 ]

    七十分暴力代码(暴力分贼多)

    # include <bits/stdc++.h>
    # define RG register
    # define IL inline
    # define Fill(a, b) memset(a, b, sizeof(a))
    using namespace std;
    typedef long long ll;
    const int _(1e5 + 10);
    
    IL ll Read(){
        char c = '%'; ll x = 0, z = 1;
        for(; c > '9' || c < '0'; c = getchar()) if(c == '-') z = -1;
        for(; c >= '0' && c <= '9'; c = getchar()) x = x * 10 + c - '0';
        return x * z;
    }
    
    int n, m;
    ll sqx, sqy, sx, sy, x[_], y[_], ans = -1e18, mn = 1e18;
    
    int main(RG int argc, RG char *argv[]){
        n = Read(); m = Read();
        for(RG int i = 1; i <= n; ++i) x[i + n] = x[i] = Read(), sx += x[i], sqx += x[i] * x[i];
        for(RG int i = 1; i <= n; ++i) y[i] = Read(), sy += y[i], sqy += y[i] * y[i];
        for(RG int i = 0; i < n; ++i){
            RG ll cnt = 0;
            for(RG int j = 1; j <= n; ++j) cnt += x[j + i] * y[j];
            ans = max(ans, cnt);
        }
        for(RG int c = -m; c <= m; ++c) mn = min(mn, 1LL * n * c * c + 1LL * 2 * c * (sx - sy) - 2 * ans);
        printf("%lld
    ", mn + sqx + sqy);
        return 0;
    }
    
    

    [Sigma^{n}_{i=1} x_{i+k}y_i,很套路,就往FFT上靠,把y反转不就变成Sigma^{n}_{i=1} x_{i+k}y_{n-i+1} ]

    [这不就是卷积,就是多项式相乘后第n+k+1项的系数,这就可以FFT了 ]


    把y反转,再倍长,跑一遍FFT,取有用的中间一段的最大值
    再枚举c求解即可


    # include <bits/stdc++.h>
    # define RG register
    # define IL inline
    # define Fill(a, b) memset(a, b, sizeof(a))
    using namespace std;
    typedef long long ll;
    const int _(4e5 + 10);
    const double Pi(acos(-1));
    
    IL ll Read(){
    	char c = '%'; ll x = 0, z = 1;
    	for(; c > '9' || c < '0'; c = getchar()) if(c == '-') z = -1;
    	for(; c >= '0' && c <= '9'; c = getchar()) x = x * 10 + c - '0';
    	return x * z;
    }
    
    struct Complex{
    	double real, image;
    	IL Complex(){  real = image = 0;  }
    	IL Complex(RG double a, RG double b){  real = a; image = b;  }
    	IL Complex operator +(RG Complex B){  return Complex(real + B.real, image + B.image);  }
    	IL Complex operator -(RG Complex B){  return Complex(real - B.real, image - B.image);  }
    	IL Complex operator *(RG Complex B){  return Complex(real * B.real - image * B.image, real * B.image + image * B.real);  }
    } A[_], B[_];
    int n, m, N, M, l, r[_];
    ll sx, sy, sqx, sqy, mx = -1e18, ans = 1e18;
    
    
    IL void FFT(RG Complex *P, RG int opt){
    	for(RG int i = 0; i < N; i++) if(i < r[i]) swap(P[i], P[r[i]]);
    	for(RG int i = 1; i < N; i <<= 1){
    		RG Complex W(cos(Pi / i), opt * sin(Pi / i));
    		for(RG int p = i << 1, j = 0; j < N; j += p){
    			RG Complex w(1, 0);
    			for(RG int k = 0; k < i; ++k, w = w * W){
    				RG Complex X = P[k + j], Y = w * P[k + j + i];
    				P[k + j] = X + Y; P[k + j + i] = X - Y;
    			}
    		}
    	}
    }
    
    int main(RG int argc, RG char *argv[]){
    	n = Read() - 1; m = Read();
    	for(RG int i = 0; i <= n; ++i) A[i].real = Read(), sx += A[i].real, sqx += A[i].real * A[i].real;
    	for(RG int i = n; i >= 0; --i) B[i + n + 1].real = B[i].real = Read(), sy += B[i].real, sqy += B[i].real * B[i].real;
    	for(M = 3 * n, N = 1; N <= M; N <<= 1) ++l;
    	for(RG int i = 0; i < N; ++i) r[i] = (r[i >> 1] >> 1) | ((i & 1) << (l - 1));
    	FFT(A, 1); FFT(B, 1);
    	for(RG int i = 0; i < N; ++i) A[i] = A[i] * B[i];
    	FFT(A, -1);
    	for(RG int i = n; i <= 2 * n; ++i) mx = max(mx, (ll)(A[i].real / N + 0.5));
    	for(RG int c = -m; c <= m; ++c) ans = min(ans, 1LL * (n + 1) * c * c + 1LL * 2 * c * (sx - sy));
    	printf("%lld
    ", ans + sqx + sqy - 2 * mx);
    	return 0;
    }
    
    
  • 相关阅读:
    CAD开发中遇到的疑难问题整理与开发技巧
    使用jquery插件jquery.qrcode生成二维码
    小程序 跳转页面
    【已解决】Intel NUC10 拔插USB口/登录QQ/蓝牙连接等导致显示器黑屏
    element-ui表格el-table回显时默认全选数据
    设计模式
    react lib-flexible postcss-px2rem集成
    odoo 接口请求原理
    odoo 更改返回的json 格式
    git 合并两个仓库
  • 原文地址:https://www.cnblogs.com/cjoieryl/p/8206708.html
Copyright © 2020-2023  润新知