• 朝鲜战争:轰炸大小和岛


    试题描述

       1951年11月,抗美援朝,保家卫国的志愿军为了抵挡美军疯狂的进攻,决定轰炸大小和岛。支援军训练有素命中率达到90%!美军大小和岛上的设施被损毁。现在由于大多数美军都去还击志愿军了,只剩下一个修理工了,这个修理工虽然开着汽车,能够瞬时到达,但修理还是需要时间的,如果不及时维修,装备就会报废。且装备只能一个个维修。请问按怎样的修理顺序才能保住更多的装备?

    输入
    第一行:是一个整数N
    接下来N行:每行两个整数T1,T2
    修理这个建筑需要T1分钟,如果在T2分钟之内还没有
    修理完成,这个建筑就报废了。
    输出
    输出一个整数S,表示最多可以抢修S个建筑。 
    输入示例
    4
    100 200
    200 1300
    1000 1250
    2000 3200
    输出示例
    3
    其他说明
    数据范围: N<15000

    C程序:

    #include <iostream>
    #include <cstring>
    #include <queue>
    #include <cstdio>
    #include <algorithm>
    
    using namespace std;
    
    const int N=150006;
    
    struct Big_Heap {
        int A[N<<1|1], tot;
    
        void init(int n) {
            for(int i=1; i<=(n<<1|1); i++) A[i]=0;
            tot=0;
        }
    
        void Insert(int val) {
            A[++tot]=val;
            for(int x=tot; x>1 && A[x]>A[x>>1]; x>>=1) swap(A[x], A[x>>1]);
        }
    
        void Update(int val) {
            A[1]=val;
            for(int i=1, j=2; j<=tot; i=j, j<<=1) {
                if((j|1)<=tot && A[j]<A[j|1]) j|=1;
                if(A[j]<A[i]) break;
                swap(A[i], A[j]);
            }
        }
    } heap;
    
    struct data {
        int t1, t2;
        bool operator < (const data &T) const {
            return t2<T.t2;
        }
    } A[N];
    int n, ans, cur;
    
    int main() {
        while(scanf("%d", &n)!=EOF) {
            for(int i=0; i<n; i++) scanf("%d%d", &A[i].t1, &A[i].t2);
            sort(A, A+n);
            cur=ans=0, heap.init(n);
            for(int i=0; i<n; i++) {
                if(A[i].t1+cur<=A[i].t2) ans++, heap.Insert(A[i].t1), cur+=A[i].t1;
                else {
                    if(!heap.tot) continue;
                    int val=heap.A[1];
                    if(val<=A[i].t1) continue;
                    cur-=val-A[i].t1;
                    heap.Update(A[i].t1);
                }
            }
            printf("%d
    ", ans);
        }
        return 0;
    }
  • 相关阅读:
    Oracle死锁只会回滚跟死锁有关的那条SQL,而不会回滚整个事务
    Mysql安装过程(linux:2.6.18-194.el5,Mysql:)
    格式化分区,报/dev/sdb1 is apparently in use by the system; will not make a filesystem here!
    安装Oracle 11gR2,报错:[INS-06101] IP address of localhost could not be determined
    mysql-5.5.25-winx64在win7 x64 免安装配置
    insert遭遇阻塞
    linux中轻松使用backspace和上下按键
    排序
    MySQL
    基于Boost的同步TCP通信
  • 原文地址:https://www.cnblogs.com/WHYFRANK/p/4720054.html
Copyright © 2020-2023  润新知