Water Spraying Device (i.)Time limit: Ms | Memory limit: 65535 KB Difficulty: 3 describes the existing lawn, 20 meters in width, 2 meters wide, to place a spray device with a radius of Ri on the horizontal centerline, the effect of each water spray device will let the circle with its center radius for the real Ri (0<ri<15) be moist, There is plenty of water spray device I (1<i<600), and must be able to wet all the lawn, you have to do is: choose as little as possible water spray device, the whole lawn moist. Enter the first line m to indicate that there is a m group of test data
The first line of each set of test data has an integer number N,n represents a total of n water jets, followed by a row of n real Ri,ri that indicate the radius of the circle that the water spray device can cover. Sample input for the number of devices used in the output output
2
5
2 3.2 4 4.5 6 1 2 3
1 2 1.2 3 1.1 1 2
Sample output
2
5
Greedy algorithm
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int cmp (const void *a,const void *b)
{
return * (double *) b >* (double *) a? 1:-1;
}
int main ()
{
int n;
scanf ("%d", &n);
while (n--)
{
int m,i;
Double a[600];
scanf ("%d", &m);
for (i=0;i<m;i++)
{
scanf ("%lf", &a[i]);
printf ("%2.lf", A[i]);
}
Qsort (a,m,sizeof (double), CMP);
Double sum=0.0;
int t=0;
for (i=0;i<m;i++)
{
sum+=sqrt (a[i]*a[i]-1);
t++;
if (sum>=10) break
;
}
printf ("%d\n", t);
}
return 0;
}
Water spraying Device (ii)
time limit: Ms | Memory limit: 65535 KB
Difficulty: 4
Description
There is a lawn, the transverse length w, longitudinal length is H, in its horizontal center line at different locations with N (n<=10000) point-like water spray device,
The effect of spraying water on each water spray device is to let the circle with its center radius as RI be wetted. Please select as few water spray devices as possible in the given sprinkler system to
moisten the entire lawn. Enter the
first line to enter a positive integer n to indicate a total of n test data.
the first line of each set of test data has three integers n,w,h,n indicates a total of n water spray devices, W represents the transverse length of the lawn, and
h indicates the longitudinal length of the lawn.
in the subsequent n rows, there are two integers, Xi and ri,xi, which represent the horizontal axis of the first water sprinkler (0 on the left), and the
ri represents the radius of the circle that the water spray device can cover.
output
A positive integer for each set of test data, indicating how many water jets are required, each with a single line of output.
If there is no solution that can damp the whole lawn, output 0.
Sample Input
2
2 8 6
1 1 4 5 2 6 4 5 6
5
Sample Output
1
2
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
struct Node
{
Double lef;
Double rig;
} S[10001];
int cmp (const void *a, const void *b)
{
node * c = (node *) A;
Node * d = (node *) b;
if (C->lef > D->lef)
{
return 1;
}
else
{
return-1;
}
}
int main ()
{
int n,w,h,t;
scanf ("%d", &t);
while (t--)
{
scanf ("%d%d%d", &n,&w,&h);
int i,x,r,t;
for (i=0;i<n;i++)
{
scanf ("%d%d", &x,&r);
if (R<H/2) continue;
t = sqrt (R*R-H/2*H/2);
S[i].lef = x-t;
S[i].rig = x + t;
}
int ans = 0, f = 1;
Double sum = 0, max = 0;
Qsort (S,n,sizeof (s[0]), CMP);
while (sum<w)
{
max = 0;
for (i=0;i<n;i++)
{
if (s[i].lef<=sum && max<s[i].rig-sum)
{
max = S[i].rig-sum ;
}
}
if (max==0)
{
f = 0;
break;
}
Ans + +;
sum + = max;
}
if (f) printf ("%d\n", ans);
else printf ("0\n");
}
return 0;
}