【题目链接】: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. django patch

    import datetime import pytz from django.apps import AppConfig from django.db.models.fields import Da ...

  2. prettier 与 eslint 对比

    Linters have two categories of rules: 代码修正一般有两种规则: Formatting rules: eg: max-len, no-mixed-spaces-an ...

  3. web_custom_request函数做get接口测试

    最近研究了使用loadrunner做接口测试,刚开始一直不成功,后来加了QQ群,遇到大神了,经指导终于成功 下面是具体实例代码: //{"signIOS":1,"sign ...

  4. [CSS3] CSS Background Images

    Body with background image and gradient html { background: linear-gradient(#000, white) no-repeat; h ...

  5. C++ OTL MySQL(Windows/Linux) V8.1

    Windows每秒钟10000条以上插入:Linux每秒插入300条以上.Q269752451 输出截图: Linux输出: Windows输出: 有须要的联系 QQ 3508551694 water ...

  6. Oracle Study之--Oracle 单实例11.2.0.1.0升级到11.2.0.3.0

    Oracle Study之--Oracle 单实例11.2.0.1.0升级到11.2.0.3.0 系统环境: 操作系统:RedHat EL6(64位) Oracle:    Oracle 11gR2 ...

  7. Java的接口总结

    Java最主要的封装是class.除此之外还有接口interface. 这段时间一直在想接口有什么作用呢.有了接口有哪些优点呢.结合网络上各位大神的文章,接口的作用大概体如今下面几个方面. 1.回调 ...

  8. war包放入tomcat

    1.找到tomcat的安装路径(如:D:\example\download\set\apache-tomcat-7.0.23) 2.D:\example\download\set\apache-tom ...

  9. 51nod-1253: Kundu and Tree

    [传送门:51nod-1253] 简要题意: 给出一棵n个点的树,树上的边要么为黑,要么为红 求出所有的三元组(a,b,c)的数量,满足a到b,b到c,c到a三条路径上分别有至少一条红边 题解: 显然 ...

  10. JAXB xml与javaBean的转换

    转自:https://blog.csdn.net/lydong_/article/details/79812626 `1. 1.不认识到犯错,然后得到永久的教训. 也不是所谓的教训吧,真正的教训来自于 ...