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的次方则减去这个数,否则就加上这个数,求最后的结果是多少 题目分 ...
随机推荐
- java去左右的空格(包括全角空格,tab,回车等)
在开发中我们会遇到需要去除左右空格的需求,如果只是简单的空格,调一下trim()方法即可,但如果有中文全角.回车等看起来是空格的非空格,则需要自定义来开发实现,下面这个工具可以实现去左右那些看起来是空 ...
- 第1节 MapReduce入门:11、mapreduce程序的入门-2
1.5.WordCount示例编写 1.JobMain.java类 package cn.itcast.wordcount; import org.apache.hadoop.conf.Configu ...
- java was started but returned exit code =-805306369的处理方法
Myeclipse出现java was started but returned exit code =-805306369的错误,如图: 解决方法: 换个workspaces:换个工作目录,估计估计 ...
- LeetCode136,137寻找只出现一次的数
1.题目意思:在数组中,只有一个数字只出现了一次 其他的都出现了两次.找出那个只出现一次的数字. //利用位运算 异或 两个相同的数字异或为0 public int singleNumber(int[ ...
- 小程序input自动聚焦拉起键盘
微信官方提供了两种自动聚焦的方法 1,auto-focus 接受boolean值:默认为false:只需设置为true即可 自动聚焦,拉起键盘:不过官方的提示即将废弃,所以能不用还是不要用 2,foc ...
- python3.x Day6 paramiko
python3 paramiko模块,用来进行远程操作linux服务器,利用的就是ssh #利用用户名,密码,进行连接 import paramiko #创建一个SSH对象 ssh=paramiko. ...
- error trying to exec 'cc1plus': execvp: 没有那个文件或目录
出现这个问题,有两种可能: 第一,你没有安装g++ 第二,你的gcc的版本和g++版本不相符合 安装gcc和g++及一些依赖包 sudo apt-get install build-essential ...
- 【tips】RESTful架构
认识RESTful在前后端分离的应用模式里,后端API接口如何定义?例如对于后端数据库中保存了商品的信息,前端可能需要对商品数据进行增删改查,那相应的每个操作后端都需要提供一个API接口: PO ...
- Matlab学习笔记(三)
二.MATLAB基础知识 (四)数组 MATLAB总是把数组看作存储和运算的基本单位,标量数据也被看作是(1×1)的数组 一维数组的创建 创建一维数组的几种方法:(e_two_14.m) 直接输入法: ...
- Python接口测试之对MySQL的操作(六)
本文章主要来说python对mysql数据库的基本操作,当然,前提是已经搭建了python环境和搭建了Mysql 数据库的环境,python操作mysql数据库提供了MySQLdb库,下载的地址为: ...