POJ-1755 半平面交解决 判断不等式组有解

Description

Triathlon is an athletic contest consisting of three consecutive sections that should be completed as fast as possible as a whole. The first section is swimming, the second section is riding bicycle and the third one is running.

The speed of each contestant in all three sections is known. The judge can choose the length of each section arbitrarily provided that no section has zero length. As a result sometimes she could choose their lengths in such a way that some particular contestant would win the competition.
Input

The first line of the input file contains integer number N (1 <= N <= 100), denoting the number of contestants. Then N lines follow, each line contains three integers Vi, Ui and Wi (1 <= Vi, Ui, Wi <= 10000), separated by spaces, denoting the speed of ith contestant in each section.
Output

For every contestant write to the output file one line, that contains word “Yes” if the judge could choose the lengths of the sections in such a way that this particular contestant would win (i.e. she is the only one who would come first), or word “No” if this is impossible.
Sample Input

9
10 2 6
10 7 3
5 6 7
3 2 7
6 2 6
3 5 7
8 4 6
10 4 2
1 8 7
Sample Output

Yes
Yes
Yes
No
No
No
Yes
No
Yes

每个人耗时 T[i]=x/v[i]+y/u[i]+(1-x-y)/w[i]
只要不等式 T[j]>T[i] :j∈[1,n] && j!=i 有解
那个人就有可能获胜

#include<iostream>
#include<string.h>
#include<math.h>
#include<stdio.h>
#include<algorithm>
#include<vector>
using namespace std;
const int N=1e3+7;
const int maxp=1e5+7;
const double inf=1000000000;
const double eps=1e-16;
const double pi =acos(-1.0);
int sgn(double x){
	if(fabs(x)<eps)return 0;
	return x>0?1:-1;
}
double hypot(double x,double y){return sqrt(x*x+y*y);}
struct Point{
	double x,y;
	Point(double x=0,double y=0):x(x),y(y){}
	bool operator ==(const Point& b){return !sgn(x-b.x)&&!sgn(y-b.y);}
	bool operator  <(const Point& b)const{return !sgn(x-b.x)?y<b.y:x<b.x;}
	double operator ^(const Point& b){return x*b.y-y*b.x;}//叉积
	double operator *(const Point& b){return x*b.x+y*b.y;}//点积
	Point operator +(const Point &b){return Point(x+b.x,y+b.y);}
	Point operator -(const Point& b){return Point(x-b.x,y-b.y);}
	Point operator *(const double &k){return Point(x*k,y*k);}
	Point operator /(const double &k){return Point(x/k,y/k);}
	double len(){return hypot(x,y);}
	double len2(){return x*x+y*y;}
	double rad(Point a,Point b){Point p=*this;return fabs(atan2(fabs((a-p)^(b-p)),(a-p)*(b-p)));}//pa和pb夹角
	double angle(){return atan2(y,x);}//倾斜角
	double angle(Point B){
		Point A=(*this);
		return acos(A*B/A.len()/B.len());
	}//夹角
	double distance(Point b){return hypot(x-b.x,y-b.y);}
	Point trunc(double r){//化为长度为r的向量
		double l=len();
		if(!sgn(l))return *this;
		r/=l;return Point(x*r,y*r);
	}
	Point rotleft(){return Point(-y,x);}//逆时针旋转90 度
	Point rotright(){return Point(y,-x);}//顺时针旋转90 度
	Point rotate(double rad){//rad为弧度 逆时针旋转
		Point A=(*this);
		double c=cos(rad),s=sin(rad);
		return Point(A.x*c-A.y*s,A.x*s+A.y*c);
	}
	Point rotate(Point p,double angle){//绕p逆时针旋转后点
		Point v=((*this)-p).rotate(angle);
		return Point(p.x+v.x,p.y+v.y);
	}
	void input(){scanf("%lf%lf",&x,&y);}
	void show(){printf("(%.2lf %.2lf)\n",x,y);}
};
double cross(Point A,Point B,Point C){return (B-A)^(C-A);}//AB X AC
double dot(Point A,Point B,Point C){return (B-A)*(C-A);}//AB*AC
struct Line{
	Point s,e;
	Line(){};
	Line(Point s,Point e):s(s),e(e){};
	Line(Point p,double angle){//根据一个点和倾斜角angle 确定直线0<=angle<pi
		s=p;
		if(sgn(angle-pi/2)==0)e=(s+Point(0,1));
		else e=(s+Point(1,tan(angle)));
	}
	Line(double a,double b,double c){//ax+by+c=0
		if(!sgn(a)){
			s=Point(0,-c/b),e=Point(1,-c/b);
			if(sgn(b)<0)adjust();
		}
		else if(!sgn(b)){
			s=Point(-c/a,0),e=Point(-c/a,1);
			if(sgn(a)>0)adjust();
		}
		else{
			s=Point(0,-c/b),e=Point(1,(-c-a)/b);
			if(sgn(b)<0)adjust();
		}
	}
	void adjust(){swap(s,e);}
	int relation(Point p){//点和直线关系1在左侧 2在右侧 3在直线上
		int c=sgn((p-s)^(e-s));
		if(c<0)return 1;
		if(c>0)return 2;
		return 3;
	}
	bool pointonseg(Point p){//点在线段上的判断
		return !sgn((p-s)^(e-s))&&sgn((p-s)*(p-e))<=0;
	}
	bool parallel(Line v){//两向量平行(对应直线平行或重合)
		return !sgn((e-s)^(v.e-v.s));
	}
	int linecrossline(Line v){//两直线关系//0平行//1重合//2相交
		if((*this).parallel(v))return v.relation(s)==3;
		return 2;
	}
	int linecrossseg(Line v){//直线和线段相交判断//v:seg//2规范相交//1非规范相交//0不相交
		int d1=sgn((e-s)^(v.s-s));
		int d2=sgn((e-s)^(v.e-s));
		if((d1^d2)==-2)return 2;
		return !d1||!d2;
	}
	int segcrossseg(Line v){//两线段相交判断//2规范相交//1非规范相交//0不相交
		int d1=sgn((e-s)^(v.s-s));
		int d2=sgn((e-s)^(v.e-s));
		int d3=sgn((v.e-v.s)^(s-v.s));
		int d4=sgn((v.e-v.s)^(e-v.s));
		if((d1^d2)==-2&&(d3^d4)==-2)return 2;
		return (d1==0&&sgn((v.s-s)*(v.s-e))<=0)||(d2==0&&sgn((v.e-s)*(v.e-e))<=0)
		||(d3==0&&sgn((s-v.s)*(s-v.e))<=0)||(d4==0&&sgn((e-v.s)*(e-v.e))<=0);
	}
	double len(){return s.distance(e);}
	double angle(){//返回直线倾斜角0<=angle<pi
		double k=atan2(e.y-s.y,e.x-s.x);
		if(sgn(k)<0)k+=pi;
		if(!sgn(k-pi))k-=pi;
		return k;
	}
	Point crosspoint(Line v){//求两直线的交点//要保证两直线不平行或重合
		double a1=(v.e-v.s)^(s-v.s);
		double a2=(v.e-v.s)^(e-v.s);
		return Point((s.x*a2-e.x*a1)/(a2-a1),(s.y*a2-e.y*a1)/(a2-a1));
	}
	double dispointtoline(Point p){return fabs((p-s)^(e-s))/len();}//点到直线的距离
	double dispointtoseg(Point p){//点到线段的距离
		if(sgn((p-s)*(e-s))<0||sgn((p-e)*(s-e))<0)return min(p.distance(s),p.distance(e));
		return dispointtoline(p);
	}
	double dissegtoseg(Line v){//返回线段到线段的距离//前提是两线段不相交,相交距离就是0了
		return min(min(dispointtoseg(v.s),dispointtoseg(v.e)),min(v.dispointtoseg(s),v.dispointtoseg(e)));
	}
	Point lineprog(Point p){return s+(((e-s)*((e-s)*(p-s)))/((e-s).len2()));}//返回点p 在直线上的投影
	Point symmetrypoint(Point p){//返回点p关于直线的对称点
		Point q = lineprog(p);
		return Point(2*q.x-p.x,2*q.y-p.y);
	}
	void input(){s.input(),e.input();}
	void show(){s.show(),e.show();}
};
struct halfplane:public Line{
	double angle;
	halfplane(){}//表示向量s->e 逆时针(左侧) 的半平面
	halfplane(Point _s,Point _e){s=_s,e=_e;}
	halfplane(Line v){s=v.s,e=v.e;}
	void calcangle(){angle=atan2(e.y-s.y,e.x-s.x);}
	bool operator <(const halfplane &b)const{return angle<b.angle;}
};
struct halfplanes{
	halfplane hp[maxp];
	Point p[maxp];
	int n,st,ed;
	int que[maxp];
	void push(halfplane tmp){hp[n++]=tmp;}//逆时针首尾相接
	void unique(){//去重
		int m=1;
		for(int i=1;i<n;++i)
			if(sgn(hp[i].angle-hp[i-1].angle))hp[m++]=hp[i];
			else if(sgn((hp[m-1].e-hp[m-1].s)^(hp[i].s-hp[m-1].s))>0)hp[m-1]=hp[i];
		n=m;
	}
	bool halfplaneinsert(){
		for(int i=0;i<n;++i)hp[i].calcangle();
		sort(hp,hp+n),unique();
		que[st=0]=0,que[ed=1]=1;
		p[1]=hp[0].crosspoint(hp[1]);
		for(int i=2;i<n;++i){
			while(st<ed&&sgn((hp[i].e-hp[i].s)^(p[ed]-hp[i].s))<0)--ed;
			while(st<ed&&sgn((hp[i].e-hp[i].s)^(p[st+1]-hp[i].s))<0)++st;
			que[++ed]=i;
			if(hp[i].parallel(hp[que[ed-1]]))return false;
			p[ed]=hp[i].crosspoint(hp[que[ed-1]]);
		}while(st<ed&&sgn((hp[que[st]].e-hp[que[st]].s)^(p[ed]-hp[que[st]].s))<0)--ed;
		while(st<ed&&sgn((hp[que[ed]].e-hp[que[ed]].s)^(p[st+1]-hp[que[ed]].s))<0)++st;
		if(st+1>=ed)return false;
		return true;
	}
};
int v[N],u[N],w[N];
halfplanes A;
int main(){
	int n;
	scanf("%d",&n);
	for(int i=0;i<n;++i)scanf("%d%d%d",v+i,u+i,w+i);
	for(int i=0;i<n;++i){
		int ok=1;
		A.n=0;
		for(int j=0;j<n;++j)if(i^j){
			if(v[i]<=v[j]&&u[i]<=u[j]&&w[i]<=w[j]){ok=0;break;}
			if(v[i]>=v[j]&&u[i]>=u[j]&&w[i]>=w[j])continue;
			double sum=1.0*u[i]*v[i]*w[i]*u[j]*v[j]*w[j];
			double a=(sum/v[j]-sum/w[j])-(sum/v[i]-sum/w[i]);
			double b=(sum/u[j]-sum/w[j])-(sum/u[i]-sum/w[i]);
			double c=sum/w[j]-sum/w[i];
			A.push(halfplane(Line(a,b,c)));
		}
		if(ok){
			A.push(halfplane(Point(eps,1),Point(eps,eps)));
			A.push(halfplane(Point(eps,eps),Point(1,eps)));
			A.push(halfplane(Point(1,eps),Point(eps,1)));
			if(!A.halfplaneinsert())ok=0;
		}
		puts(ok?"Yes":"No");
	}
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值