Transmitters

Time Limit: 2 Seconds      Memory Limit: 65536 KB

In a wireless network with multiple transmitters sending on the same frequencies, it is often a requirement that signals don't overlap, or at least that they don't conflict. One way of accomplishing this is to restrict a transmitter's coverage area. This problem uses a shielded transmitter that only broadcasts in a semicircle.

A transmitter T is located somewhere on a 1,000 square meter grid. It broadcasts in a semicircular area of radius r. The transmitter may be rotated any amount, but not moved. Given N points anywhere on the grid, compute the maximum number of points that can be simultaneously reached by the transmitter's signal. Figure 1 shows the same data points with two different transmitter rotations.

All input coordinates are integers (0-1000). The radius is a positive real number greater than 0. Points on the boundary of a semicircle are considered within that semicircle. There are 1-150 unique points to examine per transmitter. No points are at the same location as the transmitter.

Input consists of information for one or more independent transmitter problems. Each problem begins with one line containing the (x,y) coordinates of the transmitter followed by the broadcast radius, r. The next line contains the number of points N on the grid, followed by N sets of (x,y) coordinates, one set per line. The end of the input is signalled by a line with a negative radius; the (x,y) values will be present but indeterminate. Figures 1 and 2 represent the data in the first two example data sets below, though they are on different scales. Figures 1a and 2 show transmitter rotations that result in maximal coverage.

For each transmitter, the output contains a single line with the maximum number of points that can be contained in some semicircle. 

Example input:

25 25 3.5
7
25 28
23 27
27 27
24 23
26 23
24 29
26 29
350 200 2.0
5
350 202
350 199
350 198
348 200
352 200
995 995 10.0
4
1000 1000
999 998
990 992
1000 999
100 100 -2.5


Example output:

3
4
4

 

记得大一的时候做过这道题目,只想练一下手,还是花了很长时间啊。然这道题目颠覆了我快速排序的世界观,为什么用“return cc->rad - dd->rad;”排序没效果了,而用“return cc->rad > dd->rad;”居然可以?

ZOJ 1041 Transmitters(叉积)_快速排序ZOJ 1041 Transmitters(叉积)_快速排序_02View Code
 1 # include<stdio.h>
 2 # include<stdlib.h>
 3 # include<math.h>
 4 # define pi acos(-1)
 5 struct node{
 6     double x,y;
 7     double rad;
 8     double d;
 9 }s1[160],s2[160];
10 double a,b,r;
11 double f(double x,double y){
12     return sqrt((x-a)*(x-a)+(y-b)*(y-b));
13 }
14 double g(double x,double y){
15     if(x==0 && y>0)
16         return pi/2.0;
17     if(x==0 && y<0)
18         return 3.0*pi/2.0;
19     if(x>0 && y>=0)
20         return atan(y/x);
21     if(x>0 && y<0)
22         return 2*pi+atan(y/x);
23     if(x<0 && y>=0)
24         return pi+atan(y/x);
25     if(x<0 && y<0)
26         return pi+atan(y/x);
27 }
28 int cmp(const void *c,const void *d){
29     struct node *cc = (node *)c;
30     struct node *dd = (node *)d;
31     return cc->rad > dd->rad;
32 }
33 int main(){
34     int i,j;
35     int max,sum,ans,n;
36     while(scanf("%lf%lf%lf",&a,&b,&r)!=EOF){
37         if(r<0)
38             break;
39         scanf("%d",&n);
40         ans=0;
41         for(i=0,j=0;i<n;i++){
42             scanf("%lf%lf",&s1[i].x,&s1[i].y);
43             if(s1[i].x==a &&s1[i].y==b)
44             {
45                 ans++;
46                 continue;
47             }
48             s1[i].d = f(s1[i].x,s1[i].y);
49             if(s1[i].d <= r)
50             {
51                 s2[j].x = s1[i].x-a;
52                 s2[j].y = s1[i].y-b;
53                 s2[j].rad = g(s2[j].x,s2[j].y);
54                 j++;
55             }
56         }
57         int temp=j;
58         max = 0;
59         qsort(s2,temp,sizeof(s2[0]),cmp);
60         for(i=0;i<temp;i++){
61             double tt=s2[i].rad;
62             sum = 1;
63             if(tt<=pi){
64                 for(j=i+1;j<temp;j++){
65                     if(s2[j].rad>tt+pi)
66                         break;
67                     sum++;
68                 }
69                 if(sum>max)
70                     max = sum;
71             }
72             else{
73                 for(j=i+1;j<temp;j++){
74                     sum++;
75                 }
76                 for(j=0;j<temp;j++){
77                     if(s2[j].rad > tt-pi)
78                         break;
79                     sum++;
80                 }
81                 if(sum>max)
82                     max = sum;
83             }
84         }
85         printf("%d\n",max+ans);
86     }
87     return 0;
88 }

 大牛的做法是叉积,着个我都没怎么用过,附贴代码如下:

ZOJ 1041 Transmitters(叉积)_快速排序ZOJ 1041 Transmitters(叉积)_快速排序_02View Code
 1 #include<stdio.h>
 2 int n;
 3 double x0,y0;
 4 struct point{
 5          double xx,yy;
 6 }s[300];
 7 double dis(double x,double y)
 8 {
 9          return ((x0-x)*(x0-x)+(y0-y)*(y0-y));
10 }
11 int compare(int k)
12 {
13          int i,top=0;
14          double a,b;
15          for(i=0;i<n;i++)
16          {
17                    a=(s[k].xx-x0)*(s[i].yy-y0);
18                    b=(s[i].xx-x0)*(s[k].yy-y0);
19                    if(b-a>=0)
20                             top++;
21          }
22                    return top;
23 }
24 int main()
25 {
26          double r,x,y,max;
27          int i;
28          while(scanf("%lf%lf%lf",&x0,&y0,&r)!=-1,r>=0)
29          {
30                    scanf("%d",&n);
31                    i=0;
32                    while(n--)
33                    {
34                             scanf("%lf%lf",&x,&y);
35                             if(dis(x,y)<=r*r)
36                             {
37                                      s[i].xx=x;
38                                      s[i++].yy=y;
39                             }
40                    }
41                    n=i;max=0;
42                    for(i=0;i<n;i++)
43                    {
44                             if(max<compare(i))
45                                      max=compare(i);
46                    }
47                    printf("%d\n",(int)max);
48          }
49          return 0;
50 }

 

把每一件简单的事情做好,就是不简单;把每一件平凡的事情做好,就是不平凡!相信自己,创造奇迹~~