HDU1687_数学几何

题目大意: 给你一个光源的坐标,然后给你n条线段,然后由于有光源,线段会在x轴上产生阴影,注意线段只在x轴上,而且线段在的横坐标在[-100,100]. 解题思路: 求出每天线段产生的阴影的区间,然后对区间排序,按区间的头部排序即可,然后用一个max标志扫过的区间的末尾最长点,之后比较区间头与max即可,算出count. 吐吐槽: 好吧,我承认我自己弱,可是最后发现,我弱得有点离谱,最后的max是double型的,为什么我写成了int型,唉~~这次比赛,又水了,本来以为碰到了这道水题可以轻松过的。想不到最后还来出现这种离谱的差错。这次比赛有两道水题,标志之前只看搜索跟图论的。失望而归后,居然漏掉了这些水题,还是得练练看题速度啊。 代码:

#include
#include
using namespace std;
const int MAX = 105;
typedef struct node
{
	double s, e;
}N;

N qu[MAX];

//double getX(int x1, int y1, int x, int y)//这个更简洁,不过我的也没错
//{
//	if(x == x1)
//		return (double)x;
//	else
//	{
//		double ans = (double)(x1*y - x*y1) / (double)(y - y1);
//		return ans;
//	}
//}

bool cmp(N a, N b)
{
	return a.s < b.s;
}

int main(void)
{
	int cas;
	scanf("%d", &cas);
	while(cas--)
	{
		int n;
		double x, y, x1, y1, x2, y2, u1, v1;
		scanf("%d", &n);
		scanf("%lf %lf", &x, &y);
		for(int i = 0; i < n; i++)
		{
			scanf("%lf %lf %lf %lf", &x1, &y1, &u1, &v1);
			
			double k, b;
			double sx, ex;
			if(x1 == x)
				sx = x;
			else
			{
				k = (y1 - y) / (x1 - x);
				b =  k * x - y;
				sx = b / k;//第一个点的位置
			}
			if(u1 == x)
				ex = x;
			else
			{
				k = (v1 - y) / (u1 - x);
				b = k * x - y;
				ex = b / k;
			}
			if(sx <= ex)
			{
				qu[i].s = sx;
				qu[i].e = ex;
			}
			else
			{
				qu[i].s = ex;
				qu[i].e = sx;
			}
		}
		if(n == 0)
		{
			printf("1\n");
			continue;
		}
		sort(qu, qu+n, cmp);
		int count = 1;
		double max = qu[0].e;
		for(int i = 1; i < n; i++)
		{
			if(qu[i].s > max)
			{
				count++;
			}
			if(qu[i].e > max)
				max = qu[i].e;
		}
		printf("%d\n", count+1);
	}
	return 0;
}
原文地址:https://www.cnblogs.com/cchun/p/2520216.html