Codefoces 429 D. Tricky Function
裸的近期点对....
2 seconds
256 megabytes
standard input
standard output
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?
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 a single integer — the value of mini ≠ j f(i, j).
4
1 0 0 -1
1
2
1 -1
2
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm> using namespace std; typedef long long int LL; const int maxn=100100;
const LL INF=1LL<<62; LL a[maxn];
int n; struct POINT
{
LL x,y;
}p[maxn],temp[maxn]; bool cmpxy(POINT a,POINT b)
{
if(a.x!=b.x) return a.x<b.x;
return a.y<b.y;
} bool cmpy(POINT a,POINT b)
{
return a.y<b.y;
} inline LL square(LL x)
{
return x*x;
} LL dist(POINT a,POINT b)
{
return square(a.x-b.x)+square(a.y-b.y);
} LL Close_pair(int left,int right)
{
LL d=INF;
if(left==right) return INF;
if(left+1==right) return dist(p[left],p[right]);
int mid=(left+right)/2;
d=min(Close_pair(left,mid),Close_pair(mid+1,right)); int k=0;
for(int i=left;i<=right;i++)
{
if(square(p[i].x-p[mid].x)<=d)
{
temp[k++]=p[i];
}
}
sort(temp,temp+k,cmpy);
for(int i=0;i<k;i++)
{
for(int j=i+1;j<k&&square(temp[i].y-temp[j].y)<d;j++)
{
d=min(d,dist(temp[i],temp[j]));
}
} return d;
} int main()
{
scanf("%d",&n);
for(int i=1;i<=n;i++)
{
scanf("%I64d",a+i);
p[i].x=i;
p[i].y=p[i-1].y+a[i];
}
sort(p+1,p+1+n,cmpxy);
printf("%I64d\n",Close_pair(1,n));
return 0;
}
Codefoces 429 D. Tricky Function的更多相关文章
- Codeforces Round #245 (Div. 1) 429D - Tricky Function 最近点对
D. Tricky Function Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 codeforces.com/problemset/problem/42 ...
- Codeforces 429D Tricky Function(平面最近点对)
题目链接 Tricky Function $f(i, j) = (i - j)^{2} + (s[i] - s[j])^{2}$ 把$(i, s[i])$塞到平面直角坐标系里,于是转化成了平面最近点 ...
- Codeforces(429D - Tricky Function)近期点对问题
D. Tricky Function time limit per test 2 seconds memory limit per test 256 megabytes input standard ...
- codeforce 429D. Tricky Function (思维暴力过)
题目描述 Iahub and Sorin are the best competitive programmers in their town. However, they can't both qu ...
- 【Codeforces 429D】 Tricky Function
[题目链接] http://codeforces.com/problemset/problem/429/D [算法] 令Si = A1 + A2 + ... + Ai(A的前缀和) 则g(i,j) = ...
- 【codeforces 429D】Tricky Function
[题目链接]:http://codeforces.com/problemset/problem/429/D [题意] 给你n个数字; 让你求出一段区间[l,r] 使得 (r−l)2+(∑rl+1a[i ...
- Codeforces 429D Tricky Function 近期点对
题目链接:点击打开链接 暴力出奇迹. 正解应该是近期点对.以i点为x轴,sum[i](前缀和)为y轴,求随意两点间的距离. 先来个科学的暴力代码: #include<stdio.h> #i ...
- javascript零散要点收集
1.this永远指向函数对象的所有者 2.ECMA-262 把对象(object)定义为“属性的无序集合,每个属性存放一个原始值.对象或函数”.严格来说,这意味着对象是无特定顺序的值的数组. 3.pr ...
- CF数据结构练习
1. CF 438D The Child and Sequence 大意: n元素序列, m个操作: 1,询问区间和. 2,区间对m取模. 3,单点修改 维护最大值, 取模时暴力对所有>m的数取 ...
随机推荐
- $(document).Ready()方法 VS OnLoad事件 VS $(window).load()方法
$(document).Ready()方法 VS OnLoad事件 VS $(window).load()方法接触JQuery一般最先学到的是何时启动事件.在曾经很长一段时间里,在页面载入后引发的事件 ...
- C#实现CAD数据转shape或mdb
jojojojo2002 原文C#实现CAD数据转shape或mdb 本文所指的CAD数据为不带空间参考和扩展数据的数据.如果CAD带了空间参考或是扩展属性数据的话,就要采用图形和属性分离的方法转CA ...
- Delphi RxRichEdit高级操作
unit InsertRichEditUnit;interfaceuses Windows, Messages, SysUtils, Classes, Graphics, Controls, For ...
- iOS优秀博客收录(持续更新)
唐巧 王巍 破船之家 NSHipster Limboy 无网不剩 念茜的博客 Xcode Dev Ted’s Homepage txx’s blog KEVIN BLOG 阿毛的蛋疼地 亚庆的 Blo ...
- searchBar 隐藏
searchBar 隐藏 CGRect newBounds = self.tableView.bounds; newBounds.origin.y = newBounds.origin.y + _he ...
- 解决SQL Server Always 日志增大的问题-摘自网络
配置了Alwayson之后,因为没有只能使用完全恢复模式,不能使用简单或大容量日志模式,所以日志不断增长,不能使用改变恢复模式的方式清空日志 手动操作收缩或截断日志也无效 读了一些文章后发现,有人使用 ...
- 通过网络方式安装linux的五种方法
在线观看:http://video.sina.com.cn/v/b/43086503-1443650204.html http://video.sina.com.cn/v/b/43095530-144 ...
- 错误提示:类型“GridView”的控件“GridView1”必须放在具有 runat=server 的窗体标记内 .
错误提示:类型“GridView”的控件“GridView1”必须放在具有 runat=server 的窗体标记内 在做导出数据到EXCEL程序中,出现了错误提示:类型“GridView”的控件“Gr ...
- $watch How the $apply Runs a $digest
作者:junyuecao | 发表于 8-8 13:39 | 最后更新时间:8-9 02:34 原文地址:http://angular-tips.com/blog/2013/08/watch-how- ...
- LoadRunner执行自动化以及报告自动化的方法
There are three major articles KB articles on Automating LR: 1. Command line arguments for the LoadR ...