Description
“我是超级大沙茶”——Mato_No1
为了证明自己是一个超级大沙茶,Mato 神犇决定展示自己对叉(十字型)有多么的了解。
Mato 神犇有一个平面直角坐标系,上面有一些线段,保证这些线段至少与一条坐标轴平行。Mato 神犇需要指出,这些线段构成的最大的十字型有多大。
称一个图形为大小为R(R 为正整数)的十字型,当且仅当,这个图形具有一个中心点,它存在于某一条线段上,并且由该点向上下左右延伸出的长度为R 的线段都被已有的线段覆盖。
你可以假定:没有两条共线的线段具有公共点,没有重合的线段。
Input
第一行,一个正整数N,代表线段的数目。
以下N 行,每行四个整数x1,y1,x2,y2(x1=x2 或y1=y2),描述了一条线段。
Output
当不存在十字型时:输出一行“Human intelligence is really terrible”(不包括引号)。
否则:输出一行,一个整数,为最大的R。
Sample Input
输入1:
1
0 0 0 1
输入2:
3
-1 0 5 0
0 -1 0 1
2 -2 2 2
Sample Output
输出1:
Human intelligence is really terrible
输出2:
2
Data Constraint
对于50%的数据:N≤1000。
对于100%的数据:1≤N≤100000,所有坐标的范围在-10^9~10^9 中。
后50%内,所有数据均为随机生成。
题解
“没有两条共线的线段具有公共点,没有重合的线段。”
说明每个十字形最多涉及一个水平线段和一个竖直线段。
100’扫描线
现在需要解决这个判定性问题。
按X坐标扫描,维护水平线段的Y坐标的集合。
涉及的操作:
加一个点
删一个点
查询一段区间内有没有数
100’扫描线
维护性问题
STL set
查询部分用lower_bound和upper_bound即可
#include<cstdio>
#include<set>
#include<algorithm>
#define N 100005
using namespace std;
int n,t0=0,t1=0;
multiset<int>s;
struct seg
{
int x,y,len;
}s0[N],s1[N];
struct node
{
int tp,x,y,z;
bool operator<(const node &a)const
{
if(x!=a.x) return x<a.x;
else return tp*z>a.tp*a.z;
}
}a[N*2];
bool check(int x)
{
int t=0;
for(int i=1;i<=t0;i++)
if(s0[i].len>=2*x)
a[++t]=(node){0,s0[i].x,s0[i].y+x,s0[i].y+s0[i].len-x};
for(int i=1;i<=t1;i++)
if(s1[i].len>=2*x)
{
a[++t]=(node){1,s1[i].x+x,s1[i].y,1};
a[++t]=(node){1,s1[i].x+s1[i].len-x,s1[i].y,-1};
}
sort(a+1,a+t+1);
s.clear();
for(int i=1;i<=t;i++)
if(a[i].tp)
if(a[i].z==1) s.insert(a[i].y);
else s.erase(s.find(a[i].y));
else if(s.lower_bound(a[i].y)!=s.upper_bound(a[i].z)) return 1;
return 0;
}
int main()
{
scanf("%d",&n);
for(int i=1;i<=n;i++)
{
int x1,y1,x2,y2;
scanf("%d%d%d%d",&x1,&y1,&x2,&y2);
if(x1==x2)
{
if(y1>y2) swap(y1,y2);
s0[++t0]=(seg){x1,y1,y2-y1};
}
else
{
if(x1>x2) swap(x1,x2);
s1[++t1]=(seg){x1,y1,x2-x1};
}
}
if(!check(1))
{
printf("Human intelligence is really terrible");
return 0;
}
int l=1,r=1000000000;
while(r-l>1)
{
int mid=(l+r)>>1;
if(check(mid)) l=mid;
else r=mid-1;
}
if(check(r)) printf("%d",r);
else printf("%d",l);
return 0;
}