zoukankan      html  css  js  c++  java
  • Bzoj1029 [JSOI2007]建筑抢修

    Time Limit: 4 Sec  Memory Limit: 162 MB
    Submit: 4452  Solved: 2006

    Description

      小刚在玩JSOI提供的一个称之为“建筑抢修”的电脑游戏:经过了一场激烈的战斗,T部落消灭了所有z部落的
    入侵者。但是T部落的基地里已经有N个建筑设施受到了严重的损伤,如果不尽快修复的话,这些建筑设施将会完全
    毁坏。现在的情况是:T部落基地里只有一个修理工人,虽然他能瞬间到达任何一个建筑,但是修复每个建筑都需
    要一定的时间。同时,修理工人修理完一个建筑才能修理下一个建筑,不能同时修理多个建筑。如果某个建筑在一
    段时间之内没有完全修理完毕,这个建筑就报废了。你的任务是帮小刚合理的制订一个修理顺序,以抢修尽可能多
    的建筑。

    Input

      第一行是一个整数N接下来N行每行两个整数T1,T2描述一个建筑:修理这个建筑需要T1秒,如果在T2秒之内还
    没有修理完成,这个建筑就报废了。

    Output

      输出一个整数S,表示最多可以抢修S个建筑.N < 150,000;  T1 < T2 < maxlongint

    Sample Input

    4
    100 200
    200 1300
    1000 1250
    2000 3200

    Sample Output

    3

    HINT

     

    Source

    贪心

    凭感觉盲狙了两个贪心方案:

    1、按结束时间排序,能修就修。不能修的时候,如果不修之前耗时最多的那个就能让这个被修好,就用这个替换耗时最多的建筑。

    WA

    2、发现并不需要当前这个一定被修好,只要用这个替换之前耗时最多的,能让累计耗时减少,就替换。

    AC

     1 /*by SilverN*/
     2 #include<algorithm>
     3 #include<iostream>
     4 #include<cstring>
     5 #include<cstdio>
     6 #include<cmath>
     7 #include<queue>
     8 #include<vector>
     9 #define LL long long
    10 using namespace std;
    11 const int mxn=200010;
    12 int read(){
    13     int x=0,f=1;char ch=getchar();
    14     while(ch<'0' || ch>'9'){if(ch=='-')f=-1;ch=getchar();}
    15     while(ch>='0' && ch<='9'){x=x*10+ch-'0';ch=getchar();}
    16     return x*f;
    17 }
    18 struct task{
    19     int t1,t2;
    20 }a[mxn];
    21 int cmp(const task a,const task b){
    22     return a.t2<b.t2;
    23 }
    24 priority_queue<int>q;
    25 int n;
    26 int ans=0;
    27 int main(){
    28     int i,j;
    29     n=read();
    30     for(i=1;i<=n;i++){
    31         a[i].t1=read();a[i].t2=read();
    32     }
    33     sort(a+1,a+n+1,cmp);
    34     int now=0;
    35     for(i=1;i<=n;i++){
    36         if(a[i].t1+now<=a[i].t2){
    37             now+=a[i].t1;
    38             q.push(a[i].t1);
    39             ++ans;
    40         }
    41         else if(!q.empty()){
    42             int tmp=q.top();
    43             if(tmp>a[i].t1){
    44                 now=now-tmp+a[i].t1;
    45                 q.pop();
    46                 q.push(a[i].t1);
    47             }
    48         }
    49     }
    50     printf("%d
    ",ans);
    51     return 0;
    52 }
  • 相关阅读:
    TCP/IP协议,,OSI的七层参考模型,HTTP请求响应机制
    火狐浏览器缓存导致JS已经改变的ID没改变
    Server.MapPath 出现未将对象引用设置到对象的实例
    线程池发送邮件的方法(向多个用户发送同一邮件)
    用多线程发送邮箱(一次给一个用户发送N封邮件)
    验证文本框输入内容类型是汉字
    名人博客
    tinyget
    内存分析
    Silverlight Forums
  • 原文地址:https://www.cnblogs.com/SilverNebula/p/6639887.html
Copyright © 2011-2022 走看看