Source Code
#include "bits/stdc++.h"
using namespace std;
typedef long long ll;
const int N=2e5+9;

int n,k,a[N],b[N];

int main()
{
#ifndef ONLINE_JUDGE
    freopen("in.txt", "r", stdin);
#endif
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cin>>n>>k;
    for(int i=1;i<=n;i++){
        cin>>a[i];
    }
    for(int i=1;i<=k;i++){
        cin>>b[i];
    }
    sort(a+1,a+1+n);
    sort(b+1,b+1+k);
    int q;
    cin>>q;
    while(q--){
        int x;
        cin>>x;
        int l=1,r=n,ans=-1;
        while(l<=r){
            int mid=(l+r)/2;
            if(a[mid]>=x){
                ans=mid;
                r=mid-1;
            }
            else {
                l=mid+1;
            }
        }
        int res=0;
        if(ans!=-1) res=n-ans+1;
        else  ans=n+1;
        int j=k;
        int votes=k;
        int can=b[j];
        for(int i=ans-1;i>=1;i--){
            int ned=x-a[i];
            if(ned>votes)break;
            res++;
            can--;
            if(can==0){
                if(j-1>=1){
                    b[j-1]-=b[j];
                    can=b[j-1];
                    votes--;
                }
            }
        }
        cout<<res<<' ';
    }
}
Copy
Votes Ahmed_Allawati
GNU G++17
0 ms
396 KB
Wrong Answer