12-喷水装置(二)

题目来源:http://acm.nyist.cf/problem/12

 

题目描述:

有一块草坪,横向长w,纵向长为h,在它的橫向中心线上不同位置处装有n(n<=10000)个点状的喷水装置,每个喷水装置i喷水的效果是让以它为中心半径为Ri的圆都被润湿。请在给出的喷水装置中选择尽量少的喷水装置,把整个草坪全部润湿。

输入描述:

第一行输入一个正整数N表示共有n次测试数据。
每一组测试数据的第一行有三个整数n,w,h,n表示共有n个喷水装置,w表示草坪的横向长度,h表示草坪的纵向长度。
随后的n行,都有两个整数xi和ri,xi表示第i个喷水装置的的横坐标(最左边为0),ri表示该喷水装置能覆盖的圆的半径。

输出描述:

每组测试数据输出一个正整数,表示共需要多少个喷水装置,每个输出单独占一行。
如果不存在一种能够把整个草坪湿润的方案,请输出0。

 

样例输入:

2
2 8 6
1 1
4 5
2 10 6
4 5
6 5

样例输出:

1
2 

思路:就是把中心线也就是横坐标当轴,以覆盖面积为区间,简单的区间覆盖;

收获:学到了类似结构体的vector 

代码如下:

#include <iostream>
#include <vector>
#include <cmath>
#include <algorithm>
using namespace std;
double f(double r,double h)
{
    return sqrt(r*r-h*h);
}
bool cmp(pair<double,double> a,pair<double,double> b)
{
    return a.first<b.first;
}
int main()
{
    int N;
    cin>>N;
    while(N--)
    {
        int n,w,h;
        cin>>n>>w>>h;
        vector<pair<double,double> >vec;
        for(int i=0; i<n; i++)
        {
            int x,r;
            cin>>x>>r;
            if(r>h/2.0)
            {
                double l=f(r,h/2.0);
                pair<double,double> p;
                p.first=x-l;
                p.second=x+l;
                vec.push_back(p);
            }
        }
        sort(vec.begin(),vec.end(),cmp);
        double right=0.0;
        int cnt=0;
        while(right<w)
        {
            double maxl=0.0;
            for(int i=0; i<(int)vec.size() && vec[i].first<=right; i++)
            {
                if(vec[i].second-right>maxl)
                    maxl=vec[i].second-right;
            }
            if(maxl!=0)
            {
                cnt++;
                right+=maxl;
            }
            else
                break;
        }
        if(right>=w)
            cout<<cnt<<endl;
        else
            cout<<0<<endl;
    }
    return 0;
}