标签:blog http io os ar for sp 数据 div
2 2 8 6 1 1 4 5 2 10 6 4 5 6 5
1 2
/* 由于喷水器的喷水半径不一样,且每个喷水器的坐标也不同, 所以直接采用坐标贪心或者采用半径大小贪心都不太合适, 这里采用每个喷水器在草坪边缘的两个交点来排序, 设每个喷水器i都会和草坪的一条边相交于两点lefti和righti, 那么我们对所有节点,对lefti由小到大排序, 如果lefti大小相同,则按照righti由大到小排。 在排序前其实应该判断lefti的最小值是否大于0, 若最小的lefti都大于0,那么肯定不可能把草坪全部润湿, 同时判断最大的righti和草坪的长的关系。 同时把left和right均小于0或者均大于草坪长的喷水器去掉, 因为根本用不上。这样得到的若干喷水器就是合法喷水器的组合。 根据left值从小向大找,其中left小于0且right值对大的那个节点肯定是第一个需要的喷水器, 这样当该喷水器去掉之后,该喷水器的边缘到草坪的右边缘就构成了一个新的问题,再重复上面的步骤即可
*/
#include <iostream>
#include <stdlib.h>
#include <math.h>
#include <algorithm>
using namespace std;
struct node{
    double l;
    double r;
}point[1005];
bool cmp(node a,node b) //排序规则 区间左端点升序 左端点相同则按右端点降序
{
    return a.l<b.l;
    if(a.l==b.l) return a.r>b.r;
}
int main()
{
    int T,n,i,j,count;
    double w,h,x,r,begin,end;
    cin>>T;
    while(T--)
    {
        cin>>n>>w>>h;
        h/=2;
        point[1005]={0};
        for(i=0,j=0;i<n;i++)
        {
            cin>>x>>r;
            double t=r*r-h*h;
            if(t>0)//只选有可能覆盖的点
            {
                point[j].l=x-sqrt(t);  //区间左端点
                point[j++].r=x+sqrt(t);//区间右端点
            }
        }
        sort(point,point+j,cmp);
       /* for(int i=0;i<j;i++)
        {
            cout<<"l="<<point[i].l<<" "<<"r="<<point[i].r<<endl;
        }
        cout<<endl;
        */
        if(point[0].l>0){ cout<<"0"<<endl; continue;} //如果第一个不能覆盖起点则不能覆盖
        else
        {
            begin=end=0;//每次比较的基准值起点begin、终点end
            i=count=0;
            while(end<=w&&i<j&&count<=n)
            {
                while(point[i].l<=begin&&i<j)//找区间左端点在基准点的左侧
                {
                    if(point[i].r>=end){end=point[i].r;} //右基准点每次选大于上一次的
                    i++;
                }
                begin=end; //定义新的基准点为所选择区间的右端点
                ++count; //每次找完符合条件的
            }
            if(end<w||count>n) //是否有未完全覆盖
                cout <<"0"<<endl;
            else
                cout<<count<<endl;
        }
    }
    return 0;
}
标签:blog http io os ar for sp 数据 div
原文地址:http://www.cnblogs.com/xiaoyunoo/p/4025049.html