codeforces 1676 E

算是发现了,cf低分题都是思维…

ACcode

#include

using namespace std;

using ll = long long;

bool cmp(int x, int y) {
    return x > y;
}

void solve() {
    int n, q;cin >> n >> q;
    vectora(n + 3);
    for (int i = 1;i <= n;i++)cin >> a[i];
    sort(a.begin() + 1, a.begin() + 1 + n, cmp);
    for (int i = 1;i <= n;i++)a[i] += a[i - 1];
    while (q--) {
        int x;cin >> x;
        if (x > a[n]) {
            cout << -1 << '\n';
            continue;
        }
        if (x == a[n]) {
            cout << n << '\n';
            continue;
        }
        int ans = -1;
        ans = lower_bound(a.begin() + 1, a.begin() + 1 + n, x) - a.begin();
        cout << ans << '\n';
    }
}

int main() {
    ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
    int t;cin >> t;
    while (t--) {
        solve();
    }
    return 0;
}

你可能感兴趣的:(codeforces,板刷二分,rating,1200,算法,c++,数据结构)