poj 1755 Triathlon(半平面解线性规划)

在编程过程中,遇到一个小bug并解决的过程。涉及到C++语言的使用,包括头文件的引用,结构体和函数的定义,以及算法的实现。通过一天的努力最终找到了问题所在并成功修复。

这个题刚开始写的时候出了一个小bug。。。。妈蛋。。就是那个东西。。我调了一天才把它调出来大哭

#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<cmath>
#include<algorithm>
#define inf 1000000
using namespace std;
const double eps=1e-8;
int dq[1000000],top,bot,n;
struct P{
	double x,y;
	P (){}
	P(double x,double y):x(x),y(y){
	}
	P operator +(P p){
		return P(x+p.x,y+p.y);
	}
	P operator -(P p){
		return P(x-p.x,y-p.y);
	}
	P operator *(double d){
		return P(x*d,y*d);
	}
	double det(P p){
		return x*p.y-y*p.x;
	}
};
struct ll
{
	double v,w,u;
}spd[1000000];
int dblcmp(double k)
{
	if(fabs(k)<eps)
		return 0;
	return k>0?1:-1;
}
struct Line{
	P a,b;
	double angle;
};
P in(Line a,Line b){
	return a.a+(a.b-a.a)*((b.b-b.a).det(b.a-a.a) / (b.b-b.a).det(a.b-a.a));
}
bool cmp(Line a,Line b){
	int d=dblcmp(a.angle-b.angle);
	if(!d)
		return dblcmp((b.a-a.a).det(b.b-a.a))>0;   //顺时针小于0,逆时针大于0;
	return d<0;
}
bool judge(Line a,Line b,Line c){
	P r=in(b,c);
	return dblcmp((a.a-r).det(a.b-r))<=0;    //顺时针大于0,逆时针小于,和上面的一定是异号的
}
P p[1000000];
Line l[1000000];
int ln;
bool HPI()
{
	int i,j;
	sort(l,l+ln,cmp);
	for(i=0,j=0;i<ln;i++)
	{
		if(dblcmp(l[i].angle-l[j].angle)>0)
			l[++j]=l[i];
	}
	ln=j+1;
	top=1;
	bot=0;
	dq[0]=0;
	dq[1]=1;
	for(i=2;i<ln;i++)
	{
		while(top>bot && judge(l[i],l[dq[top]],l[dq[top-1]])) top--;
		while(top>bot && judge(l[i],l[dq[bot]],l[dq[bot+1]])) bot++;
		dq[++top]=i;
	}
	while(top>bot && judge(l[dq[bot]],l[dq[top]],l[dq[top-1]])) top--;
	while(top>bot && judge(l[dq[top]],l[dq[bot]],l[dq[bot+1]])) bot++;
	if(top-bot>1)
		return 1;
	return 0;
}
bool check(int i)
{
		ln=0;
		l[ln].a.x=0;l[ln].a.y=0;l[ln].b.x=inf;l[ln].b.y=0;
		l[ln].angle=atan2(l[ln].b.y-l[ln].a.y,l[ln].b.x-l[ln].a.x);
		ln++;

		l[ln].a.x=inf;l[ln].a.y=0;l[ln].b.x=inf;l[ln].b.y=inf;
		l[ln].angle=atan2(l[ln].b.y-l[ln].a.y,l[ln].b.x-l[ln].a.x);
		ln++;

		l[ln].a.x=inf;l[ln].a.y=inf;l[ln].b.x=0;l[ln].b.y=inf;
		l[ln].angle=atan2(l[ln].b.y-l[ln].a.y,l[ln].b.x-l[ln].a.x);
		ln++;

		l[ln].a.x=0;l[ln].a.y=inf;l[ln].b.x=0;l[ln].b.y=0;
		l[ln].angle=atan2(l[ln].b.y-l[ln].a.y,l[ln].b.x-l[ln].a.x);
		ln++;
	for(int j=0;j<n;j++)
	{
		if(i==j)
			continue;





		//不明所以,先存着
		double A,B,C;
		A=(spd[i].v-spd[j].v)/(spd[i].v*spd[j].v);
		B=(spd[i].u-spd[j].u)/(spd[i].u*spd[j].u);
		C=(spd[i].w-spd[j].w)/(spd[i].w*spd[j].w);
		double x1,x2,y1,y2;
		int d1 = dblcmp(A);  
		int d2 = dblcmp(B);  
		int d3 = dblcmp(C);  
		if (!d1) 
		{  
			if (!d2) 
			{  
				if (d3 <= 0)
				{  
					return 0; 
				}  
				continue;  
			}  
			x1 = 0;  
			x2 = d2;//d2的值为1或-1  
			y1 = y2 = - C / B;  
		}  
		else 
		{  
			if (!d2) 
			{  
				x1 = x2 = - C / A;  
				y1 = 0;  
				y2 = -d1;  
			}  
			else {  
				x1 = 0; y1 = - C / B;  
				x2 = d2;  
				y2 = -(C + A * x2) / B;  
			}  
		}  






		l[ln].a.x=x1;l[ln].a.y=y1;
		l[ln].b.x=x2;l[ln].b.y=y2;
		l[ln].angle=atan2(l[ln].b.y-l[ln].a.y,l[ln].b.x-l[ln].a.x);
		ln++;
	}
	if(HPI())
		return 1;
	else
		return 0;
}
int main()
{
	while(~scanf("%d",&n))
	{
		
		for(int i=0;i<n;i++)
			scanf("%lf%lf%lf",&spd[i].v,&spd[i].u,&spd[i].w);
		
		
		for(int i=0;i<n;i++)
		{
			if(check(i))
				printf("Yes\n");
			else
				printf("No\n");
		}
	}
	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值