Cleaning the Phone

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

排序 + 双指针

#include <bits/stdc++.h>
using namespace std;
#define IO ios::sync_with_stdio(false);cin.tie(0); cout.tie(0)
inline int lowbit(int x) { return x & (-x); }
#define ll long long
#define pb push_back
#define PII pair<int, int>
#define fi first
#define se second
#define inf 0x3f3f3f3f
const int N = 2e5 + 7;
int a[N], b[N];
    
int main() {
    IO;
    int _;
    cin >> _;
    while (_--) {
        int n, m;
        cin >> n >> m;
        ll sum = 0;
        for (int i = 0; i < n; ++i) {
            cin >> a[i];
            sum += a[i];
        }
        for (int i = 0; i < n; ++i) cin >> b[i];
        if (sum < m) {
            cout << -1 << '\n';
            continue;
        }
        
        sum = 0;
        int ans = inf;
        vector<int> k1, k2;
        for (int i = 0; i < n; ++i) {
            if (b[i] == 1) k1.pb(a[i]);
            else {
                k2.pb(a[i]);
                sum += a[i];
            }
        }
        int n1 = k1.size(), n2 = k2.size();
        sort(k1.rbegin(), k1.rend());
        sort(k2.rbegin(), k2.rend());
        
        int j = 0;
        for (int i = n2; i >= 0; --i) {
            while (j < n1 && sum < m) {
                sum += k1[j];
                j++;
            }
            if (sum >= m) ans = min(ans, i * 2 + j);
            if (i > 0) sum -= k2[i - 1];
        }

        cout << ans << '\n';
    }

    return 0;
}
posted @ 2021-01-27 20:09  phr2000  阅读(54)  评论(0编辑  收藏  举报