POJ 1382 Cover The Island

#include <stdio.h>
#include <math.h>

#define MAX 1001

int islands;
int radius;
int x[MAX];
int y[MAX];
double left[MAX];
double right[MAX];
int radars;
double radarX;

int partition(int left, int right){
    int pivotValueX = x[left];
    int pivotValueY = y[left];
    while (left < right){
        while (left < right && x[right] >= pivotValueX){
            right--;
        }
        x[left] = x[right];
        y[left] = y[right];
        while (left < right && x[left] <= pivotValueX){
            left++;
        }
        x[right] = x[left];
        y[right] = y[left];
    }
    x[left] = pivotValueX;
    y[left] = pivotValueY;
    return left;
}
void quickSort(int left, int right){
    if (left == right){
        return;
    }
    int pivot = partition(left, right);
    partition(left, pivot - 1);
    partition(pivot + 1, right);
}

int main()
{
    int caseNum = 0;
    while (scanf("%d %d", &islands, &radius)!= EOF){
        if (islands == 0 && radius == 0){
            return 0;
        }
        int noSolution = 0;
        int island;
        for (island = 0; island < islands; island++){
            scanf("%d %d", &x[island], &y[island]);
            if (y[island] > radius){
                noSolution = 1;
            }
        }
        if (noSolution == 1 || radius <= 0){
            printf("Case %d: -1\n", ++caseNum);
            continue;
        }

        int round;
        for (round = 1; round < islands; round++){
            int island;
            for (island = 0; island < islands - round; island++){
                if (x[island] > x[island + 1]){
                    int temp = x[island];
                    x[island] = x[island + 1];
                    x[island + 1] = temp;
                    temp = y[island];
                    y[island] = y[island + 1];
                    y[island + 1] = temp;
                }
            }
        }

        for (island = 0; island < islands; island++){
            double temp = sqrt((double)(radius * radius - y[island] * y[island]));
            left[island] = x[island] - temp;
            right[island] = x[island] + temp;
        }


        radars = 1;
        radarX = right[0];
        for (island = 1; island < islands; island++){
            if (left[island] > radarX){
                radars++;
                radarX = right[island];
            } else if (right[island] < radarX){
                radarX = right[island];
            }
        }

        printf("Case %d: %d\n", ++caseNum, radars);

    }
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值