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;
}

Codeforces(429D - Tricky Function)近期点对问题的更多相关文章

  1. Codeforces 429D Tricky Function 近期点对

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

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

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

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

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

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

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

  5. Codefoces 429 D. Tricky Function

    裸的近期点对.... D. Tricky Function time limit per test 2 seconds memory limit per test 256 megabytes inpu ...

  6. 【Codeforces 429D】 Tricky Function

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

  7. 【codeforces 429D】Tricky Function

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

  8. CodeForces - 598A Tricky Sum (数学,快速幂的运用)

    传送门: http://codeforces.com/problemset/problem/598/A A. Tricky Sum time limit per test 1 second memor ...

  9. codeforces 598A Tricky Sum

    题目链接:http://codeforces.com/contest/598/problem/A 题目分类:大数 题意:1到n 如果是2的次方则减去这个数,否则就加上这个数,求最后的结果是多少 题目分 ...

随机推荐

  1. 实训day01 python基础

    一.编程语言 编程语言:可以被计算机所识别的表达方式. 编程:程序员通过编程语言将自己的想法编写出来,产生的结果就是包含字符的文件. 其中,只有程序在运行时,其中的字符才有特定的语法意义. 二.计算机 ...

  2. python appium自动化,走过的坑

    使用的夜神模拟器,使用android5.1.1 第一坑:使用的android7.1.2,刚开始写好了登录的代码,需要的是滑屏进入到登录界面,结果运行的时候,没有自动滑屏就报错:因为运行时,报了一个进程 ...

  3. 第4节 hive调优:1、2、fetch抓取和表的优化

    hive的调优:第一个调优:fetch抓取,能够避免使用mr的,就尽量不要用mr,因为mr太慢了 set hive.fetch.task.conversion=more 表示我们的全局查找,字段查找, ...

  4. jquery toggle()设置

    很多朋友对jquery toggle()比较熟练,甚至经常用到,而且对toggle的三个参数也比较了解$(selector).toggle(speed,callback,switch).但是当你设置$ ...

  5. 笔试算法题(13):反转链表 & 左旋转字符串

    出题:反转链表(递归和非递归解法): 分析:有递归跟非递归实现,注意对原始链表头节点的处理,因为其他节点都指向下一个节点,其需要指向NULL: 解题: struct Node { int v; Nod ...

  6. react初探索--react + react-router + ant-design 后台管理系统配置

    首先确认安装了node环境,Node >= 6. 如果对react 及 ant-design 一无所知,建议去阅读下api文档,react 可以在 codePen 在线练习. react Api ...

  7. php扩展1:filp/whoops(用于调试,方便定位错误点)

    一.composer下载filp/whoops: 1.在composer.json中添加:"filp/whoops": "*",如下所示: 2.执行compos ...

  8. 22Spring基于配置文件的方式配置AOP

    直接看代码: package com.cn.spring.aop.impl; //加减乘除的接口类 public interface ArithmeticCalculator { int add(in ...

  9. Python自动化测试-使用Pandas来高效处理测试数据

    一.思考 1.Pandas是什么? 功能极其强大的数据分析库 可以高效地操作各种数据集 csv格式的文件 Excel文件 HTML文件 XML格式的文件 JSON格式的文件 数据库操作 2.经典面试题 ...

  10. 关于 <customErrors> 标记的“mode”属性设置为“Off”的问题的解决方案

    用 权限问题 <customErrors> 标记的“mode”属性设置为“Off”. 权限问题标记的“mode”属性设置为“Off”.说明: 服务器上出现应用程序错误.此应用程序的当前自定 ...