BZOJ 1029 [JSOI2007]建筑抢修

按照t2从小到大排列之后贪心。

若当前任务可以插入,则插入。

若当前任务不可以插入,分两种情况:

①当前任务的耗时大于等于之前插入的任务的最大耗时:跳过当前任务

②当前任务的耗时小于之前插入的任务的耗时:将最前插入的耗时最大的那个任务删除,插入当前任务

此过程用堆维护~

 

View Code
 1 #include <iostream>
 2 #include <cstring>
 3 #include <cstdlib>
 4 #include <cstdio>
 5 #include <algorithm>
 6 
 7 #define N 1000000
 8 #define INF 1LL<<60
 9 
10 using namespace std;
11 
12 struct HP
13 {
14     long long t1,t2;
15 }hp[N],px[N];
16 
17 int n,sz;
18 long long fin;
19 
20 inline bool cmppx(const HP &a,const HP &b)
21 {
22     if(a.t2!=b.t2) return a.t2<b.t2;
23     return a.t1<b.t1;
24 }
25 
26 inline bool cmphp(const HP &a,const HP &b)
27 {
28     return a.t1<b.t1;
29 }
30 
31 inline void read()
32 {
33     scanf("%d",&n);
34     for(int i=1;i<=n;i++) scanf("%lld%lld",&px[i].t1,&px[i].t2);
35     sort(px+1,px+1+n,cmppx);
36 }
37 
38 inline void go()
39 {
40     for(int i=1;i<=n;i++)
41     {
42         if(px[i].t1+fin<=px[i].t2)
43         {
44             hp[++sz]=px[i];
45             push_heap(hp+1,hp+1+sz,cmphp);
46             fin+=px[i].t1;
47         }
48         else if(sz!=0)
49         {
50             HP sta=hp[1];
51             if(px[i].t1>=sta.t1) continue;
52             pop_heap(hp+1,hp+1+sz,cmphp);
53             hp[sz]=px[i];
54             push_heap(hp+1,hp+1+sz,cmphp);
55             fin-=sta.t1-px[i].t1;
56         }
57     }
58     printf("%d\n",sz);
59 }
60 
61 int main()
62 {
63     read();
64     go();
65     return 0;
66 } 

 

 

posted @ 2013-01-18 22:44  proverbs  阅读(1347)  评论(0编辑  收藏  举报