0
点赞
收藏
分享

微信扫一扫

TOYS POJ - 2318

​​http://poj.org/problem?id=2318​​

利用叉积判断点在线段所确定直线的哪一侧 二分一下即可

叉积参考​

 

#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
typedef long long ll;
const int maxn=5e3+10;

ll up[maxn],down[maxn],x[maxn],y[maxn];
int ans[maxn];
ll x1,x2,y1,y2;
int n,m;

ll getval(ll x1,ll y1,ll x2,ll y2)
{
return x1*y2-x2*y1;
}

int judge(ll xx,ll yy,int pos)
{
ll res1,res2;
res1=getval(xx-down[pos],yy-y1,up[pos]-down[pos],y2-y1);
res2=getval(xx-down[pos+1],yy-y1,up[pos+1]-down[pos+1],y2-y1);
if(res1*res2<0) return 0;
else{
if(res1<0) return -1;
else return 1;
}
}

int main()
{
int i,l,r,mid,res,p;
while(scanf("%d",&n)!=EOF){
if(n==0) break;
scanf("%d%lld%lld%lld%lld",&m,&x1,&y1,&x2,&y2);
swap(y1,y2);
up[0]=x1,down[0]=x1;
up[n+1]=x2,down[n+1]=x2;
for(i=1;i<=n;i++){
scanf("%lld%lld",&up[i],&down[i]);
}
for(i=1;i<=m;i++){
scanf("%lld%lld",&x[i],&y[i]);
}
for(i=0;i<=n;i++) ans[i]=0;
for(i=1;i<=m;i++){
if(x[i]==x1) ans[0]++;
else if(x[i]==x2) ans[n]++;
else{
l=0,r=n;
while(l<=r){
mid=(l+r)/2;
res=judge(x[i],y[i],mid);
if(res==0){
p=mid;
break;
}
else if(res==-1) r=mid-1;
else l=mid+1;
}
ans[p]++;
}
}
for(i=0;i<=n;i++){
printf("%d: %d\n",i,ans[i]);
}
printf("\n");
}
return 0;
}

/*
1 1 0 2 4 0
3 1
3 1
*/

 


举报

相关推荐

0 条评论