【题目链接】: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. C learn questions list

    \主 题: C语言面试题大汇总,个人觉得还是比较全地!!! 作 者: free131 (白日?做梦!) 信 誉 值: 100 最近因为找工作,收集了很多C语言方面方面的面试题以及答案.现在新工作搞定了 ...

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

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

  3. org.xml.sax.SAXParseException: Failed to read schema document 的原因分析与解决方法

    现象: org.xml.sax.SAXParseException: schema_reference.4: Failed to read schema documen t 'http://www.s ...

  4. HDU 1005 Number Sequence(找规律)

    链接:传送门 题意:略 思路:f(n) = (A * f(n - 1) + B * f(n - 2)) mod 7 -> f(n) = (A * f(n-1)%7 + B * f(n-1)%7) ...

  5. hdu5791 TWO

    hdu5791 TWO 题意 给你两个数串 问你两个数串有多少子串一致 子串不一定是连续的 解法 我们设 \(dp[i][j]\) 表示A串匹配到 i 位,B串匹配到 j 位,一致的子串数.那么我们有 ...

  6. 笔记本安装Archlinux笔记

    同步更新于wendster大佬的个人博客 搬运自我的洛谷博客 可能会不定期更新! 因为前几天给我的小炸鸡加了一根内存条:而且先前装的Xubuntu是32位的,使用极其不方便:再加上wendster大佬 ...

  7. HTML5 基础测试题

          HTML5 基础测试题 1.HTML5 之前的 HTML 版本是什么?() A.HTML 4.01 B.HTML 4 C.HTML 4.1 D.HTML 4.9 2.HTML5 的正确 d ...

  8. 命令行 对MYSQL导入sql

    1 use database name;  //选择使用的数据库 2 mysql>source d:\datafilename.sql  导入sql

  9. HDOJ find the safest road 1596【最短路变形】

    find the safest road Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Ot ...

  10. Composer使用实践

    Composer 是 PHP5.3以上 的一个依赖管理工具.它允许你声明项目所依赖的代码库,它会在你的项目中为你安装他们. 地址在这里 库地址 这里相当于php应用商店,存放着很多库. 这些库,基本上 ...