牛客假日团队赛1 I.接机

链接:

https://ac.nowcoder.com/acm/contest/918/I

题意:

一场别开生面的牛吃草大会就要在Farmer John的农场举办了!
世界各地的奶牛将会到达当地的机场,前来参会并且吃草。具体地说,有N头奶牛到达了机场(1≤N≤105),其中奶牛i在时间ti(0≤ti≤109)到达。Farmer John安排了M(1≤M≤10^5)辆大巴来机场接这些奶牛。每辆大巴可以乘坐C头奶牛(1≤C≤N)。Farmer John正在机场等待奶牛们到来,并且准备安排到达的奶牛们乘坐大巴。当最后一头乘坐某辆大巴的奶牛到达的时候,这辆大巴就可以发车了。Farmer John想要做一个优秀的主办者,所以并不想让奶牛们在机场等待过长的时间。如果Farmer John合理地协调这些大巴,等待时间最长的奶牛等待的时间的最小值是多少?一头奶牛的等待时间等于她的到达时间与她乘坐的大巴的发车时间之差。

输入保证MC≥N。

思路:

二分答案

代码:

#include <bits/stdc++.h>
 
using namespace std;
 
typedef long long LL;
const int MAXN = 1e5 + 10;
const int MOD = 1e9 + 7;
int n, m, k, t;
 
int Times[MAXN];
 
bool Check(int x)
{
    int he = 1, now = 1;
    for (int c = 1;c <= m;c++)
    {
        while (now <= n && now-he+1 <= k && Times[now]-Times[he] <= x)
            now++;
        he = now;
    }
    if (he > n)
        return true;
    return false;
}
 
int main()
{
    scanf("%d%d%d", &n, &m, &k);
    for (int i = 1;i <= n;i++)
        scanf("%d", &Times[i]);
    sort(Times+1, Times+1+n);
    int l = 0, r = Times[n];
    int res = 1e9;
    while (l<r)
    {
//        cout << l << ' ' << r << endl;
        int mid = (l+r)/2;
        if (Check(mid))
        {
            res = min(res, mid);
            r = mid;
        }
        else
            l = mid+1;
    }
    cout << res << endl;
 
    return 0;
}
原文地址:https://www.cnblogs.com/YDDDD/p/10995673.html