POJ 2318 TOYS(计算几何)(二分)

TOYS

大意:给你一个箱子,有n个挡板分隔成n+1部分,给你m个玩具的坐标,问每一部分有几个玩具。

思路:举对每个玩具,二分线段下标,判断玩具在线段左边还是右边,枚举后统计。

  1 #include <map>
  2 #include <stack>
  3 #include <queue>
  4 #include <math.h>
  5 #include <stdio.h>
  6 #include <stdlib.h>
  7 #include <string.h>
  8 #include <iostream>
  9 #include <limits.h>
 10 #include <algorithm>
 11 #define LL long long
 12 #define max(a,b) ((a)>(b)?(a):(b))
 13 #define min(a,b) ((a)<(b)?(a):(b))
 14 #define max3(a, b, c) (a>b?max(a, c):max(b, c))
 15 #define min3(a, b, c) (a<b?min(a, c):min(b, c))
 16 #define max4(a, b, c, d) max(max(a, b), max(c, d))
 17 #define min4(a, b, c, d) min(min(a, b), min(c, d))
 18 #define eps 1e-9
 19 #define INF 1 << 30
 20 using namespace std;
 21 
 22 int n, m;
 23 int cnt[5010];
 24 
 25 struct Point
 26 {
 27     int x, y;
 28 } P;
 29 
 30 struct node
 31 {
 32     Point a, b;
 33 } Line[5010];
 34 
 35 int Location(Point a, Point b, Point c)
 36 {
 37     return (a.x-c.x)*(b.y-c.y) - (b.x-c.x)*(a.y-c.y);
 38 }
 39 
 40 void Binary_Search(Point a, int n)
 41 {
 42     int l, r, mid;
 43     l = 0;
 44     r = n-1;
 45     while(l < r)
 46     {
 47         mid = (l+r)>>1;
 48         if(Location(a, Line[mid].a, Line[mid].b) > 0)
 49         {
 50             l = mid+1;
 51         }
 52         else
 53         {
 54             r = mid;
 55         }
 56     }
 57     if(Location(a, Line[l].a, Line[l].b) < 0)
 58     {
 59         cnt[l]++;
 60     }
 61     else
 62     {
 63         cnt[l+1]++;
 64     }
 65 }
 66 
 67 void Solve()
 68 {
 69     int x1, x2, y1, y2, t1, t2;
 70     while(~scanf("%d", &n) && n)
 71     {
 72         memset(cnt, 0, sizeof(cnt));
 73         scanf("%d%d%d%d%d", &m, &x1, &y1, &x2, &y2);
 74         for(int i = 0; i < n; ++i)
 75         {
 76             scanf("%d%d", &t1, &t2);
 77             Line[i].a.x = t1;
 78             Line[i].a.y = y1;
 79             Line[i].b.x = t2;
 80             Line[i].b.y = y2;
 81         }
 82         for(int i = 0; i < m; ++i)
 83         {
 84             scanf("%d%d", &P.x, &P.y);
 85             Binary_Search(P, n);
 86         }
 87         for(int i = 0; i <= n; ++i)
 88         {
 89             printf("%d: %d
", i, cnt[i]);
 90         }
 91 
 92         printf("
");
 93     }
 94 }
 95 
 96 int main(void)
 97 {
 98     freopen("data.in", "r", stdin);
 99     //freopen("data.out", "w", stdout);
100     Solve();
101 
102     return 0;
103 }
TOYS
原文地址:https://www.cnblogs.com/Silence-AC/p/3586796.html