描述
You, the best hacker in the world, want to download the books published on Google Book. After some investigation, you found
that the address of each page consists of two parts. The first part is the page number, the second part is the signature which is unique for each page. To get the signature, you can send the query to the server. The query has one parameter, which indicates the page number. The server will return the signature of the required page, and it may also return the signature of some adjacent pages.
To minimize the bytes downloaded from the internet, and also make the server adminstrator hard to notice your "hack", you'd like to minimize the number of queries.
Input
The input has multiple cases. The first line of the input is a single integer T which is the number of test cases. Then T consecutive test cases follow.
In each test case, the first line is a number N (1<=N<=5000), indicating the number of pages of the book. Then n lines follows. On the i-th line, there will be two integers ai and bi (ai<=i<=bi). They indicate that the query for the i-th page will return the signatures from page ai to page bi (inclusive).
Output
Results should be directed to standard output. The output of each test case should be a single integer, which is the minimum number of queries to get all the signatures.
Sample Input
2 3 1 1 2 2 3 3 3 1 1 1 3 3 3
Sample Output
3 1
区间覆盖问题
贪心策略:选取的区间能覆盖尽可能长的区域,并且保证所有的1-n的范围都给覆盖到
第1步:1开始的区间,取覆盖范围最长的,设为[1,max]
接下来,每次取能覆盖到max+1的最长覆盖区间[s,e], s<=max+1
对每个区间,以其起始坐标为关键字,从小到大排序;如果起始坐标相等,则按结束坐标从大到小排序
1
#include
<
stdio.h
>
2 #define N 5000
3 /*
4 定义结构体
5 含ai,bi两属性
6 */
7 struct t
8 {
9 int ai;
10 int bi;
11
12 }sqList[N];
13
14 int main()
15 {
16 int input; // 记录输入次数
17 int n; // 记录结构体数组大小
18
19 scanf( " %d " , & input);
20 for ( int c = 0 ;c < input;c ++ )
21 {
22
23 int count = 0 ;
24 int max = 0 ;
25 scanf( " %d " , & n);
26
27 for ( int i = 0 ; i < n ; i ++ )
28 {
29 scanf( " %d%d " , & sqList[i].ai, & sqList[i].bi);
30 }
31
32
33 /*
34 求出以1为首的覆盖的最长范围
35 用max记录下最长范围的值
36 */
37
38 max = sqList[ 0 ].bi;
39 for ( int j = 0 ; j < n ;j ++ )
40 {
41 if (sqList[j].ai == sqList[ 0 ].ai && sqList[j].bi > max)
42 max = sqList[j].bi;
43 }
44 count ++ ;
45
46
47
48
49
50 while (max != n)
51 {
52
53 max = max + 1 ;
54 int index = max;
55 for ( int z = 0 ; z < n; z ++ )
56 {
57 /*
58 找出包含最大范围值max+1
59 且包含的最大范围值,赋值给新的max
60 */
61 if (sqList[z].ai <= index && sqList[z].bi >= index)
62 {
63
64 if (sqList[z].bi >= max)
65 {
66 max = sqList[z].bi;
67 } // ~if
68
69 } // ~if
70
71 } // ~for
72
73
74 } // ~while
75
76 count ++ ; // 记录要计算的次数
77 printf( " %d\n " ,count);
78
79 }
80 return 1 ;
81
82
83 }
84
2 #define N 5000
3 /*
4 定义结构体
5 含ai,bi两属性
6 */
7 struct t
8 {
9 int ai;
10 int bi;
11
12 }sqList[N];
13
14 int main()
15 {
16 int input; // 记录输入次数
17 int n; // 记录结构体数组大小
18
19 scanf( " %d " , & input);
20 for ( int c = 0 ;c < input;c ++ )
21 {
22
23 int count = 0 ;
24 int max = 0 ;
25 scanf( " %d " , & n);
26
27 for ( int i = 0 ; i < n ; i ++ )
28 {
29 scanf( " %d%d " , & sqList[i].ai, & sqList[i].bi);
30 }
31
32
33 /*
34 求出以1为首的覆盖的最长范围
35 用max记录下最长范围的值
36 */
37
38 max = sqList[ 0 ].bi;
39 for ( int j = 0 ; j < n ;j ++ )
40 {
41 if (sqList[j].ai == sqList[ 0 ].ai && sqList[j].bi > max)
42 max = sqList[j].bi;
43 }
44 count ++ ;
45
46
47
48
49
50 while (max != n)
51 {
52
53 max = max + 1 ;
54 int index = max;
55 for ( int z = 0 ; z < n; z ++ )
56 {
57 /*
58 找出包含最大范围值max+1
59 且包含的最大范围值,赋值给新的max
60 */
61 if (sqList[z].ai <= index && sqList[z].bi >= index)
62 {
63
64 if (sqList[z].bi >= max)
65 {
66 max = sqList[z].bi;
67 } // ~if
68
69 } // ~if
70
71 } // ~for
72
73
74 } // ~while
75
76 count ++ ; // 记录要计算的次数
77 printf( " %d\n " ,count);
78
79 }
80 return 1 ;
81
82
83 }
84
此算法并不是最优解法,其中的空间浪费与时间浪费严重,不过优点是目标明确。