CodeForces - 1701C Schedule Management

CodeForces - 1701C Schedule Management

题解:二分答案

很显然如果你给的时间越长,所有工作就越容易被完成,所以时间存在二分性,我们直接二分时间

但是我们现在需要解决一个问题:

1.check函数怎么写,也就是如何判断一个答案的合法性:首先我们肯定是让所有人去做他所精通的工作,如果在给出的mid时间中,他把自己精通的工作做完了,那么我们直接看他能不能帮别人做别人做不完的任务,如果他做不完,他只能等待别人帮忙,所以我们去记录每次没有完成任务的数量,如果有任务没有完成,答案就是不合法的

#include <bits/stdc++.h>
#define Zeoy std::ios::sync_with_stdio(false), std::cin.tie(0), std::cout.tie(0)
#define all(x) (x).begin(), (x).end()
#define endl '\n'
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
const int inf = 0x3f3f3f3f;
const ll INF = 0x3f3f3f3f3f3f3f3f;
const int mod = 1e9 + 7;
const double eps = 1e-9;
const int N = 2e5 + 10;
ll a[N];
int n, m;
map<int, int> mp;
bool check(int mid)
{
    ll cnt = 0;                             //代表没有做完的任务的数量,记得开longlong,寄了
    for (int i = 1; i <= n; ++i)
    {
        if (mp[i] > mid)                    //自己完不成自己精通的所有任务
            cnt += mp[i] - mid;
        else
            cnt -= (mid - mp[i]) / 2;       //自己有空余时间就去帮别人做
    }
    if (cnt > 0)
        return 0;
    else
        return 1;
}
int main(void)
{
    Zeoy;
    int t = 1;
    cin >> t;
    while (t--)
    {
        mp.clear();
        cin >> n >> m;
        for (int i = 1; i <= m; ++i)
        {
            cin >> a[i];
            mp[a[i]]++;                     //记录每个人精通任务的数量
        }
        int l = 0, r = 1e9;
        while (l <= r)
        {
            int mid = l + r >> 1;
            if (check(mid))
                r = mid - 1;
            else
                l = mid + 1;
        }
        cout << l << endl;
    }
    return 0;
}
posted @ 2023-01-08 13:41  Zeoy_kkk  阅读(35)  评论(0编辑  收藏  举报