C. Electric Charges

题目连接:

http://www.codeforces.com/contest/623/problem/C

Description

Programmer Sasha is a student at MIPT (Moscow Institute of Physics and Technology) and he needs to make a laboratory work to pass his finals.

A laboratory unit is a plane with standard coordinate axes marked on it. Physicists from Moscow Institute of Physics and Technology charged the axes by large electric charges: axis X is positive and axis Y is negative.

Experienced laboratory worker marked n points with integer coordinates (xi, yi) on the plane and stopped the time. Sasha should use "atomic tweezers" to place elementary particles in these points. He has an unlimited number of electrons (negatively charged elementary particles) and protons (positively charged elementary particles). He can put either an electron or a proton at each marked point. As soon as all marked points are filled with particles, laboratory worker will turn on the time again and the particles will come in motion and after some time they will stabilize in equilibrium. The objective of the laboratory work is to arrange the particles in such a way, that the diameter of the resulting state (the maximum distance between the pairs of points of the set) is as small as possible.

Since Sasha is a programmer, he naively thinks that all the particles will simply "fall" into their projections on the corresponding axes: electrons will fall on axis X, while protons will fall on axis Y. As we are programmers too, we will consider the same model as Sasha. That is, a particle gets from point (x, y) to point (x, 0) if it is an electron and to point (0, y) if it is a proton.

As the laboratory has high background radiation and Sasha takes care of his laptop, he did not take it with him, and now he can't write a program that computes the minimum possible diameter of the resulting set. Therefore, you will have to do it for him.

Print a square of the minimum possible diameter of the set.

Input

The first line of the input contains a single integer n (1 ≤ n ≤ 100 000) — the number of points marked on the plane.

Each of the next n lines contains two integers xi and yi ( - 108 ≤ xi, yi ≤ 108) — the coordinates of the i-th point. It is guaranteed that no two points coincide.

Output

Print a single integer — the square of the minimum possible diameter of the set.

Sample Input

3

1 10

1 20

1 30

Sample Output

0

Hint

题意

平面上有n个点,坐标为(xi,yi)

然后每个点可以变成(xi,0)或者(0,yi)

都这样变换之后,问你最小的两点最大距离的平方是多少呢?

题解:

首先考虑全部扔到一维的情况,答案为min(sq(xmax-xmin),sq(ymax-ymin))sq为平方的意思。

然后我们再考虑x轴和y轴都有电子的情况

这种情况的距离最大值,显然是sq(max(abs(x)))+sq(max(abs(y)))

我们首先二分答案,然后暴力枚举放在x轴的区间的左端点,右端点显然是x轴哪些不影响答案的点

然后剩下的点都在y轴上,然后看看是否够

然后再暴力枚举右端点

然后这样就完了,这道题……

代码

#include<bits/stdc++.h>
using namespace std;
const int maxn = 1e5+7;
long long lmin[maxn],rmin[maxn];
long long lmax[maxn],rmax[maxn];
pair<long long,long long>a[maxn];
int n;
long long sq(long long x)
{
return x*x;
}
bool check(long long mid)
{
int r = 1;
for(int l=1;l<=n;l++)
{
if(a[l].first>0) break;
while(r<n && sq(a[r+1].first-a[l].first)<=mid && abs(a[r+1].first)<=abs(a[l].first)) r++;
while(abs(a[r].first)>abs(a[l].first)) r--;
long long low = min(lmin[l-1],rmin[r+1]);
long long high = max(lmax[l-1],rmax[r+1]);
if (sq(high-low)<=mid && sq(max(abs(low),abs(high)))+sq(max(abs(a[l].first),abs(a[r].first)))<=mid) return true;
}
int l = n;
for(int r=n;r>=1;r--)
{
if(a[r].first<0) break;
while(l>1 && sq(a[l-1].first-a[r].first)<=mid && abs(a[l-1].first)<=abs(a[r].first)) l--;
while(abs(a[l].first)>abs(a[r].first)) l++;
long long low = min(lmin[l-1],rmin[r+1]);
long long high = max(lmax[l-1],rmax[r+1]);
if (sq(high-low)<=mid && sq(max(abs(low),abs(high)))+sq(max(abs(a[l].first),abs(a[r].first)))<=mid) return true;
}
return false;
}
long long xmin,xmax,ymin,ymax;
int main()
{
xmin=ymin=1e16,xmax=ymax=-1e16;
scanf("%d",&n);
for(int i=1;i<=n;i++)
{
scanf("%lld%lld",&a[i].first,&a[i].second);
xmin=min(xmin,a[i].first);
xmax=max(xmax,a[i].first);
ymin=min(ymin,a[i].second);
ymax=max(ymax,a[i].second);
}
sort(a+1,a+1+n);
for(int i=1;i<=n;i++)
{
lmin[i]=min(a[i].second,lmin[i-1]);
lmax[i]=max(a[i].second,lmax[i-1]);
}
for(int i=n;i>=1;i--)
{
rmin[i]=min(a[i].second,rmin[i+1]);
rmax[i]=max(a[i].second,rmax[i+1]);
}
long long l = -1,r = min(sq(xmax-xmin),sq(ymax-ymin)),ans = r;
while(l<=r)
{
long long mid = (l+r)/2;
if(check(mid))ans = mid,r = mid-1;
else l = mid+1;
}
printf("%lld\n",ans);
}

AIM Tech Round (Div. 1) C. Electric Charges 二分的更多相关文章

  1. Codeforces AIM Tech Round (Div. 2)

    这是我第一次完整地参加codeforces的比赛! 成绩 news standings中第50. 我觉这个成绩不太好.我前半小时就过了前三题,但后面的两题不难,却乱搞了1.5h都没有什么结果,然后在等 ...

  2. AIM Tech Round (Div. 1) D. Birthday 数学 暴力

    D. Birthday 题目连接: http://www.codeforces.com/contest/623/problem/D Description A MIPT student named M ...

  3. AIM Tech Round (Div. 2) D. Array GCD dp

    D. Array GCD 题目连接: http://codeforces.com/contest/624/problem/D Description You are given array ai of ...

  4. AIM Tech Round (Div. 2) C. Graph and String 二分图染色

    C. Graph and String 题目连接: http://codeforces.com/contest/624/problem/C Description One day student Va ...

  5. AIM Tech Round (Div. 2) B. Making a String 贪心

    B. Making a String 题目连接: http://codeforces.com/contest/624/problem/B Description You are given an al ...

  6. AIM Tech Round (Div. 2) A. Save Luke 水题

    A. Save Luke 题目连接: http://codeforces.com/contest/624/problem/A Description Luke Skywalker got locked ...

  7. AIM Tech Round (Div. 2) B

    B. Making a String time limit per test 1 second memory limit per test 256 megabytes input standard i ...

  8. AIM Tech Round (Div. 2) A

    A. Save Luke time limit per test 1 second memory limit per test 256 megabytes input standard input o ...

  9. AIM Tech Round (Div. 2) C. Graph and String

    C. Graph and String time limit per test 2 seconds memory limit per test 256 megabytes input standard ...

随机推荐

  1. 【DLL】动态库的创建,隐式加载和显式加载(转)

    原文转自:https://blog.csdn.net/dcrmg/article/details/53437913

  2. 003 CopyOnWriteArrayList原理

    聊聊并发-Java中的Copy-On-Write容器 Copy-On-Write简称COW,是一种用于程序设计中的优化策略.其基本思路是,从一开始大家都在共享同一个内容,当某个人想要修改这个内容的时候 ...

  3. [ Python ] set集合及函数的使用

    1. set类型 set 和 dict 类似,也是一组 key 的集合,但是不存储 value. 由于 key  不重复,所以,在 set 中, 没有重复的 key 集合是可变类型 (1)集合的创建 ...

  4. 应用程序有bug崩溃重启的案例2

    ------解决思路----------------------另外做一个服务或者程序定时监控系统进程.程序奔溃的话,都会在入口函数出现异常处理一下winform可以有两个事件来捕获主线程异常和线程异 ...

  5. grid+report 怎么在项目中使用

    grid+report 的例子很丰富,首先看你所用对应编程语言的例子.参考帮助的“产品介绍->快速入门指导”部分.根据快速入门指导中的说明,先把例子程序运行. 例子分两部分:1.报表模板例子,主 ...

  6. hdu 1087(LIS变形)

    Super Jumping! Jumping! Jumping! Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 ...

  7. phoenix到hbase的应用

    一.phoenix的简介 hbase的java api或者其语法很难用,可以认为phoenix是一个中间件,提供了访问hbase的另外的语法. 二.配置phoenix和hbase 1.下载 phoen ...

  8. AC日记——[USACO06FEB]奶牛零食Treats for the Cows 洛谷 P2858

    [USACO06FEB]奶牛零食Treats for the Cows 思路: 区间DP: 代码: #include <bits/stdc++.h> using namespace std ...

  9. Template Mode 'HTML5' is deprecated与UnsatisfiedDependencyException

    org.thymeleaf.templatemode.TemplateMode : [THYMELEAF][main] Template Mode 'HTML5' is deprecated. Usi ...

  10. 五十七 POP3收取邮件

    SMTP用于发送邮件,如果要收取邮件呢? 收取邮件就是编写一个MUA作为客户端,从MDA把邮件获取到用户的电脑或者手机上.收取邮件最常用的协议是POP协议,目前版本号是3,俗称POP3. Python ...