D. Cleaning the Phone(枚举+前缀和+二分)

62 篇文章 2 订阅
40 篇文章 0 订阅

https://codeforces.com/contest/1475/problem/D


经典两个物品状态的总最优题:

雨雨的类似题:https://blog.csdn.net/tlyzxc/article/details/112465594

#include<iostream>
#include<vector>
#include<queue>
#include<cstring>
#include<cmath>
#include<map>
#include<set>
#include<cstdio>
#include<algorithm>
#define debug(a) cout<<#a<<"="<<a<<endl;
using namespace std;
const int maxn=2e5+1000;
typedef long long LL;
LL a[maxn],b1[maxn],b2[maxn],tag[maxn];
LL n,m;
void init(){
    for(LL i=0;i<n+10;i++) a[i]=b1[i]=b2[i]=tag[i]=0;
}
bool cmp(LL A,LL B){
    return A>B;
}
int main(void)
{
  cin.tie(0);std::ios::sync_with_stdio(false);
  LL t;cin>>t;
  while(t--){
    cin>>n>>m;
    init();
    LL tot1=0;LL tot2=0;LL flag=0;
    for(LL i=1;i<=n;i++) cin>>a[i],flag+=a[i];
    for(LL i=1;i<=n;i++) cin>>tag[i];
    if(flag<m){
        cout<<"-1"<<endl;continue;
    }
    for(LL i=1;i<=n;i++){
        if(tag[i]==1){
            b1[++tot1]=a[i];
        }
        else if(tag[i]==2){
            b2[++tot2]=a[i];
        }
    }
    sort(b1+1,b1+tot1+1,cmp);sort(b2+1,b2+tot2+1,cmp);
    for(LL i=1;i<=tot1;i++) b1[i]+=b1[i-1];
    for(LL i=1;i<=tot2;i++) b2[i]+=b2[i-1];
    LL ans=1e18;
    for(LL i=0;i<=tot1;i++){
        LL cnt=m-b1[i];
        LL pos=lower_bound(b2+1,b2+1+tot2,cnt)-b2;///可能1就够了
        if(b1[i]+b2[pos]>=m){
            ans=min(ans,i*1+pos*2);
        }
        pos--;
        if(b1[i]+b2[pos]>=m) ans=min(ans,i*1+pos*2);
    }
    cout<<ans<<endl;
  }
return 0;
}

 

  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值