bzoj 1029 贪心

 

贪心的一种,维护一种尽可能优的状态(即不会比最优解差),将这种状态保持到最后。

 

 1 /**************************************************************
 2     Problem: 1029
 3     User: idy002
 4     Language: C++
 5     Result: Accepted
 6     Time:400 ms
 7     Memory:2368 kb
 8 ****************************************************************/
 9  
10 #include <cstdio>
11 #include <queue>
12 #include <algorithm>
13 #define maxn 150010
14 using namespace std;
15  
16 int n, cnt, ctm;
17 priority_queue<int> heap;
18  
19 struct Pair {
20     int t1, t2;
21     bool operator<( const Pair & b ) const {
22         return t2<b.t2;
23     }
24 };
25 Pair jobs[maxn];
26  
27 int main() {
28     scanf( "%d", &n );
29     for( int i=1; i<=n; i++ ) 
30         scanf( "%d%d", &jobs[i].t1, &jobs[i].t2 );
31     sort( jobs+1, jobs+1+n );
32     cnt = ctm = 0;
33     for( int i=1; i<=n; i++ ) {
34         if( jobs[i].t1+ctm <= jobs[i].t2 ) {
35             heap.push( jobs[i].t1 );
36             cnt++;
37             ctm += jobs[i].t1;
38         } else {
39             if( jobs[i].t1 < heap.top() ) {
40                 cnt--;
41                 ctm -= heap.top();
42                 heap.pop();
43                 heap.push( jobs[i].t1 );
44                 cnt++;
45                 ctm += jobs[i].t1;
46             }
47         }
48     }
49     printf( "%d\n", cnt );
50 }
View Code

 

posted @ 2015-02-25 19:44  idy002  阅读(184)  评论(0编辑  收藏  举报