哎,一道这样的题就被整成这样,太粗心了
题意:已知一个点光源,和N条线段,求在光源照射下,在x轴上有多少个亮的区域
分析:先求出投影到x轴上的N个区间,然后就是简单的区间覆盖问题了
View Code
#include<iostream>
#include<algorithm>
using namespace std;
int xl,yl;
struct seg
{
int x1,y1,x2,y2;
double xx1,xx2;
}s[101];
bool cmp(seg a,seg b)
{
return a.xx1<b.xx1;
}
double max(double a,double b)
{
if(a>b)
return a;
return b;
}
double get_x(int x,int y)
{
if(x==xl)
return (double)x;
else
return (double)(y*xl-yl*x)/(double)(y-yl);
}
int main()
{
int T,n;
scanf("%d",&T);
while(T--)
{
scanf("%d",&n);
scanf("%d %d",&xl,&yl);
for(int i=0;i<n;i++)
scanf("%d %d %d %d",&s[i].x1,&s[i].y1,&s[i].x2,&s[i].y2);
for(int i=0;i<n;i++)
{
s[i].xx1=get_x(s[i].x1,s[i].y1);//这个我不想说什么,真的服了自己了
s[i].xx2=get_x(s[i].x2,s[i].y2);
if(s[i].xx1>s[i].xx2)
swap(s[i].xx1,s[i].xx2);
}
sort(s,s+n,cmp);
int count=0;
double max1=s[0].xx2;//max1一开始定义成int了,哎
for(int i=1;i<n;i++)
{
if(s[i].xx1>max1)
{
count++;
max1=s[i].xx2;
}
else {
max1=max(s[i].xx2,max1);
}
}
if(n==0)//没考虑到n等于0
printf("1\n");
else
printf("%d\n",count+2);
}
return 0;
}