【题目链接】: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的更多相关文章

  1. 【Codeforces 429D】 Tricky Function

    [题目链接] http://codeforces.com/problemset/problem/429/D [算法] 令Si = A1 + A2 + ... + Ai(A的前缀和) 则g(i,j) = ...

  2. 【codeforces 415D】Mashmokh and ACM(普通dp)

    [codeforces 415D]Mashmokh and ACM 题意:美丽数列定义:对于数列中的每一个i都满足:arr[i+1]%arr[i]==0 输入n,k(1<=n,k<=200 ...

  3. 【codeforces 604D】Moodular Arithmetic

    time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...

  4. 【codeforces 602D】Lipshitz Sequence

    time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...

  5. 【codeforces 707E】Garlands

    [题目链接]:http://codeforces.com/contest/707/problem/E [题意] 给你一个n*m的方阵; 里面有k个联通块; 这k个联通块,每个连通块里面都是灯; 给你q ...

  6. 【codeforces 707C】Pythagorean Triples

    [题目链接]:http://codeforces.com/contest/707/problem/C [题意] 给你一个数字n; 问你这个数字是不是某个三角形的一条边; 如果是让你输出另外两条边的大小 ...

  7. 【codeforces 709D】Recover the String

    [题目链接]:http://codeforces.com/problemset/problem/709/D [题意] 给你一个序列; 给出01子列和10子列和00子列以及11子列的个数; 然后让你输出 ...

  8. 【codeforces 709B】Checkpoints

    [题目链接]:http://codeforces.com/contest/709/problem/B [题意] 让你从起点开始走过n-1个点(至少n-1个) 问你最少走多远; [题解] 肯定不多走啊; ...

  9. 【codeforces 709C】Letters Cyclic Shift

    [题目链接]:http://codeforces.com/contest/709/problem/C [题意] 让你改变一个字符串的子集(连续的一段); ->这一段的每个字符的字母都变成之前的一 ...

随机推荐

  1. Jmeter中模拟多用户执行多场景操作

    1.其实一个用户组就是一个场景(Thread Group).可以在一个测试计划中进行多个场景的执行,在测试计划下加一个全局的User Defined Variables,在这个里面可以设置执行总数to ...

  2. 说说Shell在代码重构中的应用

    说说Shell在代码重构中的应用    出处信息 出处:http://blogread.cn/it/article/3426?f=wb 代码重构(Code refactoring)有时是很枯燥的,字符 ...

  3. Activiti Workflow HelloWorld 示例与测试环境搭建

    作者:Rock 出处:http://www.ecmkit.com/zh-hans/2012/03/21/activiti-workflow-hell Activiti Workflow HelloWo ...

  4. vivo输入法 需求分析

    我使用的输入法:vivo输入法 1,用户界面:界面为白色和灰色,整体简洁大方,个人而言外观挺不错.但是不能自定义界面,更改背景图片或是主题. 2,记住用户选择:可记忆上次使用后的键盘方式(26键或是9 ...

  5. 3.is null和is not null

    3.WHERE中使用is null和is not null   //查询工资是null空值的人   select * from person where salary is null;   //查询工 ...

  6. Linux Mint 17.1 安装全配置

    Linux Mint 17.1 安装全配置 I. 前言 由于自己的本子出现了一些故障需要重新安装系统,就上网看看今年4,5月份发布的一些新的发行版来试试.原先电脑上安装的是opensuse13.2, ...

  7. Android DatePickerDialog样式不一致的问题

    三星和华为的平板上,DatePickerDialog的显示样式不一致.三星的仅仅显示月日年选择框,而华为的平板上另外还显示了日历表.代码同样. 可能是系统控件做了部分改动,后来你发现是能够设置的: D ...

  8. NYOJ 815 三角形【海伦公式】

    /* 关键点:海伦公式 解题人:lingnichong 解题时间:2014-10-04 21:48:47 解题体会:海伦公式的使用 */ 三角形 时间限制:1000 ms  |  内存限制:65535 ...

  9. MongoDB数据查询详解

    查询全部 ​ db.infos.find(); db.infos.find({"url":"www.baidu.com"}); id不要显示出来 db.info ...

  10. javascript系列-class4.函数

    欢迎加入前端交流群来py: 转载请标明出处!                   在火影的世界中存在忍术,忍术是把强大的能量集中在一起以各种各样不同的形式发射出来.怎样使用各种各样的忍术那?通过结印. ...