码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2014-07-21 11:23:56      阅读:176      评论:0      收藏:0      [点我收藏+]

标签:acm   贪心   

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.csdn.net/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;
}


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

标签:acm   贪心   

原文地址:http://blog.csdn.net/sr_19930829/article/details/38009767

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!