POJ 1328(贪心)


#include
#include
#include
#include
#include
#include
#include
#include
using namespace std;

struct point
{
    double left, right;
}p[2010], temp;

bool cmp(point a, point b)
{
    return a.left < b.left;
}

int main()
{
    int n;
    double r;
    int kase = 0;
    while (cin >> n >> r && (n || r))
    {
        bool flag = false;
        for (int i = 1; i <= n; i++)
        {
            double a, b;
            cin >> a >> b;
            if (fabs(b) > r)
            {
                flag = true;
            }
            else
            {
                p[i].left = a * 1.0 - sqrt(r * r - b * b);
                p[i].right = a * 1.0 + sqrt(r * r - b * b);
            }
        }
        cout << "Case " << ++kase << ": ";
        if (flag)
        {
            cout << -1 << endl;
        }
        else
        {
            int count = 1;
            sort(p+1, p + n+1,cmp);
            temp = p[1];
            for (int i = 1; i <= n; i++)
            {
                if (p[i].left > temp.right)
                {
                    count++;
                    temp = p[i];
                }
                else if (p[i].right < temp.right)
                {
                    temp = p[i];
                }
            }
            cout << count << endl;
        }
    }
    return 0;
}

你可能感兴趣的:(ACM_POJ,贪心)