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)如果需 ...
随机推荐
- Springboot-webscoket with sockjs
新建springboot maven工程,引入以下包 <dependency> <groupId>org.springframework.boot</groupId> ...
- H3C 单区域OSPF配置示例一(续)
- ubuntu snmp 安装与配置
0.说明 关于一个完整的教程,还是那句话,国内的要么不完整,要么就太旧了,而且思路也不清晰,所以这里写一篇完整的给大家分享一下. 虽然对于Linux主机的监控可以通过执行特定的命令来完成,但是相比之后 ...
- H3C RIPv2配置任务
- 闲来无事写一个jquery计算器,没有进行封装......
<!doctype html> <html> <head> <meta charset="utf-8" /> <title&g ...
- Web中的通配符
/**的意思是所有文件夹及里面的子文件夹 /*是所有文件夹,不含子文件夹 /是web项目的根目录 http://www.coderanch.com/t/364782/Servlets/java ...
- Codeforces Round #587 C. White Sheet(思维+计算几何)
传送门 •题意 先给一个白矩阵,再两个黑矩阵 如果两个黑矩阵能把白矩阵包含,则输出NO 否则输出YES •思路 计算几何题还是思维题呢? 想起了上初中高中做几何求面积的题 这个就类似于那样 包含的话分 ...
- c# T4模板生成实体类(sqlserver)
1.用vs新建tt文件. 2.tt文件保存就自动运行 3.tt文件代码如下,设置生成cs文件的命名空间和生成地址 <#@ template language="C#" hos ...
- DEVOPS技术实践_18:Jenkins的Pinpeline对于参数的使用
因为最近使用Pipeline声明式语法构建项目,但是最近项目的参数设置较多,特地的来学习一下关于参数的调用和测试,主要式从一个大神那里学习的,结尾回贴上大神的博客链接 1 构建一个pipeline项目 ...
- blackarch 安装指南
建议直接 www.blackangle.cn/BlackArchGuide.htm v\:* {behavior:url(#default#VML);} o\:* {behavior:url(#de ...