频道栏目
首页 > 考试 > 其他 > 正文

【POJ 3004】Subway planning(极角排序+贪心)

2016-09-08 09:34:54         来源:ACMer'  
收藏   我要投稿
Subway planning
Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 1384   Accepted: 375

Description

The government in a foreign country is looking into the possibility of establishing a subway system in its capital. Because of practical reasons, they would like each subway line to start at the central station and then go in a straight line in some angle as far as necessary. You have been hired to investigate whether such an approach is feasible. Given the coordinates of important places in the city as well as the maximum distance these places can be from a subway station (possibly the central station, which is already built), your job is to calculate the minimum number of subway lines needed. You may assume that any number of subway stations can be built along a subway line.


Figure 1: The figure above corresponds to the first data set in the example input.


Input

The first line in the input file contains an integer N, the number of data sets to follow. Each set starts with two integers, n and d (1 ≤ n ≤ 500, 0 ≤ d < 150). n is the number of important places in the city that must have a subway station nearby, and d is the maximum distance allowed between an important place and a subway station. Then comes n lines, each line containing two integers x and y (-100 ≤ x, y ≤ 100), the coordinates of an important place in the capital. The central station will always have coordinates 0, 0. All pairs of coordinates within a data set will be distinct (and none will be 0, 0).

Output

For each data set, output a single integer on a line by itself: the minimum number of subway lines needed to make sure all important places in the city is at a distance of at most d from a subway station.

Sample Input

2
7 1
-1 -4
-3 1
-3 -1
2 3
2 4
2 -2
6 -2
4 0
0 4
-12 18
0 27
-34 51

Sample Output

4
2

Hint


Source

Svenskt M?sterskap i Programmering/Norgesmesterskapet 2003

 






不得不说 今天题感很好~~自满一下~~~

切几何都1A了,而且都是之前不太有思路的题。做题做多了经验也就多了啊~

题目大意:
二维坐标平面里n个点,从原点出发可以画任意条线段,要求每个点附近都有至少一条距离d的线段。问最少画几条线段能满足要求。

如果距离原点为d内,也不需要线段。这些点可以先扣去。

其余点可以先找到与原点构成的线段的极角弧度。然后对于该点满足条件的线段,其实就是原点到 以该点为圆心,d为半径的圆的两条切线内部的点。

借张图:

\http://blog.csdn.net/lin375691011/article/details/38852129

其实这张图大家应该看过不知多少遍了=。=

这样把每个点做成一个区间,区间边界为左右切线的弧度值。

然后按照区间最小点覆盖那一套来。

这里有一个处理很妙,原本的二维坐标平面内极角范围是[?PI,PI],但因为成环,这样边界很不好处理,借鉴别人的思路,把点double一下,复制一份范围[?PI+2?PI,PI+2?PI]的,这样就不用管边界了,直接搞就行了。

代码如下:

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include
#include 
#define LL long long
#define Pr pair
#define fread(ch) freopen(ch,"r",stdin)
#define fwrite(ch) freopen(ch,"w",stdout)

using namespace std;
const int INF = 0x3f3f3f3f;
const int msz = 10000;
const int mod = 1e9+7;
const double eps = 1e-8;
const int MAXN = 555;
const double PI = acos(-1);

double dcmp(double x)
{
    return fabs(x) <= eps? 0: x - eps;
}

struct Point
{
    double x,y,l,r;

    Point(){}
    Point(double _x,double _y):x(_x),y(_y){}

    double operator -(const struct Point a)const
    {
        return sqrt((x-a.x)*(x-a.x)+(y-a.y)*(y-a.y));
    }

    bool operator <(const struct Point a)const
    {
        return l < a.l;
    }
};

Point pt[MAXN<<1];

int main()
{
    //fread("");
    //fwrite("");

    int n,t;
    double d;

    scanf("%d",&t);

    while(t--)
    {
        scanf("%d%lf",&n,&d);

        for(int i = 0; i < n; ++i)
        {
            scanf("%lf%lf",&pt[i].x,&pt[i].y);
            if(dcmp(pt[i]-Point(0,0)-d) <= 0) --i,--n;
            else
            {
                double tmp = asin(d/(pt[i]-Point(0,0)));
                pt[i].l = pt[i].r = atan2(pt[i].x,pt[i].y);

                pt[i].l -= tmp;
                pt[i].r += tmp;
            }
        }

        sort(pt,pt+n);

        for(int i = n; i < (n<<1); ++i)
        {
            pt[i] = pt[i-n];
            pt[i].l += PI*2;
            pt[i].r += PI*2;
        }

        //for(int i = 0; i < n; ++i)
        //{
        //  printf("(%f,%f) %f~%f\n",pt[i].x,pt[i].y,pt[i].l,pt[i].r);
        //}

        double r;
        int ans = n;

        for(int i = 0; i < n; ++i)
        {
            int tmp = 1;
            r = pt[i].r;

            for(int j = i; j < i+n; ++j)
            {
                //printf("%f %f %f\n",r,pt[j].l,pt[j].r);
                if(dcmp(r-pt[j].l) >= 0) r = min(r,pt[j].r);
                else
                {
                    tmp++;
                    r = pt[j].r;
                }
            }
            ans = min(ans,tmp);
        }

        printf("%d\n",ans);
    }

    return 0;
}

上一篇:LeetCode笔记:136. Single Number
下一篇:剑指Offer——Trie树(字典树)
相关文章
图文推荐

关于我们 | 联系我们 | 广告服务 | 投资合作 | 版权申明 | 在线帮助 | 网站地图 | 作品发布 | Vip技术培训 | 举报中心

版权所有: 红黑联盟--致力于做实用的IT技术学习网站