[ACM] POJ 3485 Highway (区间选点问题)

本文介绍了一个算法问题,目标是在已知高速公路长度、村庄位置及距离限制的情况下,确定最少的出口数量,确保每个村庄都能在规定距离内到达至少一个出口。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Highway
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 871 Accepted: 402

Description

Bob is a skilled engineer. He must design a highway that crosses a region with few villages. Since this region is quite unpopulated, he wants to minimize the number of exits from the highway. He models the highway as a line segment S(starting from zero), the villages as points on a plane, and the exits as points on S. Considering that the highway and the villages position are known, Bob must find the minimum number of exists such that each village location is at most at the distance D from at least one exit. He knows that all village locations are at most at the distance D from S.

Input

The program input is from a text file. Each data set in the file stands for a particular set of a highway and the positions of the villages. The data set starts with the length L (fits an integer) of the highway. Follows the distance D (fits an integer), the number N of villages, and for each village the location (x,y). The program prints the minimum number of exits.

White spaces can occur freely in the input. The input data are correct and terminate with an end of file.

Output

For each set of data the program prints the result to the standard output from the beginning of a line. An input/output sample is in the table below. There is a single data set. The highway length L is 100, the distance D is 50. There are 3villages having the locations (2, 4)(50, 10)(70, 30). The result for the data set is the minimum number of exits: 1.

Sample Input

100
50
3
2 4
50 10
70 30

Sample Output

1

Source


解题思路:

本题和POJ 1328http://blog.youkuaiyun.com/sr_19930829/article/details/37742711思想是一样的,区间选点。

X轴上公路从0到L,X轴上下有一些点给出坐标代表村庄,问在公路上最少建几个出口才能使每个村庄到出口的距离不超过D。

以每个村庄坐标为圆心,D为半径画圆,与X轴有两个交点,得到一个区间,得到N个区间后,就转化为了区间选点问题。策略为:先按区间的右端点从小到大排序,如果相同则按左端点从大到小排序。

代码:

#include <iostream>
#include <stdio.h>
#include <cmath>
#include <algorithm>
#include <string.h>
using namespace std;
int L,d;

struct I
{
    double l,r;
}inter[10000];

bool cmp(I a ,I b)
{
    if(a.r<b.r)
        return true;
    else if(a.r==b.r)
    {
        if(a.l>b.l)
            return true;
        return false;
    }
    return false;
}


int main()
{
    double x,y;
    while(scanf("%d",&L)!=EOF)
    {
        scanf("%d",&d);
        int n;
        cin>>n;
        for(int i=1;i<=n;i++)
        {
            scanf("%lf%lf",&x,&y);
            inter[i].l=x-sqrt(d*d-y*y);
            inter[i].r=x+sqrt(d*d-y*y);
        }
        sort(inter+1,inter+1+n,cmp);
        //for(int i=1;i<=n;i++)
            //cout<<inter[i].l<<" "<<inter[i].r<<endl;
        int ans=1;
        double temp=inter[1].r;
        for(int i=2;i<=n;i++)
        {
            if(temp>L)
                temp=L;
            if(temp<inter[i].l)
            {
                ans++;
                temp=inter[i].r;
            }
        }
        cout<<ans<<endl;
    }

    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值