【贪心】【堆】bzoj1029 [JSOI2007]建筑抢修

按完成时限排序,一个个修复。
若当前建筑花费时间+之前花费的总时间不超过时限,则ans++;
否则,从之前已修复的建筑中挑一个耗时最多的,与当前建筑比较,若当前建筑更优,则更新ans。

 1 #include<cstdio>
 2 #include<queue>
 3 #include<algorithm>
 4 using namespace std;
 5 priority_queue<int,vector<int> >Heap;
 6 struct Point{int x,y;};
 7 bool cmp(const Point &a,const Point &b){return a.y<b.y;}
 8 int n,v,used,ans=1;
 9 Point a[150001];
10 int main()
11 {
12     scanf("%d",&n);
13     for(int i=1;i<=n;i++)scanf("%d%d",&a[i].x,&a[i].y);
14     sort(a+1,a+n+1,cmp);
15     used=a[1].x;Heap.push(a[1].x);
16     for(int i=2;i<=n;i++)
17       {
18           if(used+a[i].x<=a[i].y)
19             {
20                 Heap.push(a[i].x);
21                 used+=a[i].x;
22                 ans++;
23                 continue;
24             }
25           v=Heap.top();
26           if(v>a[i].x)
27             {
28                 Heap.pop();
29                 Heap.push(a[i].x);
30                 used+=(a[i].x-v);
31             }
32       }
33     printf("%d\n",ans);
34 }

 

posted @ 2014-09-13 10:46  AutSky_JadeK  阅读(183)  评论(0编辑  收藏  举报
TVアニメ「Charlotte(シャーロット)」公式サイト TVアニメ「Charlotte(シャーロット)」公式サイト