裸的近期点对....

D. Tricky Function
time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

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?

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).

Sample test(s)
input
4
1 0 0 -1
output
1
input
2
1 -1
output
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的更多相关文章

  1. Codeforces Round #245 (Div. 1) 429D - Tricky Function 最近点对

    D. Tricky Function Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 codeforces.com/problemset/problem/42 ...

  2. Codeforces 429D Tricky Function(平面最近点对)

    题目链接  Tricky Function $f(i, j) = (i - j)^{2} + (s[i] - s[j])^{2}$ 把$(i, s[i])$塞到平面直角坐标系里,于是转化成了平面最近点 ...

  3. Codeforces(429D - Tricky Function)近期点对问题

    D. Tricky Function time limit per test 2 seconds memory limit per test 256 megabytes input standard ...

  4. codeforce 429D. Tricky Function (思维暴力过)

    题目描述 Iahub and Sorin are the best competitive programmers in their town. However, they can't both qu ...

  5. 【Codeforces 429D】 Tricky Function

    [题目链接] http://codeforces.com/problemset/problem/429/D [算法] 令Si = A1 + A2 + ... + Ai(A的前缀和) 则g(i,j) = ...

  6. 【codeforces 429D】Tricky Function

    [题目链接]:http://codeforces.com/problemset/problem/429/D [题意] 给你n个数字; 让你求出一段区间[l,r] 使得 (r−l)2+(∑rl+1a[i ...

  7. Codeforces 429D Tricky Function 近期点对

    题目链接:点击打开链接 暴力出奇迹. 正解应该是近期点对.以i点为x轴,sum[i](前缀和)为y轴,求随意两点间的距离. 先来个科学的暴力代码: #include<stdio.h> #i ...

  8. javascript零散要点收集

    1.this永远指向函数对象的所有者 2.ECMA-262 把对象(object)定义为“属性的无序集合,每个属性存放一个原始值.对象或函数”.严格来说,这意味着对象是无特定顺序的值的数组. 3.pr ...

  9. CF数据结构练习

    1. CF 438D The Child and Sequence 大意: n元素序列, m个操作: 1,询问区间和. 2,区间对m取模. 3,单点修改 维护最大值, 取模时暴力对所有>m的数取 ...

随机推荐

  1. hibernate建表多对多建表

    Student.java package cn.itcast.hiberate.sh.domain; import java.util.Set; public class Student { priv ...

  2. zzzzz

    Extension Method: Return another string if string is null or emptyJust a tiny little extension metho ...

  3. CMDB反思4

    CMDB模型设计2 http://blog.vsharing.com/xqscool/A1275233.html 估计大家看到破子的这两篇都有点晕哈,我也有点晕. 两篇对比来看.   第1处,属性部分 ...

  4. iOS学习笔记之ARC内存管理

    iOS学习笔记之ARC内存管理 写在前面 ARC(Automatic Reference Counting),自动引用计数,是iOS中采用的一种内存管理方式. 指针变量与对象所有权 指针变量暗含了对其 ...

  5. html --- canvas --- javascript --- 绘图方法

    Canvas元素是HTML5的一部分,允许脚本语言动态渲染位图像. 如有疑问请访问链接:http://javascript.ruanyifeng.com/htmlapi/canvas.html < ...

  6. 批量还原数据库 SQL Server 2008

    1.如果你够懒,不想一步一步点路径,一步一步选择 2.如果你连单个备份数据库的存储过程都不想多执行,一般每还原一个需要修改数据库名 下面的脚本适合你: /*********************** ...

  7. BITED-Windows8应用开发学习札记之四:如何在Win8 应用中实现语义缩放

    语意缩放的意义在于:创新的语意缩放外观,让你的应用随时展现信息可视化的力量.如图表般的Tile,随着数据的不同而变化,让你的页面更富节奏.而所谓的语意缩放就是通过上下文的跳转,帮助我们实现一种更快更便 ...

  8. SCAU 13校赛 17115 ooxx numbers

    17115 ooxx numbers 时间限制:1000MS  内存限制:65535K 题型: 编程题   语言: 无限制 Description a number A called oo numbe ...

  9. DD_belatedPNG,解决 IE6 不支持 PNG-24 绝佳解决方案

    png24在ie下支持透明.终于找到下面的可行办法: 我们知道 IE6 是不支持透明的 PNG-24 的,这无疑限制了网页设计的发挥空间. 然而整个互联网上解决这个 IE6 的透明 PNG-24 的方 ...

  10. shell学习目录

    1. 了解shell 2. shell 入门基础 3. Shell脚本文件中常用的操作语句