[Question Solution] #10002. "One Book Pass 1.1 Example 3" Water Spray Device

Title source: loj

Title description

There are n watering sprinklers in the lawn of length L meters and width W meters. Each sprinkler is installed on the centerline of the lawn (W/2 meters away from each side). We know the location of each sprinkler (the distance from the left end of the lawn centerline) and the watering range it can cover.

Excuse me: If you want to water a whole lawn at the same time, how many sprinklers need to be opened at least?

Input format

The input contains several sets of test data.

An integer T in the first line represents the number of data groups;

The first row of each group of data is the integers n, L and W;

The next n lines, each line contains two integers, giving the position and irrigation radius of a sprinkler (the above schematic diagram is the case described by the sample input of the first set of data).

Output format

Output a number for each set of test data, indicating the minimum number of sprinklers needed to water the entire lawn. If all the sprinklers are turned on and the entire lawn cannot be watered, the output is −1.

Sample input

3
8 20 2
5 3
4 1
1 2
7 2
10 2
13 3
16 2
19 4
3 10 1
3 5
9 3
6 1
3 10 1
5 3
1 1
9 1

Sample output

6
2
-1

data range

For 100% of the data, n≤15000.

Ideas

A concise and easy to understand picture:
Insert picture description here
first filter out the nozzle i that can not spray or just spray to the upper and lower boundaries, because if i can not spray or just sprays to the upper and lower boundaries, then it is better to have a nozzle j that sprays the upper and lower boundaries. (Otherwise, you can never water the entire lawn)

Then you can convert this question into a complete interval coverage problem. As shown in the figure above, the rectangle is to be watered. You can convert the circle into dis in the figure. This is equivalent to choosing as few intervals as possible to cover the length of the rectangle among several diss .

How to choose? Greedy strategy

Every election to cover the currently selected point of maximum right point

This question has many small details that need attention, see the code for details

code

#include<bits/stdc++.h>
using namespace std;
const int N=15010;
int T,n,L,W;
struct node{
    
    
	double st,ed;
}a[N];
int cmp(node x,node y) {
    
     return x.st<y.st; }
int main()
{
    
    
	scanf("%d",&T);
    for (int i=1;i<=T;i++)
    {
    
    
    	int cnt=0,anss=0;
    	scanf("%d%d%d",&n,&L,&W);
    	for (int i=1;i<=n;i++)
    	{
    
    
    		int x,r;
    		scanf("%d%d",&x,&r);
    		if (r<=W/2) continue;
    		cnt++;
    		a[cnt].st=x-sqrt(r*r-W*W/4.0);
    		a[cnt].ed=x+sqrt(r*r-W*W/4.0);
		}
		sort(a+1,a+1+cnt,cmp);
		int i=1;
		double now_st=0,now_ed=0;
		bool pd=true;
		while (now_ed<L)
		{
    
    
			now_st=now_ed;
			while (a[i].st<=now_st && i<=cnt) //能覆盖当前点 
			{
    
    
				if (a[i].ed>now_ed)  //贪心 
			      now_ed=a[i].ed;
				i++;
			}
			
			if (now_st==now_ed && now_ed<L)
			{
    
    
				cout<<-1<<endl;
				pd=false;
				break;
			}
			anss++;
		}
		if (pd==true) cout<<anss<<endl;
	}
	return 0;
}

Guess you like

Origin blog.csdn.net/weixin_45485187/article/details/102700113