[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\\ =& ...
随机推荐
- 在Arrays.asList()引发的问题中进一步学习集合与泛型等内容
前言 最近在网上看到一个问题,情况类似如下(记为问题1): public class Demo { public static void main(String[] args) { System.ou ...
- 解决`向github提交代码是老要输入用户名密码`
在命令行输入命令:git config --global credential.helper store☞ 这一步会在用户目录下的.gitconfig文件最后添加: [credential] help ...
- 基于MATLAB2016b图形化设计自动生成Verilog语言的积分模块及其应用
在电力电子变流器设备中,常常需要计算发电量,由于电力电子变流器设备一般是高频变流设备,所以发电量的计算几乎时实时功率的积分,此时就会用到一个积分模块.发电量计算的公式如下:Q=∫P. FPGA由于其并 ...
- C# Swagger 生成接口文档
一直听说Swagger是做Web API文档的好工具,这次手里暂时没什么事,类体验下它的强大之处.下面是使用Swashbuckle.net 给asp.net web API添加文档的简要步骤. 参考地 ...
- MysqL错误之_ERROR! MySQL server PID file could not be found!
在配置Mysql主从GTID模式下,启动Mysql服务时出现报错,搜索了一番,找到了一个简单可靠的方法,直接成功.如果遇到相同问题没有解决的童鞋,那就去试一下很多其他方案,如,强制杀掉进程重启,修改其 ...
- Eventlog控件的使用
CreateEventSource 已重载. 建立一个能够将事件信息写入到系统的特定日志中的应用程序. Delete 已重载. 移除日志资源. DeleteEventSource 已重载. 从事件日志 ...
- 《android开发艺术探索》读书笔记(十一)--Android的线程和线程池
接上篇<android开发艺术探索>读书笔记(十)--Android的消息机制 No1: 在Android中可以扮演线程角色的有很多,比如AsyncTask.IntentService.H ...
- UVA - 1631 Locker 记忆化搜索
题意:给定两个密码串,每次可以让1~3个相邻的密码向上或者向下滚动,每个密码是 ,问最少需要多少次滚动可以让原串成为目标串? 思路:假设当前要让第i位密码还原,我们可以同时转动,不同的转动方式会影响后 ...
- soj4538: ShouHuXueJie Problem DFS
类似八皇后,暴力深搜. 其实我觉得这题目叙述不是很好,如果答案为0呢,难道不输出? AC代码: #include<cstdio> #include<cstring> #incl ...
- ARC068E - Snuke Line
原题链接 题意简述 给出个区间和.求对于任意,有多少个区间包含的倍数. 题解 考虑怎样的区间不包含的倍数. 对于的倍数和,满足的区间不包含任何的倍数. 于是转化为二维数点问题,可以用可持久化线段树解决 ...