POJ 2376

题意:问最少要几个区间才能使区间 [ 1, T ] 全被覆盖掉

 

 1 #include <cstdio>
 2 #include <cstring>
 3 #include <cstdlib>
 4 #include <cctype>
 5 #include <cmath>
 6 #include <time.h>
 7 #include <string>
 8 #include <map>
 9 #include <stack>
10 #include <set>
11 #include <queue>
12 #include <vector>
13 #include <algorithm>
14 #include <iostream>
15 using namespace std;
16 typedef long long ll;
17 #define PI acos( -1.0 )
18 const double E = 1e-8;
19 const int INF = 0x7fffffff;
20 
21 const int NO = 25000 + 5;
22 int n, t;
23 struct ND
24 {
25     int be, en;
26 }st[NO];
27 
28 bool cmp( const ND &a, const ND &b )
29 {
30     if( a.be == b.be )
31         return a.en > b.en;
32     return a.be < b.be;
33 }
34 
35 int main()
36 {
37     scanf( "%d%d", &n, &t );
38     for( int i = 0; i < n; ++i )
39         scanf( "%d%d", &st[i].be, &st[i].en );
40     sort( st, st+n, cmp );
41     int ans = 0;
42     int k = 0;
43     for( int i = 0; i < n; ++i )
44     {
45         if( k+1 >= st[i].be && st[i].en > k )
46         {
47             ++ans;
48             int m = st[i].en, j;
49             if( m >= t )
50             {
51                 printf( "%d\n", ans );
52                 return 0;
53             }
54             for( j = i+1; j < n; ++j )//起始时间在k+1中找最大的终止时间
55                 if( st[j].be <= k+1 )
56                 {
57                     if( m < st[j].en )
58                         m = st[j].en;
59                     if( m >= t ) {
60                         printf( "%d\n", ans );
61                         return 0;
62                     }
63                 } else break;
64             k = m;
65             i = j - 1;
66         }
67         else if( k+1 < st[i].be )
68             break;
69     }
70     puts( "-1" );
71     return 0;
72 }
View Code

 

posted @ 2014-11-14 14:30  A_dan  阅读(112)  评论(0编辑  收藏  举报