喷水装置(二) nyist12

来源:互联网 发布:问剑传说手游进阶数据 编辑:程序博客网 时间:2024/05/22 01:49

喷水装置(二)

时间限制:3000 ms  |  内存限制:65535 KB
难度:4
描述
有一块草坪,横向长w,纵向长为h,在它的橫向中心线上不同位置处装有n(n<=10000)个点状的喷水装置,每个喷水装置i喷水的效果是让以它为中心半径为Ri的圆都被润湿。请在给出的喷水装置中选择尽量少的喷水装置,把整个草坪全部润湿。
输入
第一行输入一个正整数N表示共有n次测试数据。
每一组测试数据的第一行有三个整数n,w,h,n表示共有n个喷水装置,w表示草坪的横向长度,h表示草坪的纵向长度。
随后的n行,都有两个整数xi和ri,xi表示第i个喷水装置的的横坐标(最左边为0),ri表示该喷水装置能覆盖的圆的半径。
输出
每组测试数据输出一个正整数,表示共需要多少个喷水装置,每个输出单独占一行。
如果不存在一种能够把整个草坪湿润的方案,请输出0。
样例输入
22 8 61 14 52 10 64 56 5
样例输出
12


思路:

贪心,先求出范围,在范围中找右可达最远的。

#include <iostream>#include <cstring>#include <cmath>#include <algorithm>#define N 10010using namespace std;struct Node{    double s,e;}node[N];double w, h;int n;bool cmp(Node a, Node b){    if (a.s == b.s)    {        return a.e <= b.e;    }    return a.s < b.s;}int work(int n){    int ans = 0;    Node p, mx;    p = mx = node[0];    if(mx.s > 0)    {        return 0;    }    ans = 1;    for(int i = 1; i < n;)    {        if(mx.e == w)        {            return ans;        }        for(; i < n && node[i].s <= p.e; i++)        {            if(node[i].e > mx.e)            {                mx = node[i];            }        }        if(p.e==mx.e) return 0;        if(p.s==mx.s&&p.e<mx.e)        {            p=mx;continue;        }        p=mx;        ans++;    }    return ans;}void input(){    int t;    double x, r;    cin >> t;    while(t--)    {        cin >> n >> w >> h;        int k = 0;        for(int i = 0; i < n; i++)        {            cin >> x >> r;            if(r <= h / 2.0)            {                continue;            }            double len = sqrt(r * r - h * h / 4.0);            node[k].s = x - len;            node[k].e = x + len;            if(node[k].s < 0)            {                node[k].s = 0;            }            if(node[k].e > w)            {                node[k].e = w;            }            k++;        }        int ans = 0;        if(k > 0)        {            sort(node, node + k, cmp);            ans = work(n);        }        cout << ans << endl;    }}int main(){    std::ios::sync_with_stdio(false);    input();    return 0;}


原创粉丝点击