题意

题目链接

Sol

越来越菜了。。裸的FFT写了1h。。

思路比较简单,直接把

\(\sum (x_i - y_i + c)^2\)

拆开

发现能提出一坨东西,然后与c有关的部分是关于C的二次函数可以直接算最优取值

剩下的要求的就是\(max (\sum x_i y_i)\)

画画图就知道把y序列倒过来就是个裸的FFT了。

#include<bits/stdc++.h>
#define Pair pair<int, int>
#define MP(x, y) make_pair(x, y)
#define fi first
#define se second
#define LL long long
#define Fin(x) {freopen(#x".in","r",stdin);}
#define Fout(x) {freopen(#x".out","w",stdout);}
using namespace std;
const int MAXN = 1e6 + 10, mod = 1e9 + 7, INF = 1e9 + 10;
const double eps = 1e-9, Pi = acos(-1);
template <typename A, typename B> inline bool chmin(A &a, B b){if(a > b) {a = b; return 1;} return 0;}
template <typename A, typename B> inline bool chmax(A &a, B b){if(a < b) {a = b; return 1;} return 0;}
template <typename A, typename B> inline LL add(A x, B y) {if(x + y < 0) return x + y + mod; return x + y >= mod ? x + y - mod : x + y;}
template <typename A, typename B> inline void add2(A &x, B y) {if(x + y < 0) x = x + y + mod; else x = (x + y >= mod ? x + y - mod : x + y);}
template <typename A, typename B> inline LL mul(A x, B y) {return 1ll * x * y % mod;}
template <typename A, typename B> inline void mul2(A &x, B y) {x = (1ll * x * y % mod + mod) % mod;}
template <typename A> inline void debug(A a){cout << a << '\n';}
template <typename A> inline LL sqr(A x){return 1ll * x * x;}
inline int read() {
char c = getchar(); int x = 0, f = 1;
while(c < '0' || c > '9') {if(c == '-') f = -1; c = getchar();}
while(c >= '0' && c <= '9') x = x * 10 + c - '0', c = getchar();
return x * f;
}
int N, M;
int a[MAXN], b[MAXN], c[MAXN], t[MAXN], nx, ny, rev[MAXN];
double sx, sy;
struct com {
double x, y;
}A[MAXN], B[MAXN], C[MAXN];
com operator * (const com a, const com b) {
return (com) {a.x * b.x - a.y * b.y, a.x * b.y + a.y * b.x};
}
com operator + (const com a, const com b) {
return (com) {a.x + b.x, a.y + b.y};
}
com operator - (const com a, const com b) {
return (com) {a.x - b.x, a.y - b.y};
}
void FFT(com *A, int lim, int opt) {
for(int i = 0; i < lim; i++) if(i < rev[i]) swap(A[i], A[rev[i]]);
for(int mid = 1; mid < lim; mid <<= 1) {
com Wn = (com) {cos(Pi / mid), opt * sin(Pi / mid)};
for(int i = 0, R = mid << 1; i <= lim; i += R) {//tag
com w = (com) {1, 0};
for(int j = 0; j < mid; j++, w = w * Wn) {
com x = A[i + j], y = w * A[i + j + mid];
A[i + j] = x + y;
A[i + j + mid] = x - y;
}
}
}
if(opt == -1) {
for(int i = 0; i <= lim; i++) A[i].x /= lim;
}
}
LL check(int c) {
ny = 0;
memcpy(b, t, sizeof(t));
for(int i = 0; i < N; i++) b[i] += c, b[i + N] = b[i], ny += b[i] * b[i];
int M = 2 * N - 1;
reverse(b, b + M + 1);
memset(A, 0, sizeof(A));
memset(B, 0, sizeof(B));
memset(C, 0, sizeof(C));
N--;
for(int i = 0; i <= N; i++) A[i].x = a[i];
for(int i = 0; i <= M; i++) B[i].x = b[i];
int lim = 1, len = 0;
while(lim <= N + M) lim <<= 1, len++;
for(int i = 1; i <= lim; i++) rev[i] = (rev[i >> 1] >> 1) + ((i & 1) << (len - 1));
FFT(A, lim, 1); FFT(B, lim, 1);
for(int i = 0; i <= lim; i++) C[i] = A[i] * B[i];
FFT(C, lim, -1); N++;
LL ret = 0;
for(int i = 0; i <= M; i++) chmax(ret, C[i].x + 0.5);
return -2 * ret + nx + ny;
}
signed main() {
N = read(); M = read();
for(int i = 0; i < N; i++) a[i] = read(), sx += a[i], nx += a[i] * a[i];
for(int i = 0; i < N; i++) b[i] = read(), sy += a[i];
memcpy(t, b, sizeof(b));
int c = - (sx - sy) / N;
LL ans = check(c);
ans = min(ans, min(check(c - 1), check(c + 1)));
cout << ans;
return 0;
}

最新文章

  1. webgl动画小测试
  2. JS中的if和else的用法以及基础语法
  3. unbuntu apahce 2 设置 多域名
  4. Bzoj1189 [HNOI2007]紧急疏散evacuate
  5. struts2-(2)HelloWorld
  6. PHP查看SSL证书信息
  7. MySQL数据库之数据类型BOOL/BOOLEAN与TINYINT测试总结
  8. [翻译]创建ASP.NET WebApi RESTful 服务(7)
  9. 剑指offer--面试题8
  10. Unity3D 之2D动画机
  11. UVA11538Chess Queen(组合数学推公式)
  12. JSP 网页格式判定执行哪一块html
  13. C#与Java区别(一)
  14. 【集训第四天&#183;继续刷题】之 lgh怒刚ypj
  15. kettle 连接 SQL Server 异常
  16. word文档的python解析
  17. JDK自带jvisualvm监控工具
  18. GPUImage API 文档之GPUImageFilter类
  19. 算法笔记_222:串中取3个不重复字母(Java)
  20. bzoj1613

热门文章

  1. mysql数据库崩溃:InnoDB: Database page corruption on disk or a failed
  2. eslint 配置及规则说明
  3. 编译lua-5.3.5时出错解决方法
  4. JDK中ConcurrentHashMap效率测试
  5. java信任所有证书
  6. 使用安装 php-memcache-client
  7. SQL Server 跨域访问
  8. GO入门——3. 控制语句
  9. 前端组件化Polymer入门教程(1)——初识&amp;&amp;安装
  10. j2ee高级开发技术课程第十四周