uva-10382-贪心

  题意:对于长为L,宽为W的矩形草地,需要对它进行浇水,总共有n个水龙头,给每个水龙头的浇水半径,和位置.求覆盖整个草地需要的最小水龙头数量.

如图,把浇水的面积转换成矩形,然后就和区间覆盖一样了,直接贪心

WA了很多次, w/2 >= r这里,w一直用整数,一直wa

#include"pch.h"
#include <string>
#include<iostream>
#include<map>
#include<memory.h>
#include<vector>
#include<algorithm>
#include<queue>
#include<vector>
#include<stack>
#include<math.h>
#include<iomanip>
#include<bitset>
#include"math.h"
namespace cc
{
    using std::cout;
    using std::endl;
    using std::cin;
    using std::map;
    using std::vector;
    using std::string;
    using std::sort;
    using std::priority_queue;
    using std::greater;
    using std::vector;
    using std::swap;
    using std::stack;
    using std::bitset;



    class Input
    {
    public:
        double s = 0;
        double e = 0;
    };

    constexpr int N = 10010;

    int n, l, w;
    Input ins[N];

    int min = 0;



    bool cmp(const Input& in1, const Input& in2)
    {
        return in1.s <  in2.s;
    }

    int flag = 0;
    void search()
    {
        double curL = 0;
        double curR = 0;
        for (int i = 0;i < n; )
        {
            int j = i;
            while (j < n && ins[j].s <= curL)
            {
                if (ins[j].e >= curR)
                    curR = ins[j].e;
                ++j;
            }
            if (i == j)
                break;
            i = j;
            curL = curR;
            ++min;
            if (curL >= l)
            {
                flag = 1;
                break;
            }

        }

    }

    void solve()
    {
        while (cin >> n >> l >> w)
        {
            double p, r;
            min = 0;

            flag = 0;
            int total = 0;
            int isok = 0;
            for (int i = 0;i < n;i++)
            {
                cin >> p >> r;
                if (w*1.0 / 2 >= r)
                    continue;
                double x = sqrt((r*r - (w*w*0.25)));
                Input in;
                in.s = p - x;
                in.e = p + x;
                if (in.s < 0)
                    in.s = 0;
                ins[total++] = in;
                if (in.e >= l)
                    isok = 1;
            }
            if (!isok)
            {
                cout << "-1" << endl;
                continue;
            }

            n = total;
            sort(ins, ins + n, cmp);
            search();
            if (flag == 0)
                cout << -1 << endl;
            else
                cout << min << endl;
        }
    }

};


int main()
{

#ifndef ONLINE_JUDGE
    freopen("d://1.text", "r", stdin);
#endif // !ONLINE_JUDGE
    cc::solve();

    return 0;
}
原文地址:https://www.cnblogs.com/shuiyonglewodezzzzz/p/10289670.html