Education Codeforce Round 131C

· · 题解

CF题目传送门

题目描述:

n个工人,m项工作,一些工人对一些工作很熟练,只要一个小时就能做完,不然就要两个小时。

求做完所有工作的最短时间。

输入:

第一行一个整数 t( 1≤t≤1000 ) 表示测试样例的数量。

每个测试用例的第一行包含两个整数 n 和 m(1≤n≤m≤210) ——工人数量和工作数量。

第二行包含m个整数 a1,a2,…,am(1≤ai≤n) -工人精通第i个工作的下标

输出:

对于每个测试用例,打印一个整数——所有任务可以完成的最短时间。

思路:

这道题可以用最多才多艺的工人会的工作减去最多空闲的人数。

例:

n = 3,m = 4;

a = {1,1,1,1};

最小工作时间就是4 - (3-1) = 2小时。

标程:

#include <bits/stdc++.h>
using namespace std;
#define int long long
const int MAX_INT = 2147483647;
const int INF = 0x3f3f3f3f;
const int MAXN = 200010;
int in(){
    int x = 0,f = 1;
    char c = getchar();
    while (!isdigit(c)){
        if (c == '-'){
            f = -1;
        }
        c = getchar();
    }
    while(isdigit(c)){
        x = x * 10 + c - '0';
        c = getchar();
    }
    return x * f;
}

signed main(){
    //freopen("xxx.in","r",stdin);
    //freopen("xxx.out","w",stdout);
    int t;
    cin >> t;
    while(t--){
        int n, m;
        cin >> n >> m;
        vector<int> a(m);
        for(int i = 0;i < m;i++){
            cin >> a[i];
            --a[i];
        }
        vector<int> cnt(n);
        for(int i = 0;i < m;i++){
            ++cnt[a[i]];
        }
        auto check = [&](int t){
            int fr = 0, need = 0;
            for(int i = 0;i < n;i++){
                if (t >= cnt[i]){
                    fr += (t - cnt[i]) / 2;
                }
                else{
                    need += cnt[i] - t;
                }
            }
            return need <= fr;
        }; 
        int l = 0, r = 2 * m;
        int res = -1;
        while (l <= r){//二分 
            int m = (l + r) / 2;
            if (check(m)){
                res = m;
                r = m - 1;
            }
            else{
                l = m + 1;
            }
        }
        cout << res << endl;
    }
    return 0;
}

沈明暄于2022/7/9作