POJ_2318

    可以二分线段的位置,然后利用叉积去判断点在线段的左边还是右边即可。

#include<stdio.h>
#include<string.h>
#define MAXD 5010
int N, M, X1, Y1, X2, Y2, a[MAXD], b[MAXD], h[MAXD];
long long int det(int x1, int y1, int x2, int y2)
{
return (long long int)x1 * y2 - (long long int)x2 * y1;
}
void init()
{
int i, j, k;
a[0] = b[0] = X1;
for(i = 1; i <= N; i ++)
scanf("%d%d", &a[i], &b[i]);
}
void solve()
{
int i, j, k, x, y, mid, min, max;
memset(h, 0, sizeof(h[0]) * (N + 1));
for(i = 0; i < M; i ++)
{
scanf("%d%d", &x, &y);
min = 0, max = N + 1;
for(;;)
{
mid = (max - min) / 2 + min;
if(mid == min)
break;
if(det(a[mid] - b[mid], Y1 - Y2, x - b[mid], y - Y2) < 0)
min = mid;
else
max = mid;
}
++ h[mid];
}
for(i = 0; i <= N; i ++)
printf("%d: %d\n", i, h[i]);
}
int main()
{
int t = 0;
while(scanf("%d%d%d%d%d%d", &N, &M, &X1, &Y1, &X2, &Y2) == 6)
{
if(t ++)
printf("\n");
init();
solve();
}
return 0;
}


posted on 2012-02-07 06:46  Staginner  阅读(236)  评论(0编辑  收藏  举报