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

解法:能够将结果转化为求(i,sum(i))近期点对问题。sum(i)为前缀1-i之和;

代码:

/******************************************************
* author:xiefubao
*******************************************************/
#pragma comment(linker, "/STACK:102400000,102400000")
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <cstdio>
#include <queue>
#include <vector>
#include <algorithm>
#include <cmath>
#include <map>
#include <set>
#include <stack>
#include <string.h>
//freopen ("in.txt" , "r" , stdin);
using namespace std; #define eps 1e-8
const double pi=acos(-1.0);
typedef long long LL;
const int Max=10100;
const int INF=1000000007; struct point
{
double x,y;
int lable;
} ;
point points[1001000]; bool operator<(const point& a,const point& b)
{
if(a.x!=b.x)
return a.x<b.x;
else
return a.y<b.y;
}
bool compareY(const point& a,const point& b)
{
return a.y<b.y;
}
double getDistance(const point& a,const point& b)
{
return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));
} double getMiniDistance(int left,int right)
{
if(left==right)
return 1000000000000;
if(right-left==1)
{
if(points[left].lable^points[right].lable)
return getDistance(points[left],points[right]);
else
return 1000000000000;
}
int mid=(left+right)/2;
double num=min(getMiniDistance(left,mid),getMiniDistance(mid+1,right));
double mLine=points[mid].x;
int L=mid;
while(L>left&&mLine-points[L].x<=num)
L--;
int R=mid+1;
while(R<right&&points[R].x-mLine<=num)
R++;
sort(points+L,points+R+1,compareY);
for(int i=L; i<=R; i++)
{
for(int j=i+1; j<=min(R,i+5); j++)
{
if(points[j].y-points[i].y>=num)
break;
if(points[j].lable^points[i].lable)
{
num=min(num,getDistance(points[i],points[j]));
}
}
}
return num;
} int main()
{
int T;
scanf("%d",&T);
for(int i=0; i<T; i++)
{
int N;
scanf("%d",&N);
for(int i=0; i<N; i++)
{
scanf("%lf%lf",&points[i].x,&points[i].y);
points[i].lable=0;
}
for(int i=N; i<N*2; i++)
{
scanf("%lf%lf",&points[i].x,&points[i].y);
points[i].lable=1;
}
sort(points,points+2*N);
printf("%.3f\n",getMiniDistance(0,2*N-1));
}
return 0;
}

最新文章

  1. Lightbox改造——支持滚轮缩放
  2. Python 正则表达式:只要整数和小数
  3. NSFileManager 遍历目录
  4. 1160 蛇形矩阵(codevs)
  5. delphi倒计时按钮写法
  6. python 编写简单的setup.py
  7. python复杂网络库networkx:基础
  8. Vue Mixin 与微信小程序 Mixins 应用
  9. kubernetes集群应用部署实例
  10. Gym101194J Mr.Panda and TubeMaster 二分图、费用流
  11. vue-router的简单实现原理
  12. Java:ConcurrentHashMap
  13. Storm Topology 提交 总结---Kettle On Storm 实现
  14. 不同浏览器的userAgent
  15. c语言中的内存分配malloc、alloca、calloc、malloc、free、realloc、sbr
  16. PHP 表单和用户输入
  17. Unity3D笔记八 Unity生命周期及动画学习
  18. zend studio 10安装+破解+汉化
  19. Java之Tomcat、Dynamic web project与Servlet
  20. Pyscripter 不能正确调用另一文件中模块的问题的解析(Internal Engine 和 Remote Engine)

热门文章

  1. 使用 PHP + shell 生成 一键设置权限的脚本。
  2. os系统下安装Python2和Python3
  3. 如何在matlab里安装libsvm包
  4. Blend 混合
  5. java 几种拼接字符串的效率问题
  6. stl lower_bound()返回值
  7. Python模块:configparser、hashlib、(subprocess)
  8. Eclipse4.5在线安装Aptana插件及配置代码提示教程
  9. hiho一下 第四十九周 欧拉路
  10. TCP/IP学习笔记(5)------IP选路