【AH/HNOI 2017】【BZOJ 4827】 礼物 (FFT,卷积,数学)

本文探讨了如何通过快速傅立叶变换(FFT)解决特定数值优化问题,具体包括将序列移动后求最小值的方法,并给出了详细的算法实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Description

click me

Solution

题目要求的其实就是把 {yi} { y i } 移动后的

i=0n1(xiyi+c) ∑ i = 0 n − 1 ( x i − y i + c )
的最小值,拆开:
i=0n1x2i+y2i+c22xiyi+2c(xiyi) ∑ i = 0 n − 1 x i 2 + y i 2 + c 2 − 2 x i y i + 2 c ( x i − y i )
显然当 c c 确定时,只有xiyi是变动的,其他的都可以直接求。

关于求 c c

发现与c有关的是 c2+2c(xiyi) c 2 + 2 c ( x i − y i ) ,是一个二次函数,直接求二次函数的最值即可。

关于求 xiyi ∑ x i y i

这个地方特别妙:
考虑把 {yi} { y i } 倒过来,发现位移之后要求的值是两个卷积!!!
FFT后直接算就行。

Code

#include<bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef pair<int, int> PII;
typedef vector<int> VI;
typedef set<int> SI;
typedef set<int>::iterator siit;
typedef complex<double> cpx;
#define rep(i, j) for (register int i = 0, i##_end_ = (j); i < i##_end_; ++ i)
#define For(i , j , k) for (register int i = (j) , i##_end_ = (k) ; i <= i##_end_ ; ++ i)
#define Fordown(i , j , k) for (register int i = (j) , i##_end_ = (k) ; i >= i##_end_ ; -- i)
#define Set(a , b) memset(a , b , sizeof(a))
#define pb(a) push_back(a)
#define mp(a, b) make_pair(a, b)
#define ALL(a) (a).begin(), (a).end()
#define SZ(a) ((int)(a).size())
#define fir first
#define sec second
#define INF (0x3f3f3f3f)
#define INF1 (2139062143)
#define Mod (1000000007)
#ifdef hany01
#define debug(...) fprintf(stderr , __VA_ARGS__)
#else
#define debug(...)
#endif

template <typename T> inline bool chkmax(T &a , T b) { return a < b ? (a = b , 1) : 0; }
template <typename T> inline bool chkmin(T &a , T b) { return b < a ? (a = b , 1) : 0; }

int _ , __;
char c_;
inline int read()
{
    for (_ = 0 , __ = 1 , c_ = getchar() ; !isdigit(c_) ; c_ = getchar()) if (c_ == '-') __ = -1;
    for ( ; isdigit(c_) ; c_ = getchar()) _ = (_ << 1) + (_ << 3) + (c_ ^ 48);
    return _ * __;
}

inline void File()
{
#ifdef hany01
    freopen("gift.in" , "r" , stdin);
    freopen("gift.out" , "w" , stdout);
#endif
}

const double pi = acos(-1.0);

const int maxn = 1 << 21;

struct Complex
{
    double real, imag;
}a[maxn], b[maxn];
inline Complex operator + (const Complex &A, const Complex &B) { return (Complex){A.real + B.real, A.imag + B.imag}; }
inline Complex operator - (const Complex &A, const Complex &B) { return (Complex){A.real - B.real, A.imag - B.imag}; }
inline Complex operator * (const Complex &A, const Complex &B) { return (Complex){A.real * B.real - A.imag * B.imag, A.real * B.imag + A.imag * B.real}; }
int n, m, nn, ans[maxn], Ans, cnt, xx[maxn], yy[maxn], xsum, ysum, t, c, rev[maxn], sum;
Complex x[maxn], y[maxn];

inline void Init()
{
    n = read(); m = read();
    rep(i, n) x[i].real = xx[i] = read();
    rep(i, n) y[n - i - 1].real = yy[i] = read();
    for (nn = n << 1, n = 1; n < nn; n <<= 1) ++ cnt;
    rep(i, n) rev[i] = (rev[i >> 1] >> 1) | ((i & 1) << (cnt - 1));
}

inline void fft(Complex *a, int type)
{
    rep(i, n) if (rev[i] > i) swap(a[i], a[rev[i]]);
    for (register int i = 2; i <= n; i <<= 1)
    {
        register Complex wn = (Complex){cos(2 * pi / i), sin(2 * pi / i * type)};
        for (register int j = 0; j < n; j += i)
        {
            register Complex w = (Complex){1, 0};
            rep(k, i >> 1)
            {
                register Complex x = a[j + k], y = a[j + k + (i >> 1)] * w;
                a[j + k] = x + y; a[j + k + (i >> 1)] = x - y;
                w = w * wn;
            }
        }
    }
}

inline void Solve()
{
    fft(x, 1); fft(y, 1);
    rep(i, n) x[i] = x[i] * y[i];
    fft(x, -1);
    rep(i, n) ans[i] = (int)(x[i].real / n + 0.5);
    n = nn >> 1;
    rep(i, n) sum += xx[i] * xx[i] + yy[i] * yy[i], xsum += xx[i], ysum += yy[i];
    t = (xsum - ysum);
    Ans = INF;
    int sx = INF;
    c = ceil(- t * 1.0 / n);
    chkmin(sx, t * c * 2 + n * c * c);
    c = floor(- t * 1.0 / n);
    chkmin(sx, t * c * 2 + n * c * c);
    rep(i, n) chkmin(Ans, sx - 2 * ((i ? ans[i - 1] : 0) + ans[n + i - 1]) + sum);
    printf("%d\n", Ans);
}

int main()
{
    File();
    Init();
    Solve();
    return 0;
}
//饮马渡秋水,水寒风似刀。
//平沙日未没,黯黯见临洮。
//昔日长城战,咸言意气高。
//黄尘足今古,白骨乱蓬蒿。
//--王昌龄《塞下曲・其二》
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值