1 条题解

  • 0
    @ 2025-3-18 7:51:57
    #include<bits/stdc++.h>
    typedef long long ll, LL;
    using namespace std;
    #define pb push_back
    
    vector<ll> a;
    
    int ans(ll m, int n) {
    	int ans = -1, l = 0, mid;
    	n--;
    	while(l <= n) {
    		mid = l + ((n-l)>>1);
    		if(m >= a[mid]) {
    			l = mid+1;
    			ans = mid;
    		}
    		else {
    			n = mid-1;
    		}
    	}
    	return ans+1;
    }
    
    void solve() {
    	int n, q;
    	cin >> n >> q;
    	int a1;
    	for(int i = 0; i < n; i++) {
    		cin >> a1;
    		a.pb(a1);
    	}
    	sort(a.begin(), a.end());
    	for(int i = 1; i < n; i++) {
    		a[i] += a[i-1];
    	}
    	ll m;
    	for(int i = 0; i < q; i++) {
    		cin >> m;
    		cout << ans(m, n) << endl;
    	}
    }
    
    int main() {
    	ios::sync_with_stdio(false);
    	cin.tie(0);
    	cout.tie(0);
    	
    	int t = 1;
    //	cin >> t;
        while(t--) {
        	solve();
    	}
    	
    	return 0;
    }
    
    • 1

    信息

    ID
    1647
    时间
    1000ms
    内存
    256MiB
    难度
    8
    标签
    提交数
    98
    已通过
    13
    上传者