Codeforces(429D - 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
解法:能够将结果转化为求(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)近期点对问题的更多相关文章
- Codeforces 429D Tricky Function 近期点对
		题目链接:点击打开链接 暴力出奇迹. 正解应该是近期点对.以i点为x轴,sum[i](前缀和)为y轴,求随意两点间的距离. 先来个科学的暴力代码: #include<stdio.h> #i ... 
- Codeforces 429D Tricky Function(平面最近点对)
		题目链接 Tricky Function $f(i, j) = (i - j)^{2} + (s[i] - s[j])^{2}$ 把$(i, s[i])$塞到平面直角坐标系里,于是转化成了平面最近点 ... 
- Codeforces Round #245 (Div. 1) 429D - Tricky Function  最近点对
		D. Tricky Function Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 codeforces.com/problemset/problem/42 ... 
- codeforce 429D. Tricky Function (思维暴力过)
		题目描述 Iahub and Sorin are the best competitive programmers in their town. However, they can't both qu ... 
- Codefoces 429 D. Tricky Function
		裸的近期点对.... D. Tricky Function time limit per test 2 seconds memory limit per test 256 megabytes inpu ... 
- 【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 - 598A Tricky Sum (数学,快速幂的运用)
		传送门: http://codeforces.com/problemset/problem/598/A A. Tricky Sum time limit per test 1 second memor ... 
- codeforces 598A Tricky Sum
		题目链接:http://codeforces.com/contest/598/problem/A 题目分类:大数 题意:1到n 如果是2的次方则减去这个数,否则就加上这个数,求最后的结果是多少 题目分 ... 
随机推荐
- 查询SQLServer2005中某个数据库中的表结构、索引、视图、存储过程、触发器以及自定义函数
			查询SQLServer2005中某个数据库中的表结构.索引.视图.存储过程.触发器以及自定义函数 2013-03-11 09:05:06| 分类: SQL SERVER|举报|字号 订阅 ( ... 
- BZOJ1001 狼抓兔子 平面图转对偶图 最小割
			现在小朋友们最喜欢的"喜羊羊与灰太狼",话说灰太狼抓羊不到,但抓兔子还是比较在行的,而且现在的兔子还比较笨,它们只有两个窝,现在你做为狼王,面对下面这样一个网格的地形: 左上角点为 ... 
- 页面jsp向后端发送:HTTP 400错误 - 请求无效(Bad request)
			HTTP 400错误 - 请求无效(Bad request) jsp页面有误 在ajax请求后台数据时有时会报 HTTP 400 错误 - 请求无效 (Bad request);出现这个请求无效报错说 ... 
- php7 安装swoole扩展
			昨天无意中看到一篇关于直播的视频教程 里面讲到了swoole,对于这个东西我相信大家(接近1年phper)都是听过它,但没有真正去用它,当然也是不知道如何使用(me too). 此处总结一下(借鉴了几 ... 
- linux文件及目录的权限管理
			一.文件的权限 1.文件权限的查看 命令:ls -l 可以使用ll命令代替 ls -l 2.ls -l 所包含的信息 (1)权限信息 (-rw-r--r-- ) 一共有10位 a.第一位:表示文件信息 ... 
- Python学习之单继承与多继承
			继承 面向对象编程语言的一个主要功能就是“继承”. 继承是指这样一种能力:它可以使用现有类的所有功能,并在无需重新编写原来的类的情况下对这些功能进行扩展. (1) 单继承:python同时支持类的继承 ... 
- Maximun product
			Given a sequence of integers S = {S1, S2, ..., Sn}, you shoulddetermine what is the value of the max ... 
- [BZOJ3555] [Ctsc2014]企鹅QQ(Hash)
			传送门 可以枚举被删除的位置,然后用hash表判重,然而网上好多题解都是用 sort 判重的. 不知道为什么,int 总是过不了,换成 long long 或者是 unsigned long long ... 
- 【bzoj1922】[Sdoi2010]大陆争霸 - STL - dijkstra
			信仰斯普林·布拉泽的克里斯国教徒. 幻想历 8012年 3月2日,位于杰森国东部小镇神谕镇的克里斯国教徒发动 起义. 幻想历 8012年 3月7日,神谕镇的起义被杰森国大军以残酷手段镇压. 幻想历 8 ... 
- Git Cheat Sheet 中文版
			Git Cheat Sheet 中文版 索引 配置 配置文件 创建 本地修改 搜索 提交历史 分支与标签 更新与发布 合并与重置 撤销 Git Flow 配置 列出当前配置: $ git config ... 
