Description
Solution
题目要求的其实就是把 {yi} { y i } 移动后的
关于求 c c
发现与有关的是 c2+2c(xi−yi) 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;
}
//饮马渡秋水,水寒风似刀。
//平沙日未没,黯黯见临洮。
//昔日长城战,咸言意气高。
//黄尘足今古,白骨乱蓬蒿。
//--王昌龄《塞下曲・其二》