NYOJ 6: Sprinkler (I) (Greed)

Keywords: Programming iOS

6-Sprinkler (I)

  • Memory Limit: 64MB Time Limit: 3000ms Special Judgment: No
  • Number of Passes: 68 Submissions: 111 Difficulties: 3

Title Description:

There is a lawn which is 20 meters long and 2 meters wide. A sprinkler with a radius of Ri should be placed on the horizontal center line. The effect of each sprinkler will make the circle with the radius of Ri as the center (0 < Ri < 15) moist. There are enough sprinklers i (1 < i < 600), and the lawn will be moist. What you have to do is to select as few sprinklers as possible and to make the whole grass moist. All the wetness of the flat.

Input Description:

The first line m represents a set of m test data
 In the first row of each set of test data, there is an integer n, which means there are n sprinklers. In the next row, there are n real numbers ri, which means the radius of the circle that the sprinkler can cover.

Output description:

Number of devices used for output

Sample input:

2
5
2 3.2 4 4.5 6 
10
1 2 3 1 2 1.2 3 1.1 1 2

Sample output:

2
5

thinking

Because the minimum sprinkler is required, the radius of the sprinkler is sequenced from large to small, and then the length of the intersection of the circle and the lawn boundary is calculated with a little geometric knowledge until the lawn is completely covered by the sprinkler and stopped.

AC code

#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <math.h>
#include <limits.h>
#include <map>
#include <stack>
#include <queue>
#include <vector>
#include <set>
#include <string>
#include <time.h>
#define ll long long
#define ull unsigned long long
#define ms(a,b) memset(a,b,sizeof(a))
#define pi acos(-1.0)
#define INF 0x7f7f7f7f
#define lson o<<1
#define rson o<<1|1
#define bug cout<<"---------"<<endl
#define debug(...) cerr<<"["<<#__VA_ARGS__":"<<(__VA_ARGS__)<<"]"<<"\n"
const double E=exp(1);
const int maxn=1e6+10;
const int mod=1e9+7;
using namespace std;
double a[maxn];
bool cmp(double a,double b)
{
	return a>b;
}
int main(int argc, char const *argv[])
{
	ios::sync_with_stdio(false);
	#ifndef ONLINE_JUDGE
	    freopen("in.txt", "r", stdin);
	    freopen("out.txt", "w", stdout);
	    double _begin_time = clock();
	#endif
	int t;
	int n;
	cin>>t;
	while(t--)
	{
		double s=20.0;
		cin>>n;
		for(int i=0;i<n;i++)
			cin>>a[i];
		sort(a,a+n,cmp);
		int ans=0;
		for(int i=0;i<n;i++)
		{
			s-=2*sqrt(a[i]*a[i]-1) ;
			ans++;
			if(s<=0)
				break;
		}
		cout<<ans<<endl;
	}
	#ifndef ONLINE_JUDGE
	    double _end_time = clock();
	    printf("time = %lf ms.", _end_time - _begin_time);
	#endif
	return 0;
}

 

Posted by Solar on Fri, 01 Feb 2019 16:51:15 -0800