zoukankan      html  css  js  c++  java
  • 建筑抢修(优先队列的运用)

     题目链接:https://vjudge.net/problem/HYSBZ-1029

    题目分析:

    刚开始看题目,我以为建立一个包含t1,t2的结构体,然后将所有建筑维修的摧毁时间进行排序,最后从小到大的将维修时间t1进行相加就可以了,结果WA;

    后来发现如果前面修缮好的建筑的时间过长就有可能会耽误后面的建筑维修数量;所以就需要用到优先队列,在满足在房子摧毁以前能够完全修缮的房子的维修时间放入队列,否则在当前建筑的维修时间小于优先队列中的队首元素(修缮好的建筑中修缮用时最长的)时,将队首元素换成当前的元素修缮时间;

    AC代码:

    #include<iostream>
    #include<queue>
    #include<algorithm>
    using namespace std;
    struct Building
    {
        int t1;
        int t2;
    }b[150005];
    bool cmp(Building a,Building b)     //将t2进行排序
    {
        return a.t2<b.t2;
    }
    int main()
    {
        int n;
        priority_queue <int>s;
        cin>>n;
        for (int i=0;i<n;i++)
            cin>>b[i].t1>>b[i].t2;
            sort (b,b+n,cmp);
          int num=0,total_time=0;
        for (int i=0;i<n ;i++)
        {
            if (b[i].t1<=b[i].t2-total_time)         //判断剩下的时间收否足够修缮目前的这栋房子
            {
                s.push(b[i].t1);
                num++;
                total_time+=b[i].t1;
            }
            else if (s.top()>b[i].t1)
            {
                total_time-=(s.top()-b[i].t1);     
                s.pop();
                s.push(b[i].t1);
            }
        }
        cout << num << endl;
        return 0;
    }
    
     
  • 相关阅读:
    spring_three
    报错:java.sql.SQLException: The server
    Spring_two
    Spring_One
    Mybatis中的collection和association一关系
    Mybatis_three
    文件操作1
    面向对象编程三大特征7
    面向对象编程三大特征6
    面向对象编程三大特征5
  • 原文地址:https://www.cnblogs.com/lisijie/p/7221731.html
Copyright © 2011-2022 走看看