BZOJ4827: [Hnoi2017]礼物(FFT 二次函数)

2019-03-05 15:59:00 浏览数 (1)

题意

题目链接

Sol

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

思路比较简单,直接把

(sum (x_i - y_i c)^2)

拆开

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

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

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

代码语言:javascript复制
#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;
}

0 人点赞