ZOJ 3197 - Google Book

  题目大意:也是区间覆盖问题,是关于书籍查询的,和UVa 10020差不多,不过比那个简单,不用判断是否能覆盖,直接输出最小个数就行了。

 1 #include <cstdio>
 2 #include <algorithm>
 3 #include <climits>
 4 using namespace std;
 5 #define MAXN 5000+10
 6 
 7 struct Interval
 8 {
 9     int l, r;
10 };
11 
12 Interval interval[MAXN];
13 
14 bool cmp(const Interval a, const Interval b)
15 {
16     if (a.l != b.l)   return a.l < b.l;
17     return a.r < b.r;
18 }
19 
20 int main()
21 {
22 #ifdef LOCAL
23     freopen("in", "r", stdin);
24 #endif
25     int N;
26     scanf("%d", &N);
27     while (N--)
28     {
29         int n;
30         scanf("%d", &n);
31         for (int i = 0; i < n; i++)
32             scanf("%d%d", &interval[i].l, &interval[i].r);
33         sort(interval, interval+n, cmp);
34         int start, end = 0;
35         int ans = 0;
36         int p = 0;  // p points to the interval which is being considered
37         // find the maximum interval starting with the same start
38         while (end < n)
39         {
40             start = end + 1;
41             int upper = INT_MIN;
42             while (p < n && interval[p].l <= start)
43             {
44                 upper = max(upper, interval[p].r);
45                 p++;
46             }
47             ans++;
48             end = upper;
49         }
50         printf("%d\n", ans);
51     }
52     return 0;
53 }
View Code

 

posted @ 2013-07-24 16:05  xiaobaibuhei  阅读(166)  评论(0编辑  收藏  举报