优先队列——51Nod逛街

题解:

比较好的一道题,我们定义三个堆,q1,q2,q3,前两个堆维护的要去的商店,q3维护的是不去的商店。当q1中的数量大于k之后我们就可以讨论q2和q3了,如果我们q2中有的时间大于q3的时间那么就可以交换,以便腾出更多的时间来逛更多的商店。

#include <bits/stdc++.h>
#define int long long
#define lowbit(x) x&(-x)
using namespace std;
#define IT (int)
const int N=1e5+10;
int a[N],b[N],c[N],n,t,k;
priority_queue<int> q1,q2;
priority_queue<int,vector<int>,greater<int> >q3;
signed main()
{
    scanf("%d%d%d",&n,&t,&k);
    int res=-1;
    for(int i=1;i<=n;i++) scanf("%d",&a[i]);
    for(int i=1;i<=n;i++) scanf("%d",&b[i]);
    for(int i=1;i<=n;i++) scanf("%d",&c[i]);
    int t1=0,t2=0;
    for(int i=1;i<=n;i++){
        if(c[i]){
            q1.push(b[i]);
            t1+=b[i];
            if(q1.size()>k){
                int tmp=q1.top();
                q1.pop();
                t1-=tmp;
                q2.push(tmp);
                t2+=tmp;
            }
        }else{
            q2.push(b[i]);
            t2+=b[i];
        }
        if(q1.size()<k) continue;
        if(t1+a[i]>t) continue;
        int rest=t-t1-a[i];
        while(!q3.empty()&&q3.top()<q2.top()){
            int tmp=q2.top();
            t2-=tmp;
            t2+=q3.top();
            q2.pop();
            q2.push(q3.top());
            q3.pop();
            q3.push(tmp);
        }
        while(!q2.empty()&&rest<t2){
            int tmp=q2.top();
            q3.push(tmp);
            t2-=tmp;
            q2.pop();
        }
        while(!q3.empty()&&rest>=t2+q3.top()){
            int tmp=q3.top();
            q2.push(tmp);
            t2+=tmp;
            q3.pop();
        }
        if(rest>=t2&&k+IT q2.size()>res){
            res=k+IT q2.size();
        }
    }
    printf("%d\n",res);
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值