BZOJ4827: [Hnoi2017]礼物(FFT 二次函数)
程序员文章站
2022-11-10 16:58:41
题意 "题目链接" Sol 越来越菜了。。裸的FFT写了1h。。 思路比较简单,直接把 $\sum (x_i y_i + c)^2$ 拆开 发现能提出一坨东西,然后与c有关的部分是关于C的二次函数可以直接算最优取值 剩下的要求的就是$max (\sum x_i y_i)$ 画画图就知道把y序列倒过来 ......
题意
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; }
上一篇: 谈恋爱没话说怎么办?怎么找话题呢