POJ2653Pick-up sticks
题意:
按照顺序给你n根棍子,判断最后有多少棍子在最上面,就是它不会和之后的棍子相交。
思路:
N 2 N^2 N2判断每根棍子和后面的是否相交即可。
代码:
#include<iostream>
#include<stdio.h>
#include<stdlib.h>
#include<algorithm>
#include<string.h>
#include<ctype.h>
#include<queue>
#include<set>
#include<stack>
#include<cmath>
#include<map>
#define pii pair<int,int>
#define ll long long
#define cl(x,y) memset(x,y,sizeof(x))
#define ct cerr<<"Time elapsed:"<<1.0*clock()/CLOCKS_PER_SEC<<"s.\n";
#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define all(x) x.begin(),x.end()
#define lson x<<1,l,mid
#define rson x<<1|1,mid+1,r
#define INF 1e18
const int N=1e6+10;
const int mod=1e9+7;
const int inf=0x3f3f3f3f;
const double eps=1e-8;
const double pi=acos(-1);
using namespace std;
int sgn(double x)
{
if(fabs(x) < eps)return 0;
if(x < 0) return -1;
else return 1;
}
struct point
{
double x,y;
point(){}
point(double xx,double yy){x=xx;y=yy;}
point operator +(point b){return point(x+b.x,y+b.y);}
point operator -(point b){return point(x-b.x,y-b.y);}
double operator ^(point b){return x*b.y-y*b.x;}//叉乘
double operator *(point b){return x*b.x+y*b.y;}//点乘
point operator *(double b){return point(x*b,y*b);}//数乘
void read(){scanf("%lf%lf",&x,&y);}
};
struct line
{
point s,e,l;
line(){}
line(point ss,point ee){s = ss;e = ee;l=e-s;}
void read(){s.read(),e.read();l=e-s;}
}l[N];
bool judge(line l1,line l2)
{
point a=l1.s,b=l1.e,c=l2.s,d=l2.e;
double x1=((c-a)^l1.l)*((d-a)^l1.l);
double x2=((a-c)^l2.l)*((b-c)^l2.l);
if(x1==0 || x2==0)
return sgn((a-c)*(b-c))<=0 || sgn((a-d)*(b-d))<=0 || sgn((c-a)*(d-a))<=0 || sgn((c-b)*(d-b))<=0;
return x1<0&&x2<0;
}
int main()
{
int n;
while(~scanf("%d",&n) && n)
{
int i,j;
for(i=1;i<=n;i++)
l[i].read();
vector<int> ans;
for(i=1;i<=n;i++)
{
int f=1;
for(j=i+1;j<=n;j++)
if(judge(l[i],l[j]))
{
f=0;
break;
}
if(f)
ans.pb(i);
}
printf("Top sticks: ");
for(i=0;i<ans.size();i++)
{
if(i==ans.size()-1)
printf("%d.\n",ans[i]);
else
printf("%d, ",ans[i]);
}
}
return 0;
}