题目链接:https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=291
cross product: for 2 vectors a and b, if a^b > 0, a is in the clockwise direction of b. else if a^b < 0, a is in the anti-clockwise direction of vector b. Otherwise, a^b==0, vector a and b are with the same direction or opposite direction.这道题用cross product计算。每一个点与transmitter的中心形成一个vector,对于每一个vector,循环剩余的vector,所有vector在同一方向的可以看做被覆盖的点。代码如下:
1 //============================================================================ 2 // Name : test.cpp 3 // Author : 4 // Version : 5 // Copyright : Your copyright notice 6 // Description : Hello World in C++, Ansi-style 7 //============================================================================ 8 9 #include <iostream> 10 #include <math.h> 11 #include <stdio.h> 12 #include <cstdio> 13 #include <algorithm> 14 #include <string.h> 15 #include <string> 16 #include <sstream> 17 #include <cstring> 18 #include <queue> 19 #include <vector> 20 #include <functional> 21 #include <cmath> 22 #include <set> 23 #define SCF(a) scanf("%d", &a) 24 #define IN(a) cin>>a 25 #define FOR(i, a, b) for(int i=a;i<b;i++) 26 #define Infinity 999999999 27 #define PI 3.14159265358979323846 28 typedef long long Int; 29 using namespace std; 30 31 struct point { 32 int x, y; 33 }; 34 35 double dis(point a, point b) 36 { 37 return pow(a.x - b.x, 2) + pow(a.y - b.y, 2); 38 } 39 40 double length(point a) 41 { 42 return sqrt(pow(a.x, 2) + pow(a.y, 2)); 43 } 44 45 double dotProduct(point a, point b) 46 { 47 return a.x*b.x + a.y*b.y; 48 } 49 50 double crossProduct(point a, point b) 51 { 52 return a.x*b.y - b.x*a.y; 53 } 54 55 double cosAngle(point a, point b) 56 { 57 double an = dotProduct(a, b) / (length(a)*length(b)); 58 return an; 59 } 60 61 int main() 62 { 63 point radar; 64 double r; 65 int N; 66 point points[155]; 67 double angle[155]; 68 while (scanf("%d %d %lf", &radar.x, &radar.y, &r) != EOF) 69 { 70 if (r < 0) 71 break; 72 SCF(N); 73 int index = 0; 74 point cp; 75 FOR(i, 0, N) 76 { 77 SCF(cp.x); 78 SCF(cp.y); 79 if (dis(radar, cp) <= pow(r, 2)) 80 { 81 points[index].x = cp.x - radar.x; 82 points[index++].y = cp.y - radar.y; 83 } 84 } 85 86 int maxAns = 0; 87 FOR(i, 0, index) 88 { 89 int cn = 1; 90 FOR(j, 0, index) 91 { 92 if (i != j) 93 { 94 double rela = crossProduct(points[i], points[j]); 95 if (rela >= 0) 96 cn++; 97 } 98 } 99 maxAns = max(maxAns, cn); 100 } 101 printf("%d\n", maxAns); 102 } 103 return 0; 104 }