[AH/HNOI2017]礼物
\]
\]
\]
七十分暴力代码(暴力分贼多)
# include <bits/stdc++.h>
# define RG register
# define IL inline
# define Fill(a, b) memset(a, b, sizeof(a))
using namespace std;
typedef long long ll;
const int _(1e5 + 10);
IL ll Read(){
char c = '%'; ll x = 0, z = 1;
for(; c > '9' || c < '0'; c = getchar()) if(c == '-') z = -1;
for(; c >= '0' && c <= '9'; c = getchar()) x = x * 10 + c - '0';
return x * z;
}
int n, m;
ll sqx, sqy, sx, sy, x[_], y[_], ans = -1e18, mn = 1e18;
int main(RG int argc, RG char *argv[]){
n = Read(); m = Read();
for(RG int i = 1; i <= n; ++i) x[i + n] = x[i] = Read(), sx += x[i], sqx += x[i] * x[i];
for(RG int i = 1; i <= n; ++i) y[i] = Read(), sy += y[i], sqy += y[i] * y[i];
for(RG int i = 0; i < n; ++i){
RG ll cnt = 0;
for(RG int j = 1; j <= n; ++j) cnt += x[j + i] * y[j];
ans = max(ans, cnt);
}
for(RG int c = -m; c <= m; ++c) mn = min(mn, 1LL * n * c * c + 1LL * 2 * c * (sx - sy) - 2 * ans);
printf("%lld\n", mn + sqx + sqy);
return 0;
}
\]
\]
把y反转,再倍长,跑一遍FFT,取有用的中间一段的最大值
再枚举c求解即可
# include <bits/stdc++.h>
# define RG register
# define IL inline
# define Fill(a, b) memset(a, b, sizeof(a))
using namespace std;
typedef long long ll;
const int _(4e5 + 10);
const double Pi(acos(-1));
IL ll Read(){
char c = '%'; ll x = 0, z = 1;
for(; c > '9' || c < '0'; c = getchar()) if(c == '-') z = -1;
for(; c >= '0' && c <= '9'; c = getchar()) x = x * 10 + c - '0';
return x * z;
}
struct Complex{
double real, image;
IL Complex(){ real = image = 0; }
IL Complex(RG double a, RG double b){ real = a; image = b; }
IL Complex operator +(RG Complex B){ return Complex(real + B.real, image + B.image); }
IL Complex operator -(RG Complex B){ return Complex(real - B.real, image - B.image); }
IL Complex operator *(RG Complex B){ return Complex(real * B.real - image * B.image, real * B.image + image * B.real); }
} A[_], B[_];
int n, m, N, M, l, r[_];
ll sx, sy, sqx, sqy, mx = -1e18, ans = 1e18;
IL void FFT(RG Complex *P, RG int opt){
for(RG int i = 0; i < N; i++) if(i < r[i]) swap(P[i], P[r[i]]);
for(RG int i = 1; i < N; i <<= 1){
RG Complex W(cos(Pi / i), opt * sin(Pi / i));
for(RG int p = i << 1, j = 0; j < N; j += p){
RG Complex w(1, 0);
for(RG int k = 0; k < i; ++k, w = w * W){
RG Complex X = P[k + j], Y = w * P[k + j + i];
P[k + j] = X + Y; P[k + j + i] = X - Y;
}
}
}
}
int main(RG int argc, RG char *argv[]){
n = Read() - 1; m = Read();
for(RG int i = 0; i <= n; ++i) A[i].real = Read(), sx += A[i].real, sqx += A[i].real * A[i].real;
for(RG int i = n; i >= 0; --i) B[i + n + 1].real = B[i].real = Read(), sy += B[i].real, sqy += B[i].real * B[i].real;
for(M = 3 * n, N = 1; N <= M; N <<= 1) ++l;
for(RG int i = 0; i < N; ++i) r[i] = (r[i >> 1] >> 1) | ((i & 1) << (l - 1));
FFT(A, 1); FFT(B, 1);
for(RG int i = 0; i < N; ++i) A[i] = A[i] * B[i];
FFT(A, -1);
for(RG int i = n; i <= 2 * n; ++i) mx = max(mx, (ll)(A[i].real / N + 0.5));
for(RG int c = -m; c <= m; ++c) ans = min(ans, 1LL * (n + 1) * c * c + 1LL * 2 * c * (sx - sy));
printf("%lld\n", ans + sqx + sqy - 2 * mx);
return 0;
}
[AH/HNOI2017]礼物的更多相关文章
- bzoj 4827: [Hnoi2017]礼物 [fft]
4827: [Hnoi2017]礼物 题意:略 以前做的了 化一化式子就是一个卷积和一些常数项 我记着确定调整值还要求一下导... #include <iostream> #include ...
- P3723 [AH2017/HNOI2017]礼物
题目链接:[AH2017/HNOI2017]礼物 题意: 两个环x, y 长度都为n k可取 0 ~ n - 1 c可取任意值 求 ∑ ( x[i] - y[(i + k) % n + 1] ...
- 【BZOJ4827】 [Hnoi2017]礼物
BZOJ4827 [Hnoi2017]礼物 Solution 如果一串数的增加,不就等于另一串数减吗? 那么我们可以把答案写成另一个形式: \(ans=\sum_{i=1}^n(x_i-y_i+C)^ ...
- 4827: [Hnoi2017]礼物
4827: [Hnoi2017]礼物 链接 分析: 求最小的$\sum_{i=1}^{n}(x_i-y_i)^2$ 设旋转了j位,每一位加上了c. $\sum\limits_{i=1}^{n}(x_{ ...
- 【LG3723】[AHOI2017/HNOI2017]礼物
[LG3723][AHOI2017/HNOI2017]礼物 题面 洛谷 题解 首先我们将\(c\)看作一个可以为负的整数,那么我们就可以省去讨论在哪个手环加\(c\)的繁琐步骤了 设我们当前已经选好了 ...
- 洛谷 P3723 [AH2017/HNOI2017]礼物 解题报告
P3723 [AH2017/HNOI2017]礼物 题目描述 我的室友最近喜欢上了一个可爱的小女生.马上就要到她的生日了,他决定买一对情侣手环,一个留给自己,一个送给她.每个手环上各有 \(n\) 个 ...
- [BZOJ4827][Hnoi2017]礼物(FFT)
4827: [Hnoi2017]礼物 Time Limit: 20 Sec Memory Limit: 512 MBSubmit: 1315 Solved: 915[Submit][Status] ...
- [Luogu P3723] [AH2017/HNOI2017]礼物 (FFT 卷积)
题面 传送门:洛咕 Solution 调得我头大,我好菜啊 好吧,我们来颓柿子吧: 我们可以只旋转其中一个手环.对于亮度的问题,因为可以在两个串上增加亮度,我们也可以看做是可以为负数的. 所以说,我们 ...
- 笔记-[AH2017/HNOI2017]礼物
笔记-[AH2017/HNOI2017]礼物 [AH2017/HNOI2017]礼物 \[\begin{split} ans_i=&\sum_{j=1}^n(a_j-b_j+i)^2\\ =& ...
随机推荐
- CocosCreator游戏开发---菜鸟学习之路(一)
PS(废话): 辞职后在家好久好久了,久到经济不允许了,接着就准备再次出去找工作了,然而工作哪有那么好找,特别是像我这种菜鸟.而且我还准备转行,准备去做游戏,技能等级接近于0,那工作就更难找了.既然如 ...
- 浅讲JUnit
JUnit单元简介: JUnit ----是一个开发源码的java测试框架,用于编写和运行可重复的测试,它是用于单元测试框架体系xUnit的一个实例, 用于java语言,主要用于白盒测试,回 ...
- Linux-安装FFmpeg
FFmpeg官网:http://www.ffmpeg.org 官网介绍 FFmpeg is the leading multimedia framework, able to decode, enco ...
- oracle 11.2.0.2以后对数据库用户名重命名
本文来自我的github pages博客http://galengao.github.io/ 即www.gaohuirong.cn [转自]http://www.xifenfei.com/2012/0 ...
- intellij idea maven 工程生成可执行的jar
新建maven 工程 写hello world 修改pom.xml 文件 <build> <pluginManagement> <plugins> <plug ...
- Hibernate学习(三)一对多数据的保存
保存习惯代码 @Test public void saveTwoTableTest(){ Transaction transaction = null; try { transaction = ses ...
- java中try catch块的使用
对于关流操作的时候,最好采用如下语句块: InputStream in=......; try{ try{ //some statemenet }finally{ //close stream in. ...
- Shell脚本的颜色样式及属性控制
首先看一下格式 echo -e "\033[字背景颜色:文字颜色m字符串\033[0m" 举例 echo -e "\033[41;36m 字体 \033[0m" ...
- 1 Python数据类型--
常见的Python数据类型: (1)数值类型:就是平时处理的数字(整数.浮点数) (2)序列类型:有一系列的对象并排或者排列的情况.如字符串(str),列表(list),元组(tuple)等 (3)集 ...
- JS-随机生成的密码
randPassword(size) =>{ //数组 let seed = new Array('A','B','C','D','E','F','G','H','I','J','K','L', ...