Description
There are many beacon lights on the sea, lighting for passing vessels. In terms of plane, the range of sea areas is [1, 10 ^ 8] × [1, 10 ^ 8].
(Figure 1)
As shown in figure 1, each beacon has a searchlight that illuminates two right-corner regions in the Northeast and Southwest China. The searchlight is powerful enough to cover any distance. The beacon itself is so small that it can be assumed that they will not block each other.
(Figure 2)
If Beacon A and beacon B are within the illumination range of the other party, they are said to be able to illuminate each other. For example, in the example in Figure 2, the blue and red light towers can illuminate each other. The blue and green beacon are not, and the red and green beacon are not.
Now, for any set of beacon, calculate how many pairs of beacon lights can illuminate each other.
Input
N + 1 rows in total.
1st act 1 integer N, indicating the total number of beacon.
Each line between 2nd and n + 1 contains two integers x and y, which respectively represent the horizontal and vertical coordinates of each beacon.
Output
An integer represents the number of beacon pairs that can illuminate each other.
Input example
32 24 35 1
Output example
1
1 # include<stdio.h> 2 3 int quick(int a[],int A[],int b, int c){ 4 int inversion = 0; 5 if (c > b){ 6 int middle = (int)((b + c) / 2); 7 int u = b; int v = c; 8 inversion+=quick(a,A,b,middle); 9 inversion+= quick(a, A, middle + 1, c);10 inversion+= merge(a, A, u, middle, v);11 }12 return inversion;13 }14 15 16 int merge(int a[],int B[], int b, int m, int c){17 int i = b; int j = m + 1; int k = 0; int inversion = 0;18 while (i <= m && j <= c)19 {20 if (a[i] <= a[j])21 { 22 B[k++] = a[i++]; 23 inversion= inversion +c-j+1;24 }25 else{26 B[k++] = a[j++];27 }28 }29 while (i <= m)30 B[k++] = a[i++];31 while (j <= c)32 B[k++] = a[j++];33 34 for (i = 0; i < k; i++)35 a[b + i] = B[i];36 return inversion;37 }38 39 void main(){40 int A[18] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }; int a[18];41 int Source_inversion = 0; int inversion = 0;42 for (int i = 0; i < 18; i++){43 a[i] = rand() % 20;44 }45 for (int i = 0; i < 18; i++){46 printf("%d ", a[i]);47 }48 49 for (int t = 0; t < 17; t++){50 for (int m = t + 1; m < 18;m++ )51 {52 if (a[m] >= a[t])53 Source_inversion++;54 }55 }56 57 printf("\nSource method: %d\n", Source_inversion);58 59 60 inversion=quick(a,A,0,17);61 printf("\n\n");62 for (int i = 0; i < 18; i++){63 printf("%d ", a[i]);64 }65 printf("\nMerge sort count:%d",inversion);66 system("pause");67 }
The beacon of C implementation