码迷,mamicode.com
首页 > 其他好文 > 详细

Cleaning the Phone

时间:2021-01-28 12:04:58      阅读:0      评论:0      收藏:0      [点我收藏+]

标签:http   als   problem   tin   The   clean   targe   blank   sync   

链接 : 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;
}

Cleaning the Phone

标签:http   als   problem   tin   The   clean   targe   blank   sync   

原文地址:https://www.cnblogs.com/phr2000/p/14336721.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!