[JSOI2007]建筑抢修

[JSOI2007]建筑抢修

https://ac.nowcoder.com/acm/problem/20154

get到了;可以后悔的贪心;有点像回溯;
类似安排会场问题,使得会场数最少;
因为每个维修时间有限,所以按照截止时间排序;(直观感受就是t2大的尽量排在后面完成)
之后就是在符合要求的条件下选择更小t1进行(这样能尽可能安排);

#include<bits/stdc++.h>
using namespace std;
#define ll long long
priority_queue<ll,vector<ll> > Q;
ll n;
struct nod{
    ll t1,t2;
}node[150010];
bool comp(nod a,nod b)
{
    if(a.t2<b.t2) return true;
    return false;
}
int main()
{
    ll ans=0,cnt=0;
    ll n;
    cin>>n;
    for(int i=1;i<=n;i++) cin>>node[i].t1>>node[i].t2;
    sort(node+1,node+1+n,comp);
    Q.push(node[1].t1);ans+=node[1].t1;
    for(int i=2;i<=n;i++)
    {
       ll m;
       m=Q.top();
       if(ans+node[i].t1<=node[i].t2) Q.push(node[i].t1),ans+=node[i].t1;
        else {
            if(m>node[i].t1) Q.pop(),Q.push(node[i].t1),ans=ans-m+node[i].t1;
        }
    }
    cout<<Q.size();
}
全部评论

相关推荐

点赞 收藏 评论
分享
牛客网
牛客企业服务