POJ1328

Written by    00:17 July 10, 2014 

今天期末考试成绩出来了,在认认真真地看完自己的各科成绩、GPA、排名过后,心里只是有一种说不出来的无奈感,果然,这个学期还是个学渣么,看着镜子里的自己,感觉什么也说不出来,也没什么好说的,只能继续刷POJ,刷完了到点就去健身,然后回来晚饭,晚饭完了继续刷POJ,最后总算是把POJ1328 給AC了。

Radar Installation
Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 66695 Accepted: 14946

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 locating in the sea side. And any radar installation, locating on the coasting, can only cover d distance, so an island in the sea can be covered by a radius installation, if the distance between them is at most d.

We use Cartesian coordinate system, defining the coasting is the x-axis. The sea side is above x-axis, and the land side below. Given the position of each island in the sea, and given the distance of the coverage of the radar installation, your task is to write a program to find the minimal number of radar installations to cover all the islands. Note that the position of an island is represented by its x-y coordinates.


Figure A Sample Input of Radar Installations

Input

The input consists of several test cases. The first line of each case contains two integers n (1<=n<=1000) and d, where n is the number of islands in the sea and d is the distance of coverage of the radar installation. This is followed by n lines each containing two integers representing the coordinate of the position of each island. Then a blank line follows to separate the cases.

The input is terminated by a line containing pair of zeros

Output

For each test case output one line consisting of the test case number followed by the minimal number of radar installations needed. “-1” installation means no solution for that case.

Sample Input

Sample Output

Source

这道题目的关键之处就是把面的问题转换成线的问题,每一个座海岛在x轴上有一个区间,在这个区间里面的雷达都可以侦测到海岛,区间的范围即是[x-sqrt(dd – yy), x+sqrt(dd+yy)],然后先以右端点为基进行从小到大排序,然后把第一个雷达默认放在最左端的xmax,接下来的点只要是xmin小于当前xmax就可以不用增加雷达,如果xmax == xmin的话也不用增加雷达。然后如果xmax < xmin的话就加一个雷达,然后以xmin所属区间的xmax为基进行比较。

其实一开始用链表写了一个一百多行的,结果折腾来折腾去也是各种WA,RE,TLE什么的,然后江老师看了我之前代码里面那么大的一段冒泡排序过后就果断说代码时间复杂度高了,建议试一试用STL里面的sort函数,后来上网七摸八摸再请教了江老师一下,就用STL重新写了个,代码速度提了不少不说,还精简了不少,虽然还没有仔细研究,但是已经感觉到了STL果断神器,必须好好搞一搞了。

代码如下:

附上测试数据一组,来自poj Discuss:

2 5

-3 4

-6 3

4 5

-5 3

-3 5

2 3

3 3

20 8

-20 7

-18 6

-5 8

-21 8

-15 7

-17 5

-1 5

-2 3

-9 6

1 2

2 3

3 4

4 5

5 6

6 7

7 8

8 7

9 6

10 5

0 0

2 3

0 2

2 3

2 3

0 2

1 3

3 3

1 2

-3 2

2 4

8 5

2 4

-4 4

-3 3

-3 1

-3 0

-1 0

0 5

6 0

3 0

1 2

-3 1

2 1

3 2

1 2

-3 1

2 1

1 2

0 2

2 3

0 2

2 3

4 -5

4 3

4 3

2 3

6 -9

3 -3

1 2

-3 2

2 1

6 2

1 2

1 2

1 2

-3 1

2 1

0 0

1 2

0 2

2 3

0 2

1 3

3 10

1 10

2 3

4 5

3 5

1 10

2 3

4 5

4 7

1 10

2 3

4 5

0 0

3 9

1 10

2 3

4 5

0 0

================结果

Case 1: 1

Case 2: 2

Case 3: 4

Case 4: 1

Case 5: 1

Case 6: -1

Case 7: 3

Case 8: -1

Case 9: 2

Case 10: 1

Case 11: 1

Case 12: -1

Case 13: -1

Case 14: 2

Case 15: 1

Case 16: 1

Case 17: 1

Case 18: -1

Case 19: -1

Case 20: -1

挥着翅膀的鳖 献上。。。

d<=0不需要判断

y<=0 不需要判断

把每个岛屿来当做雷达的圆心,半径为d,做圆,与x轴会产生两个焦点L和R,这就是一个区间;

首先就是要把所有的区间找出来,然后x轴从左往右按L排序,再然后就是所谓的贪心把那些互相重叠的区间去掉就行了,

晚上AC了过后,感觉心里面其实也没什么难过不难过的,只能说是稍微有一点郁闷吧,接下来继续high就是了~

Category : acmstudy

Tags :