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 }
  • 相关阅读:
    hdu 5171(矩阵快速幂,递推)
    hdu 1316(大整数)
    hdu 5170(数学)
    hdu 5167(dfs)
    hdu 5166(水题)
    hdu 5720(贪心+区间合并)
    BestCoder 2nd Anniversary的前两题
    hdu 3065(AC自动机)
    2.3绘制构造线与射线
    查找ARP攻击源
  • 原文地址:https://www.cnblogs.com/autsky-jadek/p/3969559.html
Copyright © 2011-2022 走看看