博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
[AH/HNOI2017]礼物
阅读量:7058 次
发布时间:2019-06-28

本文共 3571 字,大约阅读时间需要 11 分钟。

\[推推公式,即求\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 
# 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\n", 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 
# 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\n", ans + sqx + sqy - 2 * mx); return 0;}

转载于:https://www.cnblogs.com/cjoieryl/p/8206708.html

你可能感兴趣的文章
我的友情链接
查看>>
zookeeper应用场景
查看>>
mysql中explain的用法
查看>>
基于kryo序列化方案的memcached-session-manager多memcached节点配
查看>>
H5页面快速搭建之高级字体应用实践
查看>>
centos6.5、centos6.6修改ssh默认端口号
查看>>
文本文件和二进制文件
查看>>
轻量级smurf源码
查看>>
linux下桌面环境的介绍及VNC的使用
查看>>
深浅拷贝——string
查看>>
主从复制模式下跳过错误
查看>>
剑指offer17
查看>>
samba文件共享
查看>>
MySQL专题7之MySQL连接、 MySQL MULL值得处理以及MySQL 正则表达式的使用
查看>>
第二次作业
查看>>
web报表轻松实现数据异常预警功能
查看>>
ASP.NET Core之跨平台的实时性能监控
查看>>
tomcat日志切割
查看>>
WannaCry勒索软件还在继续传播和感染中
查看>>
TarsGo新版本发布,支持protobuf,zipkin和自定义插件
查看>>