zoukankan      html  css  js  c++  java
  • 【BZOJ】【1029】【JSOI2007】建筑抢修

    贪心

      按T2(完成时限)排序,然后从前往后依次枚举

        如果sum+a[i].t1<=a[i].t2则加入

        如果来不及修这个建筑:

          如果当前这个建筑的维修时间t1比之前修过的建筑中耗时最长的耗时短,则替换一下

          否则跳过- -

        以上过程用一个堆(priority_queue)维护即可

    SB了一下:pq默认是大根堆……sigh

     1 /**************************************************************
     2     Problem: 1029
     3     User: Tunix
     4     Language: C++
     5     Result: Accepted
     6     Time:284 ms
     7     Memory:2836 kb
     8 ****************************************************************/
     9  
    10 //BZOJ 1029
    11 #include<cstdio>
    12 #include<cstdlib>
    13 #include<cstring>
    14 #include<iostream>
    15 #include<algorithm>
    16 #include<queue>
    17 #define rep(i,n) for(int i=0;i<n;++i)
    18 #define F(i,j,n) for(int i=j;i<=n;++i)
    19 #define D(i,j,n) for(int i=j;i>=n;--i)
    20 #define fi first
    21 #define se second
    22 using namespace std;
    23  
    24 int getint(){
    25     int v=0,sign=1; char ch=getchar();
    26     while(ch<'0'||ch>'9') {if (ch=='-') sign=-1; ch=getchar();}
    27     while(ch>='0'&&ch<='9') {v=v*10+ch-'0'; ch=getchar();}
    28     return v*=sign;
    29 }
    30 /*******************tamplate********************/
    31 const int N=150010;
    32 typedef pair<int,int> pii;
    33 pii a[N];
    34 priority_queue<int> Q;
    35 int main(){
    36 //  freopen("input.txt","r",stdin);
    37     int n=getint();
    38     F(i,1,n){
    39         a[i].se=getint(); a[i].fi=getint();
    40     }
    41     sort(a+1,a+1+n);
    42     int _sum=0,ans=0;
    43     F(i,1,n){
    44         if (_sum+a[i].se<=a[i].fi){
    45             _sum+=a[i].se;
    46             Q.push(a[i].se);
    47             ans++;
    48         }
    49         else if (Q.top()>a[i].se){
    50             _sum=_sum-Q.top()+a[i].se;
    51             Q.pop();
    52             Q.push(a[i].se);
    53         }
    54     }      
    55     printf("%d
    ",ans);
    56     return 0;
    57 }
    View Code
  • 相关阅读:
    poj 2676 Suduku (dfs)
    poj 1562 Oil Deposits (dfs)
    poj 2907 Collecting Beepers (dfs)
    poj 1655 Balancing Act (树形dfs)
    poj 3411 Paid Roads (dfs)
    hdu 2896 病毒侵袭 (AC)
    hdu 3065 病毒侵袭持续中 (AC)
    poj 2251 Dungeon Master (bfs)
    java中debug使用
    Swing入门级小项目总结
  • 原文地址:https://www.cnblogs.com/Tunix/p/4297662.html
Copyright © 2011-2022 走看看