bzoj1029: [JSOI2007]建筑抢修(堆+贪心)

1029: [JSOI2007]建筑抢修

题目:传送门

 

题解:

   一道以前就做过的水题(找个水题签个到嘛...)

   很明显就是一道贪心题,这里我们用一个堆来维护

   具体看代码吧,很容易YY所以不讲

代码:

 1 #include<cstdio>
 2 #include<cstring>
 3 #include<cstdlib>
 4 #include<cmath>
 5 #include<algorithm>
 6 #include<queue>
 7 #include<vector>
 8 using namespace std;
 9 typedef long long LL;
10 struct node
11 {
12     LL t1,t2;
13 }a[151000];
14 priority_queue<int,vector<int>,less<int> > q;
15 bool cmp(node n1,node n2)
16 {
17     if(n1.t2>n2.t2)return false;
18     if(n1.t2<n2.t2)return true;
19     if(n1.t1<n2.t1)return true;
20     if(n1.t1>n2.t1)return false;
21     return true;
22 }
23 LL n;
24 int main()
25 {
26     scanf("%lld",&n);
27     for(int i=1;i<=n;i++)
28         scanf("%lld%lld",&a[i].t1,&a[i].t2);
29     sort(a+1,a+n+1,cmp);
30     LL ans=0,now=0;
31     for(int i=1;i<=n;i++)
32     {
33         if(now+a[i].t1<=a[i].t2)
34         {
35             ans++;
36             now+=a[i].t1;
37             q.push(a[i].t1);
38         }
39         else
40         {
41             LL t=q.top();
42             if(a[i].t1<t)
43             {
44                 q.pop();
45                 now-=t-a[i].t1;
46                 q.push(a[i].t1);
47             }
48         }
49     }
50     printf("%lld\n",ans);
51     return 0;
52 }

 

posted @ 2018-01-10 13:31  CHerish_OI  阅读(193)  评论(0编辑  收藏  举报