BZOJ4827: [Hnoi2017]礼物(FFT 二次函数)
题意
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;
}
BZOJ4827: [Hnoi2017]礼物(FFT 二次函数)的更多相关文章
- BZOJ4827:[HNOI2017]礼物(FFT)
Description 我的室友最近喜欢上了一个可爱的小女生.马上就要到她的生日了,他决定买一对情侣手环,一个留给自己,一 个送给她.每个手环上各有 n 个装饰物,并且每个装饰物都有一定的亮度.但是在 ...
- [BZOJ4827][Hnoi2017]礼物(FFT)
4827: [Hnoi2017]礼物 Time Limit: 20 Sec Memory Limit: 512 MBSubmit: 1315 Solved: 915[Submit][Status] ...
- [bzoj4827][Hnoi2017]礼物_FFT
礼物 bzoj-4827 Hnoi-2017 题目大意:给定两个长度为$n$的手环,第一个手环上的$n$个权值为$x_i$,第二个为$y_i$.现在我可以同时将所有的$x_i$同时加上自然数$c$.我 ...
- [Luogu P3723] [AH2017/HNOI2017]礼物 (FFT 卷积)
题面 传送门:洛咕 Solution 调得我头大,我好菜啊 好吧,我们来颓柿子吧: 我们可以只旋转其中一个手环.对于亮度的问题,因为可以在两个串上增加亮度,我们也可以看做是可以为负数的. 所以说,我们 ...
- bzoj 4827: [Hnoi2017]礼物 [fft]
4827: [Hnoi2017]礼物 题意:略 以前做的了 化一化式子就是一个卷积和一些常数项 我记着确定调整值还要求一下导... #include <iostream> #include ...
- BZOJ4827 [Hnoi2017]礼物 多项式 FFT
原文链接http://www.cnblogs.com/zhouzhendong/p/8823962.html 题目传送门 - BZOJ4827 题意 有两个长为$n$的序列$x$和$y$,序列$x,y ...
- 【bzoj4827】[Hnoi2017]礼物 FFT
题目描述 我的室友最近喜欢上了一个可爱的小女生.马上就要到她的生日了,他决定买一对情侣手 环,一个留给自己,一个送给她.每个手环上各有 n 个装饰物,并且每个装饰物都有一定的亮度.但是在她生日的前一天 ...
- bzoj4827 [Hnoi2017]礼物
Description 我的室友最近喜欢上了一个可爱的小女生.马上就要到她的生日了,他决定买一对情侣手 环,一个留给自己,一个送给她.每个手环上各有 n 个装饰物,并且每个装饰物都有一定的亮度.但是在 ...
- [AH2017/HNOI2017]礼物(FFT)
题目描述 我的室友最近喜欢上了一个可爱的小女生.马上就要到她的生日了,他决定买一对情侣手 环,一个留给自己,一 个送给她.每个手环上各有 n 个装饰物,并且每个装饰物都有一定的亮度.但是在她生日的前一 ...
随机推荐
- ajaxsubmit 上传文件 在IE中返回的内容 提示下载文件
在ajaxSubmit提交表单的时候,如果表单内有文件上传的话,会判断参数是否配置的iframe为false参数,如果没有,会用创建隐藏iframe方式提交表单,如果设定了iframe为false,则 ...
- nginx发布静态网页
http://www.jb51.net/article/71384.htm 切记不要把项目放在/root下 会出现 nginx open() "" failed (13: Perm ...
- JDK中ConcurrentHashMap效率测试
比较HashMap HashTable 和ConcurrentHashMap的效率. 一般情况下,达到一定的数量之后JDK1.5之后提供的ConcurrentHashMap集合类的效率是前两者的3~4 ...
- Java模式—静态代理模式
静态代理模式(Proxy):为其他对象提供一种代理以控制对这个对象的访问,提供“真实对象”的代表,在访问对象时引入一定程度的间接性,这种间接性可以附加多种用途. 代理模式的主要作用是为其他对象 ...
- 使用 Scrapyd 管理部署 Scrapy 的一些问题
环境:Ubuntu Xenial (16.04) Scrapy 是一个不错的爬虫框架,但是不支持定时执行,常规的做法是使用 crontab 的方式进行定时执行 shell ,当爬虫数量多的时候,管理起 ...
- Redis学习之实现优先级消息队列
很久没有写博客了,最近简单的学习了一下Redis,其中学习了一下用Redis实现优先级消息队列.关于更多更为详细的可以在www.redis.cn找到相关资料. 对于熟悉Redis的童鞋提到队列很自然的 ...
- Oracle SQL语句执行步骤
转自:http://www.cnblogs.com/quanweiru/archive/2012/11/09/2762345.html Oracle中SQL语句执行过程中,Oracle内部解析原理如下 ...
- Redhat6.8安装Oracle11g下遇到两个问题记录
问题一: 刚刚安装完毕Oracle之后,尝试sqlplus登陆报错,TNS:net service name is incorrectly specified 参考文章:关于环境变量ORACLE_SI ...
- POJ 1061 青蛙的约会(拓展欧几里得算法求解模线性方程组详解)
题目链接: BZOJ: https://www.lydsy.com/JudgeOnline/problem.php?id=1477 POJ: https://cn.vjudge.net/problem ...
- deque双端队列(常用方法总结)
/*关于C++ STL中deque的学习*/ #include<cstdio> #include<iostream> #include<deque> using n ...