【codeforces 429D】Tricky Function
【题目链接】:http://codeforces.com/problemset/problem/429/D
【题意】
给你n个数字;
让你求出一段区间[l,r]
使得
(r−l)2+(∑rl+1a[i])2最小
【题解】
求出前缀和数组sum[i];
可以发现,如果把数组的下标i作为第一维坐标(x),前缀和sum[i]作为第二维坐标(y);
所求的式子就是任意两点之间的距离平方;
问题转化成:已知平面上的n个点;
求最近的两个点之间的距离的平方;
这个可以用分治的方法搞出来;
(感觉就是个剪枝的暴力);
据说复杂度是N⋅log2N
【Number Of WA】
2
【完整代码】
#include <bits/stdc++.h>
using namespace std;
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define LL long long
#define rep1(i,a,b) for (int i = a;i <= b;i++)
#define rep2(i,a,b) for (int i = a;i >= b;i--)
#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define ms(x,y) memset(x,y,sizeof x)
#define Open() freopen("F:\\rush.txt","r",stdin)
#define Close() ios::sync_with_stdio(0),cin.tie(0)
typedef pair<int,int> pii;
typedef pair<LL,LL> pll;
const int dx[9] = {0,1,-1,0,0,-1,-1,1,1};
const int dy[9] = {0,0,0,-1,1,-1,1,-1,1};
const double pi = acos(-1.0);
const int N = 1e5+100;
const LL INF = 8e18 + 1;
//4e18
//8e18
struct abc{
LL x,y;
};
LL a[N],sum[N];
abc b[N],c[N];
int n;
LL sqr(LL x){
return x*x;
}
LL dis(abc a,abc b){
LL temp = 0;
temp += sqr(a.x-b.x);
temp += sqr(a.y-b.y);
return temp;
}
LL query(int l,int r){
LL ret = INF;
if (l>=r) return ret;
if (l+1==r) return dis(b[l],b[r]);
int m = (l+r)>>1,k = 0;
LL t1 = query(l,m),t2 = query(m+1,r),temp;
ret = min(t1,t2);
rep2(i,m,l){
temp = sqr(b[i].x-b[m].x);
if (temp>ret) break;
c[++k] = b[i];
}
rep1(i,m+1,r){
temp = sqr(b[i].x-b[m].x);
if (temp>ret) break;
c[++k] = b[i];
}
sort(c+1,c+1+k,[&] (abc a,abc b) {return a.y<b.y;});
rep1(i,1,k)
rep1(j,i+1,k){
temp = sqr(c[j].y-c[i].y);
if (temp > ret) break;
ret = min(ret,dis(c[i],c[j]));
}
return ret;
}
int main(){
//Open();
Close();
cin >> n;
rep1(i,1,n) cin >> a[i];
rep1(i,1,n) sum[i] = sum[i-1] + a[i];
rep1(i,1,n){
b[i].x = i,b[i].y = sum[i];
}
sort(b+1,b+1+n,[&] (abc a,abc b) { return a.x < b.x;});
cout << query(1,n) << endl;
return 0;
}
【codeforces 429D】Tricky Function的更多相关文章
- 【Codeforces 429D】 Tricky Function
[题目链接] http://codeforces.com/problemset/problem/429/D [算法] 令Si = A1 + A2 + ... + Ai(A的前缀和) 则g(i,j) = ...
- 【codeforces 415D】Mashmokh and ACM(普通dp)
[codeforces 415D]Mashmokh and ACM 题意:美丽数列定义:对于数列中的每一个i都满足:arr[i+1]%arr[i]==0 输入n,k(1<=n,k<=200 ...
- 【codeforces 604D】Moodular Arithmetic
time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...
- 【codeforces 602D】Lipshitz Sequence
time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...
- 【codeforces 707E】Garlands
[题目链接]:http://codeforces.com/contest/707/problem/E [题意] 给你一个n*m的方阵; 里面有k个联通块; 这k个联通块,每个连通块里面都是灯; 给你q ...
- 【codeforces 707C】Pythagorean Triples
[题目链接]:http://codeforces.com/contest/707/problem/C [题意] 给你一个数字n; 问你这个数字是不是某个三角形的一条边; 如果是让你输出另外两条边的大小 ...
- 【codeforces 709D】Recover the String
[题目链接]:http://codeforces.com/problemset/problem/709/D [题意] 给你一个序列; 给出01子列和10子列和00子列以及11子列的个数; 然后让你输出 ...
- 【codeforces 709B】Checkpoints
[题目链接]:http://codeforces.com/contest/709/problem/B [题意] 让你从起点开始走过n-1个点(至少n-1个) 问你最少走多远; [题解] 肯定不多走啊; ...
- 【codeforces 709C】Letters Cyclic Shift
[题目链接]:http://codeforces.com/contest/709/problem/C [题意] 让你改变一个字符串的子集(连续的一段); ->这一段的每个字符的字母都变成之前的一 ...
随机推荐
- 《Exception》第八次团队作业:Alpha冲刺(大结局)
一.项目基本介绍 项目 内容 这个作业属于哪个课程 任课教师博客主页链接 这个作业的要求在哪里 作业链接地址 团队名称 Exception 作业学习目标 1.掌握软件测试基础技术.2.学习迭代式增量软 ...
- PAT 天梯赛练习集 L2-016. 愿天下有情人都是失散多年的兄妹
题目链接:https://www.patest.cn/contests/gplt/L2-016 呵呵.大家都知道五服以内不得通婚,即两个人最近的共同祖先如果在五代以内(即本人.父母.祖父母.曾祖父母. ...
- 2019-03-15 Python time datetime 获取当下时间 和 格式化时间
import datetime start_date='2018-01-10' end_date='2019-01-10'# 转换为2018-01-10 00:00:00start_date=date ...
- Unity 常用常找的东西存放
本文章由cartzhang编写,转载请注明出处. 所有权利保留. 文章链接:http://blog.csdn.net/cartzhang/article/details/50483316 作者:car ...
- 20130912.Windows下常用命令的使用(不断更新)
Win+R================================ cmd => 命令行 lpksetup => 弹出安装或者卸载Windows显示语言 ipconfig => ...
- input输入框获取焦点是背景黄色去除
input:-webkit-autofill { -webkit-box-shadow: 0 0 0px 1000px white inset !important;}
- java ee服务器/应用服务器的理解
42.由Apache.Sun 和其他一些公司及个人共同开发而成.由于有了Sun 的参与和支持,最新的Servlet 和JSP 规范总是能在Tomcat 中得到体现.43.可以这样认为,当在一台机器上配 ...
- Cocos2d-x使用Luajit将Lua脚本编译为bytecode,实现加密 更新
项目要求对lua脚本进行加密,查了一下相关的资料 ,得知lua本身能够使用luac将脚本编译为字节码(bytecode)从而实现加密,试了一下,确实可行. 以下是使用原生的lua解释器编译字节码: 1 ...
- 使用Java语言实现,自己主动生成10个整数(1~100,求出生成数列中的最大值和最小值,不同意使用Arrays类的sort方法
这是考察主要的java基础,没啥难点,直接上代码,近期在准备面试,所以做一些基础的面试题练练手 public class Demo1 { public static void main(String[ ...
- html5中调用摄像头拍照
方法: getCamera: 获取摄像头管理对象 对象: Camera: 摄像头对象 CameraOption: JSON对象.调用摄像头的參数 PopPosition: JSON对象,弹出拍照或摄像 ...