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

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

Southeastern Europe 2007

解题思路:

本题和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;
}
时间: 2024-11-03 21:37:51

[ACM] POJ 3485 Highway (区间选点问题)的相关文章

[ACM] POJ 1328 Radar Installation (贪心,区间选点问题)

Radar Installation Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 51131   Accepted: 11481 Description Assume the coasting is an infinite straight line. Land is in one side of coasting, sea in the other. Each small island is a point loca

[ACM] POJ 1141 Brackets Sequence (区间动态规划)

Brackets Sequence Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 25087   Accepted: 7069   Special Judge Description Let us define a regular brackets sequence in the following way: 1. Empty sequence is a regular sequence. 2. If S is a re

UVa1615 Highway (贪心,区间选点)

链接:http://bak.vjudge.net/problem/UVA-1615 分析:以村庄为圆心,D为半径作圆,可选区间就是高速公路在圆内的部分(包括两个交点),按区间右端点从小到大,如若右端点相同再按左端点从大到小排好序,接下来就是很纯粹的区间选点问题. 1 #include <cstdio> 2 #include <algorithm> 3 #include <cmath> 4 using namespace std; 5 6 const int maxn =

UVa 1615 Highway (贪心,区间选点问题)

题意:给定一个数 n 个点,和一个d,要求在x轴上选出尽量少的点,使得对于给定的每个点,都有一个选出的点离它的欧几里德距离不超过d. 析:首先这是一个贪心的题目,并且是区间选点问题,什么是区间选点呢,就是说在数轴上有 n 个闭区间,取尽量少的点,使得每个区间都至少有一个点. 一看是不是和这个题很相似,是的,那么区间哪里来呢?自己造呗,既然说是距离不超过d,意思就是在给定的点为圆心,以 d 为半径画圆,在x轴上的区间, 那么区间不就有了么,然后这个怎么贪心呢,是这样的,把所有的区间的右端点从小到大

poj 1328 Radar Installation 【贪心】【区间选点问题】

Radar Installation Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 54798   Accepted: 12352 Description Assume the coasting is an infinite straight line. Land is in one side of coasting, sea in the other. Each small island is a point loca

UVA-1615 Highway (贪心,区间选点)

题目大意:有一条沿x轴正方向,长为L的高速公路,n个村庄,要求修建最少的公路出口数目,使得每个村庄到出口的距离不大于D. 题目分析:区间选点问题.在x轴上,到每个村庄距离为D的点有两个(超出范围除外),这两个点便构成了一个区间,这样的区间总共有n个.问题便转化为了,在n个区间中选取最少的点占据所有区间.贪心即可,贪心策略:将所有区间按右端点排序,若需要选择,每次都选区间右端点. 代码如下: # include<iostream> # include<cstdio> # includ

UVA 1615 Highway 高速公路 (区间选点)

题意:在一条线段上选出尽量少的点,使得和所有给出的n个点距离不超过D. 分别计算出每个点在线段的满足条件的区间,然后就转化成了区间选点的问题了,按照右端点排序,相同时按照左端点排序,按照之前的排序一定保证了包含这个点的区间是连续的.贪心,每次选右边的端点,维护一个当前选择点的位置,每遇到区间就判断一下并更新一下点的位置. #include<bits/stdc++.h> using namespace std; const int maxn = 1e5+5; struct seg { doubl

POJ 1328 Radar Installation 【贪心 区间选点】

解题思路:给出n个岛屿,n个岛屿的坐标分别为(a1,b1),(a2,b2)-----(an,bn),雷达的覆盖半径为r 求所有的岛屿都被覆盖所需要的最少的雷达数目. 首先将岛屿坐标进行处理,因为雷达的位置在x轴上,所以我们设雷达的坐标为(x,0),对于任意一个岛屿p(a,b),因为岛屿要满足在雷达的覆盖范围内,所以 (x-a)^2+b^2=r^2,解得 xmin=a-sqrt(r*r-b*b);//即为区间的左端点 xmax=a+sqrt(r*r-b*b);//即为区间的右端点 接下来区间选点即

区间选点问题

Advertisement:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=21426 经典的区间选点问题,要求每个区间内的点数满足条件k个,具体做法是把右端点从小到大排序,然后每次都检查区间内的点数,如果少了,就尽量从右边开始加点,以便于和其他区间共用一点 #include"iostream"#include"algorithm"#include"cstring"using