ZOJ 3197 - Google Book

  題目大意:也是區間覆蓋問題,是關於書籍查詢的,和UVa 10020差很少,不過比那個簡單,不用判斷是否能覆蓋,直接輸出最小個數就好了。html

 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
相關文章
相關標籤/搜索