Light OJ 1016 Brush (II)

1016 - Brush (II)
Time Limit: 2 second(s) Memory Limit: 32 MB

After the long contest, Samee returned home and got angry after seeing his room dusty. Who likes to see a dusty room after a brain storming programming contest? After checking a bit he found a brush in his room which has width w. Dusts are defined as 2D points. And since they are scattered everywhere, Samee is a bit confused what to do. So, he attached a rope with the brush such that it can be moved horizontally (in X axis) with the help of the rope but in straight line. He places it anywhere and moves it. For example, the y co-ordinate of the bottom part of the brush is 2 and its width is 3, so the y coordinate of the upper side of the brush will be 5. And if the brush is moved, all dusts whose y co-ordinates are between 2 and 5 (inclusive) will be cleaned. After cleaning all the dusts in that part, Samee places the brush in another place and uses the same procedure. He defined a move as placing the brush in a place and cleaning all the dusts in the horizontal zone of the brush.

You can assume that the rope is sufficiently large. Now Samee wants to clean the room with minimum number of moves. Since he already had a contest, his head is messy. So, help him.

Input

Input starts with an integer T (≤ 15), denoting the number of test cases.

Each case starts with a blank line. The next line contains two integers N (1 ≤ N ≤ 50000) and w (1 ≤ w ≤ 10000), means that there are N dust points. Each of the next N lines will contain two integers: xi yidenoting coordinates of the dusts. You can assume that (-109 ≤ xi, yi ≤ 109) and all points are distinct.

Output

For each case print the case number and the minimum number of moves.

Sample Input

Output for Sample Input

2

 

3 2

0 0

20 2

30 2

 

3 1

0 0

20 2

30 2

Case 1: 1

Case 2: 2

 

 

题目大意:

  这道题是说,现在有一个刷子,这个刷子每次只能横着刷,然后有n个点,这n个点分布在一个二维平面内,问最少刷几次能够让这个平面的n个点都被覆盖。

解题思路:

  这道题,n<=50000,那么O(nlogn)的算法肯定是可以解决的,那么我们首先将这些点按照y方向从小到大排序,然后,我们从第一个点开始,依次找在y[i]+w范围外的

点,如果在范围内,就continue掉。

代码:

 1 # include <cstdio>
 2 # include <iostream>
 3 # include <algorithm>
 4 # define MAX 50004
 5 using namespace std;
 6 int x[MAX],y[MAX];
 7 int main(void){
 8     int icase = 1;
 9     int t; cin>>t;
10     int n,w;
11     while ( t-- ){
12         cin>>n>>w;
13         for ( int i = 0;i < n;i++ ){
14             cin>>x[i]>>y[i];
15         }
16         sort(y,y+n);
17         int cnt = 0;
18         int tmp = INT_MIN;
19         for ( int i = 0;i < n;i++ ){
20             if ( y[i] <= tmp ) continue;
21             tmp = y[i]+w;
22             cnt++;
23         }
24         printf("Case %d: %d\n",icase++,cnt);
25     }
26 
27 
28     return 0;
29 }

 

posted @ 2016-04-06 00:36  西瓜书?蓝皮书?  阅读(220)  评论(0编辑  收藏  举报