题解:
比较好的一道题,我们定义三个堆,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);
}