题号 NC20122
名称 [HNOI2017]礼物
来源 [HNOI2017]
戳我进入往期每日一题汇总贴~
往期每日一题二期题单
如果你在题库做题时遇到了喜欢的题目,欢迎推荐给邓老师~ 点击查看详情
欢迎给每日一题投稿,投稿需要提供牛客题库里的题目+题解 投稿有牛币奖励,可发站内信给王清楚或联系QQ 234186389
每日一题QQ群:659028468
题解
[HNOI2017]礼物
我们要使最小,我们能对两个序列进行一些操作。
对序列的
操作,我们得到
。
对序列的
操作,我们得到
。
有,得到
$a, u
\sum\limits_{i = 1} ^{n} (a_i ^ 2 + u_i ^ 2)$一定是一个定值,
,其中
也一定是一个定值,然后这就是一个开口向上的二次函数有极小值,可分类讨论求得,
接下来我们考虑如何求解的最大值了
由于我们可以对其中任意的一个串循环移动,假设我们对逆时针移动
次,上式就变成了
。
要是是一个定值就好了,这就是多项式相乘的某一项,我们就能通过
求解,从这一点出发,我们构造
为一个常数。
另,有
,上式有
,
,
对于确定的,这就是一个多项式相乘的某一项了,我们把数组
加倍,然后求一次
就能得到所有
的答案了,
当然为了运算方遍我们得提前把数组给翻转一下。
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const double pi = acos(-1.0);
const int N = 1e6 + 10;
struct Complex {
double r, i;
Complex(double _r = 0, double _i = 0) : r(_r), i(_i) {}
}a[N], b[N];
Complex operator + (const Complex & a, const Complex & b) {
return Complex(a.r + b.r, a.i + b.i);
}
Complex operator - (const Complex & a, const Complex & b) {
return Complex(a.r - b.r, a.i - b.i);
}
Complex operator * (const Complex & a, const Complex & b) {
return Complex(a.r * b.r - a.i * b.i, a.r * b.i + a.i * b.r);
}
int r[N];
void fft(Complex * f, int lim, int rev) {
for (int i = 0; i < lim; i++) {
if (r[i] < i) {
swap(f[i], f[r[i]]);
}
}
for (int i = 1; i < lim; i <<= 1) {
Complex wn = Complex(cos(pi / i), rev * sin(pi / i));
for (int p = i << 1, j = 0; j < lim; j += p) {
Complex w = Complex(1, 0);
for (int k = 0; k < i; k++, w = w * wn) {
Complex x = f[j + k], y = w * f[i + j + k];
f[j + k] = x + y, f[i + j + k] = x - y;
}
}
}
if (rev == -1) {
for(int i = 0; i < lim; i++) {
f[i].r /= lim;
}
}
}
void get_r(int lim) {
for (int i = 0; i < lim; ++i) {
r[i] = (i & 1) * (lim >> 1) + (r[i >> 1] >> 1);
}
}
int main() {
// freopen("in.txt", "r", stdin);
// freopen("out.txt", "w", stdout);
// ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
ll ans = 0, res = 0;
int n, m, lim = 1;
scanf("%d %d", &n, &m);
for (int i = 1; i <= n; i++) {
scanf("%lf", &a[n - i + 1].r);
}
for (int i = 1; i <= n; i++) {
scanf("%lf", &b[i].r);
b[i + n]= b[i];
}
for (int i = 1; i <= n; i++) {
ans += a[i].r * a[i].r + b[i].r * b[i].r;
res += a[i].r - b[i].r;
}
ll c1 = floor(res * 1.0 / n), c2 = ceil(res * 1.0 / n);
ans += min(n * c1 * c1 - 2 * c1 * res, n * c2 * c2 - 2 * c2 * res);
n <<= 2;
while (lim < n) lim <<= 1;
n >>= 2;
get_r(lim);
fft(a, lim, 1);
fft(b, lim, 1);
for (int i = 0; i < lim; i++) {
a[i] = a[i] * b[i];
}
fft(a, lim, -1);
res = 0;
for (int i = 1; i <= n; i++) {
res = max(res, ll(a[i + n].r + 0.5));
}
printf("%lld\n", ans - 2 * res);
return 0;
} 欢迎各位大佬来做题写题解,也欢迎大家踊跃在当日讨论贴中提问!
活动奖励:
在牛客博客中写出题解,并回复地址
审核通过可获得(依据题目难度和题解的内容而定)
本道题目1月27日中午12:00之前写的题解有获得牛币资格~
牛客博客开通方式
- 如何开通牛客博客:https://www.nowcoder.com/discuss/202952
- 如何使用博客搬家功能:进入博客--->设置--->底部博客搬家
- 如果你对牛客博客有任何意见或建议:牛客博客意见反馈专贴

全部评论
(3) 回帖