[JSOI2007]建筑抢修

Description

  小刚在玩JSOI提供的一个称之为“建筑抢修”的电脑游戏:经过了一场激烈的战斗,T部落消灭了所有z部落的
入侵者。但是T部落的基地里已经有N个建筑设施受到了严重的损伤,如果不尽快修复的话,这些建筑设施将会完全
毁坏。现在的情况是:T部落基地里只有一个修理工人,虽然他能瞬间到达任何一个建筑,但是修复每个建筑都需
要一定的时间。同时,修理工人修理完一个建筑才能修理下一个建筑,不能同时修理多个建筑。如果某个建筑在一
段时间之内没有完全修理完毕,这个建筑就报废了。你的任务是帮小刚合理的制订一个修理顺序,以抢修尽可能多
的建筑。

Input

  第一行是一个整数N接下来N行每行两个整数T1,T2描述一个建筑:修理这个建筑需要T1秒,如果在T2秒之内还
没有修理完成,这个建筑就报废了。

Output

  输出一个整数S,表示最多可以抢修S个建筑.N < 150,000;  T1 < T2 < maxlongint

Sample Input

4
100 200
200 1300
1000 1250
2000 3200

Sample Output

3
先按t2排序
堆中维护已抢修的建筑的抢修时间
如果出现当前时间+a[i].t1>a[i].t2时,我们从堆中取出最大的时间,如果大于当前时间
那么替换掉显然可以且满足时间更早
如果可以直接抢修,那么答案+1
 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cstring>
 4 #include<algorithm>
 5 #include<cmath>
 6 #include<queue>
 7 using namespace std;
 8 struct Node
 9 {
10   int t1,t2;
11 }a[150001];
12 int n,ans,now;
13 priority_queue<int>q;
14 bool cmp(Node a,Node b)
15 {
16   return a.t2<b.t2;
17 }
18 int main()
19 {int i;
20   cin>>n;
21   for (i=1;i<=n;i++)
22     {
23       scanf("%d%d",&a[i].t1,&a[i].t2);
24     }
25   sort(a+1,a+n+1,cmp);
26   now=0;
27   for (i=1;i<=n;i++)
28     {
29       if (now+a[i].t1<=a[i].t2)
30     {
31       ans++;
32       q.push(a[i].t1);
33       now+=a[i].t1;
34     }
35       else if (q.top()>a[i].t1)
36     {
37       now=now+a[i].t1-q.top();
38       q.pop();
39       q.push(a[i].t1);
40     }
41     }
42   cout<<ans;
43 }

 

posted @ 2018-02-25 22:54  Z-Y-Y-S  阅读(209)  评论(0编辑  收藏  举报