zoukankan      html  css  js  c++  java
  • 【贪心】【堆】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
    ",ans);
    34 }
  • 相关阅读:
    FastDfs安装
    groovy学习(二)
    jenkins(一)jenkins使用教程
    jenkins(二)Pipeline语法速成
    groovy学习(一)
    Docker笔记(一)
    运维笔记(一)
    Maven管理(一)
    工厂模式案例与理解
    python 探測端口
  • 原文地址:https://www.cnblogs.com/autsky-jadek/p/3969559.html
Copyright © 2011-2022 走看看