cf - 429D
Iahub and Sorin are the best competitive programmers in their town. However, they can't both qualify to an important contest. The selection will be made with the help of a single problem. Blatnatalag, a friend of Iahub, managed to get hold of the problem before the contest. Because he wants to make sure Iahub will be the one qualified, he tells Iahub the following task.
You're given an (1-based) array a with n elements. Let's define function f(i, j) (1 ≤ i, j ≤ n) as (i - j)2 + g(i, j)2. Function g is calculated by the following pseudo-code:
int g(int i, int j) {
int sum = 0;
for (int k = min(i, j) + 1; k <= max(i, j); k = k + 1)
sum = sum + a[k];
return sum;
}
Find a value mini ≠ j f(i, j).
Probably by now Iahub already figured out the solution to this problem. Can you?
Input
The first line of input contains a single integer n (2 ≤ n ≤ 100000). Next line contains n integers a[1], a[2], ..., a[n] ( - 104 ≤ a[i] ≤ 104).
Output
Output a single integer — the value of mini ≠ j f(i, j).
Example
4
1 0 0 -1
1
2
1 -1
2 题目分析 : 首先要对公式进行变形 , 所给的函数就是让求一个前缀和,那么就是由公式的形式就可得到 (i-j)^2 + (sum[i]-sum[j])^2 , 那么不就是转换成一个平面上的最近两点的距离的平方了吗? 代码示例 :
const int eps = 1e5+5;
const double pi = acos(-1.0);
const int inf = 1<<29;
#define Max(a,b) a>b?a:b
#define Min(a,b) a>b?b:a
#define ll long long struct node
{
ll x, y;
}pre[eps], pt[eps]; bool cmpxy(node a, node b){
if (a.x == b.y) return a.y < b.y;
else return a.x < b.x;
} ll dis(ll i, ll j){
return (pre[i].x-pre[j].x)*(pre[i].x-pre[j].x)+(pre[i].y-pre[j].y)*(pre[i].y-pre[j].y);
} ll dis2(ll i, ll j){
return (pt[i].x-pt[j].x)*(pt[i].x-pt[j].x)+(pt[i].y-pt[j].y)*(pt[i].y-pt[j].y);
} bool cmpy(node a, node b){
if (a.y == b.y) return a.x < b.x;
else return a.y < b.y;
} ll close_pair(ll l, ll r){
ll d = 999999999999999;
if (l == r) return d;
if (l + 1 == r) return dis(l, r);
ll m = (l + r) >> 1;
ll d1 = close_pair(l, m);
ll d2 = close_pair(m+1, r);
d = min(d1, d2);
ll k = 0;
for(ll i = l; i <= r; i++){
if ((pre[i].x-pre[m].x)*(pre[i].x-pre[m].x) < d) pt[k++] = pre[i];
}
sort(pt, pt+k, cmpy); for(ll i = 0; i < k; i++){
for(ll j = i+1; j < k && (pt[j].y-pt[i].y)*(pt[j].y-pt[i].y) < d; j++){
ll dd = dis2(i, j);
d = min(dd, d);
}
}
return d;
} int main() {
//freopen("in.txt", "r", stdin);
//freopen("out.txt", "w", stdout);
ll n, x; cin >> n;
ll sum = 0;
for(ll i = 1; i <= n; i++){
scanf("%lld", &x);
sum += x;
pre[i].x = i; pre[i].y = sum;
}
sort(pre+1, pre+1+n, cmpxy);
printf("%lld\n", close_pair(1, n));
return 0;
}
cf - 429D的更多相关文章
- CF数据结构练习
1. CF 438D The Child and Sequence 大意: n元素序列, m个操作: 1,询问区间和. 2,区间对m取模. 3,单点修改 维护最大值, 取模时暴力对所有>m的数取 ...
- ORA-00494: enqueue [CF] held for too long (more than 900 seconds) by 'inst 1, osid 5166'
凌晨收到同事电话,反馈应用程序访问Oracle数据库时报错,当时现场现象确认: 1. 应用程序访问不了数据库,使用SQL Developer测试发现访问不了数据库.报ORA-12570 TNS:pac ...
- cf之路,1,Codeforces Round #345 (Div. 2)
cf之路,1,Codeforces Round #345 (Div. 2) ps:昨天第一次参加cf比赛,比赛之前为了熟悉下cf比赛题目的难度.所以做了round#345连试试水的深浅..... ...
- cf Round 613
A.Peter and Snow Blower(计算几何) 给定一个点和一个多边形,求出这个多边形绕这个点旋转一圈后形成的面积.保证这个点不在多边形内. 画个图能明白 这个图形是一个圆环,那么就是这个 ...
- ARC下OC对象和CF对象之间的桥接(bridge)
在开发iOS应用程序时我们有时会用到Core Foundation对象简称CF,例如Core Graphics.Core Text,并且我们可能需要将CF对象和OC对象进行互相转化,我们知道,ARC环 ...
- [Recommendation System] 推荐系统之协同过滤(CF)算法详解和实现
1 集体智慧和协同过滤 1.1 什么是集体智慧(社会计算)? 集体智慧 (Collective Intelligence) 并不是 Web2.0 时代特有的,只是在 Web2.0 时代,大家在 Web ...
- CF memsql Start[c]UP 2.0 A
CF memsql Start[c]UP 2.0 A A. Golden System time limit per test 1 second memory limit per test 256 m ...
- CF memsql Start[c]UP 2.0 B
CF memsql Start[c]UP 2.0 B B. Distributed Join time limit per test 1 second memory limit per test 25 ...
- CF #376 (Div. 2) C. dfs
1.CF #376 (Div. 2) C. Socks dfs 2.题意:给袜子上色,使n天左右脚袜子都同样颜色. 3.总结:一开始用链表存图,一直TLE test 6 (1)如果需 ...
随机推荐
- C#将可编译为本地机器码
微软宣布了.net native的开发者预览版,详见这里. 这是一个大家期待了很多年的特性.每年在技术论坛上都有无数的人问,C#能否编译成本地机器码. 有了这个特性之后,更多开发商会开始选择C#来开发 ...
- git如何移除某文件的版本控制
1:还没有加到版本控制中 (1)还没有git add 在 .gitignore中添加 (2)已经git add 先 git rm -r --cached 文件 在 .gitig ...
- P1068 压缩技术
题目描述 设某汉字由N × N的0和1的点阵图案组成. 我们依照以下规则生成压缩码.连续一组数值:从汉字点阵图案的第一行第一个符号开始计算,按书写顺序从左到右,由上至下.第一个数表示连续有几个0,第二 ...
- The Function() Constructor
Functions are usually defined using the function keyword, either in the form of a function definitio ...
- setTimeout第三个参数
在廖雪峰大神的官方网站看nodejs的时候,发现自己对Promise函数不甚了解,于是转去看Promise函数,看到了这段代码: function multiply(input) { return n ...
- D3.js力导向图中新增节点及新增关系连线示例
大家在使用D3.js中的力导向图时,基本都会遇到动态增加节点及连线的需求,这里记录一下我的实现方式. 话不多说,先放代码: <!DOCTYPE html> <html lang=&q ...
- Linux 内核 EISA 总线
扩展 ISA (EISA) 总线是一个对 ISA 的 32-位 扩展, 带有一个兼容的接口连接器; ISA 设备板可被插入一个 EISA 连接器. 增加的线在 ISA 接触之下被连接. 如同 PCI ...
- 5.29 SD省队培训D1
5.29 SD省队培训D1 自闭的一天 T1 梦批糼 先咕一咕(两天之内一定补上) T2 等你哈苏德 继续咕(一星期之内补上) T3喜欢最最痛 四十分做法: 首先,我们发现同一个点加两条额外边是一件非 ...
- 2018-2-13-win10-uwp-判断文件存在
title author date CreateTime categories win10 uwp 判断文件存在 lindexi 2018-2-13 17:23:3 +0800 2018-2-13 1 ...
- Tomcat 类加载器打破双亲委派模型
我们分为4个部分来探讨: 1. 什么是类加载机制? 2. 什么是双亲委任模型? 3. 如何破坏双亲委任模型? 4. Tomcat 的类加载器是怎么设计的? 我想,在研究tomcat 类加载之前,我们复 ...