POJ 2318 (叉积) TOYS

题意:

POJ 2318 (叉积) TOYS

有一个长方形,里面从左到右有n条线段,将矩形分成n+1个格子,编号从左到右为0~n。

端点分别在矩形的上下两条边上,这n条线段互不相交。

现在已知m个点,统计每个格子中点的个数。

分析:

用叉积判断点与线段的相对位置,对于每个点二分查找所在的格子。

 #include <cstdio>
#include <cmath>
#include <cstring> struct Point
{
int x, y;
Point(int x=, int y=):x(x), y(y) {}
};
typedef Point Vector; Point read_point()
{
int x, y;
scanf("%d%d", &x, &y);
return Point(x, y);
} Point operator + (const Point& A, const Point& B)
{ return Point(A.x+B.x, A.y+B.y); } Point operator - (const Point& A, const Point& B)
{ return Point(A.x-B.x, A.y-B.y); } int Cross(const Point& A, const Point& B)
{ return A.x*B.y - A.y*B.x; } const int maxn = + ;
int up[maxn], down[maxn], ans[maxn];
int n, m, kase = ;
Point A0, B0; int binary_search(const Point& P)
{
int L = , R = n;
while(L < R)
{
int mid = L + (R - L + ) / ;
Point A(down[mid], B0.y), B(up[mid], A0.y);
Vector v1 = B - A;
Vector v2 = P - A;
if(Cross(v1, v2) < ) L = mid;
else R = mid - ;
}
return L;
} int main()
{
//freopen("in.txt", "r", stdin); while(scanf("%d", &n) == && n)
{
memset(ans, , sizeof(ans)); scanf("%d", &m); A0 = read_point(); B0 = read_point();
for(int i = ; i <= n; ++i) scanf("%d%d", &up[i], &down[i]);
for(int i = ; i < m; ++i)
{
Point P; P = read_point();
int pos = binary_search(P);
ans[pos]++;
} if(kase++) puts("");
for(int i = ; i <= n; ++i) printf("%d: %d\n", i, ans[i]);
} return ;
}

代码君

上一篇:Jquery笔记和ajax笔记


下一篇:OpenCV 学习笔记 07 支持向量机SVM(flag)