题面:

E. Convention

Input file: standard input
Output file: standard output
Time limit: 1 second
Memory limit: 256 megabytes
 
Cows from all over the world are arriving at the local airport to attend the convention and eat grass. Specifically, there are N cows arriving at the airport (1 ≤ N ≤ 10^5) and cow i arrives at time ti (0 ≤ ti ≤ 10^9). Farmer John has arranged M (1 ≤ M ≤ 10^5) buses to transport the cows from the airport. Each bus can hold up to C cows in it (1 ≤ C ≤ N). Farmer John is waiting with the buses at the airport and would like to assign the arriving cows to the buses. A bus can leave at the time when the last cow on it arrives. Farmer John wants to be a good host and so does not want to keep the arriving cows waiting at the airport too long. What is the smallest possible value of the maximum waiting time of any one arriving cow if Farmer John coordinates his buses optimally? A cow’s waiting time is the difference between her arrival time and the departure of her assigned bus.
It is guaranteed that MC ≥ N.
 
 
Input
The first line contains three space separated integers N, M, and C. The next line contains N space separated integers representing the arrival time of each cow.
 
Output
Please write one line containing the optimal minimum maximum waiting time for any one arriving cow.
 
Example
Input
6  3  2
1  1  10  14  4  3
Output
4
 

题目描述:

农夫要在机场接待奶牛,给出每个奶牛到达的时间。现在农夫有M辆车,一辆车可以装满C头牛,问如何安排才能使在这所有奶牛的等待时间中(有些车没装够奶牛可能会多等几头奶牛来再开走,这样就产生了等待时间),奶牛等待时间最大的那个尽可能短。
 

题目分析:

这道题的表述有点像之前我做过的题:传送门
其实关键的是这里:“最大的时间尽可能短”,和奶牛隔间那道题很相似。这提示了我们应该要用二分来做。我们再看看题目的时间:10^9  ,这么大,当用二分来做时,复杂度是O(log n),也就是大概32次就能做出来了,说明极有可能是二分。这道题的确用二分来完成,所以套二分的模板就可以完成二分的部分,只要区间设置成左开右闭就行了,关键是check()函数怎么写,这个是重点。
 
当我们要验证时间 t 是否满足题目的最大等待时间时,我们可以这样想:
假设之前公交车上没有牛,现在第一头牛到了机场,然后上了车,只要这第一头牛等待了 t 时间后把车开走就行了。在这 t 时间内,如果还有其他牛到达,就把他们先安排上第一头牛的车。车如果载满牛的话就开走。然后再反复按照这个规则安排。如果按照这样的规则,车不够用,那么就返回false,车够用的化就返回true。有些人可能有疑问:如果车还没有等待到时间 t 后提前开走(也就是载满牛的情况),用这个方法就算不出答案了。其实在这里也是可以算出答案的,因为时间 t 满足上面的条件时,答案区间就会变成( left, t]。也就是说答案是小于等于 t 的,就算我们的 t 是不符合最大等待时间的,但是我们也得出了答案的范围,所以这个check()函数的写法是有效的。
 
 
AC代码:
 1 #include <cstdio>
2 #include <cstring>
3 #include <iostream>
4 #include <cmath>
5 #include <set>
6 #include <algorithm>
7 using namespace std;
8 const int maxn = 1e5+5;
9 int n, m, c;
10 long long a[maxn];
11
12 bool check(int t){
13 int all = m; //车的数量
14 int p = 0;
15 while(p < n){
16 int k = p;
17 int d = c; //车的装载量
18 while(a[k]-a[p] <= t && d && k < n){
19 d--, k++;
20 }
21 p = k;
22 all--;
23 }
24 if(all >= 0) return true; //车够用就返回true
25 else return false;
26 }
27
28 int main(){
29 cin >> n >> m >> c;
30 for(int i = 0; i < n; i++) cin >> a[i];
31 sort(a, a+n);
32
33 int l = -1, r = 1e9;
34 int mid;
35 while(l < r){
36 mid = (l+r)/2; //左开右闭向下取整
37 if(check(mid)){
38 r = mid;
39 }
40 else l = mid+1;
41 }
42 cout << l << endl;
43 return 0;
44 }
 
 

2019 GDUT Rating Contest I : Problem E. Convention的更多相关文章

  1. 2019 GDUT Rating Contest II : Problem F. Teleportation

    题面: Problem F. Teleportation Input file: standard input Output file: standard output Time limit: 15 se ...

  2. 2019 GDUT Rating Contest III : Problem D. Lemonade Line

    题面: D. Lemonade Line Input file: standard input Output file: standard output Time limit: 1 second Memo ...

  3. 2019 GDUT Rating Contest I : Problem H. Mixing Milk

    题面: H. Mixing Milk Input file: standard input Output file: standard output Time limit: 1 second Memory ...

  4. 2019 GDUT Rating Contest I : Problem A. The Bucket List

    题面: A. The Bucket List Input file: standard input Output file: standard output Time limit: 1 second Me ...

  5. 2019 GDUT Rating Contest I : Problem G. Back and Forth

    题面: G. Back and Forth Input file: standard input Output file: standard output Time limit: 1 second Mem ...

  6. 2019 GDUT Rating Contest III : Problem E. Family Tree

    题面: E. Family Tree Input file: standard input Output file: standard output Time limit: 1 second Memory ...

  7. 2019 GDUT Rating Contest III : Problem C. Team Tic Tac Toe

    题面: C. Team Tic Tac Toe Input file: standard input Output file: standard output Time limit: 1 second M ...

  8. 2019 GDUT Rating Contest III : Problem A. Out of Sorts

    题面: 传送门 A. Out of Sorts Input file: standard input Output file: standard output Time limit: 1 second M ...

  9. 2019 GDUT Rating Contest II : Problem G. Snow Boots

    题面: G. Snow Boots Input file: standard input Output file: standard output Time limit: 1 second Memory ...

随机推荐

  1. 8.rabbitmq RPC模拟微服务架构中的服务调用

    标题 : 8.rabbitmq RPC模拟微服务架构中的服务调用 目录 : RabbitMQ 序号 : 8 { var connectionFactory = new ConnectionFactor ...

  2. Linux添加系统调用

    Linux添加系统调用 1 概述 通常添加系统调用有两种方案: * 重新编译内核 * 添加内核模块 此处我们采用重新编译内核的方式增加系统调用. 实验环境:X86_64 GNU/Linux 4.15. ...

  3. redis字典

    字典作为一种保存键值对的数据结构,在redis中使用十分广泛,redis作为数据库本身底层就是通过字典实现的,对redis的增删改查实际上也是构建在字典之上. 一.字典的结构

  4. Petrozavodsk Winter Training Camp 2017G(栈)题解

    题意: \(M_i\)为一个\(m*m\)矩阵,已知 \[\begin{aligned} &M_0=A\\ &M_i=(\prod_{j=c_i}^{i+1}M_j)B \end{al ...

  5. 使用LCX进行内网端口转发

    Lcx.exe是一个端口转发工具,相当于把目标服务器A上的3389端口转发到具有外网ip地址的B机上(即我们自己的主机或是已经控制的主机),这样链接B机的3389端口就相当于链接A机的3389端口了, ...

  6. web 安全 & web 攻防: XSS(跨站脚本攻击)和 CSRF(跨站请求伪造)

    web 安全 & web 攻防: XSS(跨站脚本攻击)和 CSRF(跨站请求伪造) XSS(跨站脚本攻击)和CSRF(跨站请求伪造) Cross-site Scripting (XSS) h ...

  7. brew & apply2files bug

    brew & apply2files bug Error: Permission denied @ apply2files - /usr/local/lib/node_modules/npm/ ...

  8. 同样是NGK官方推出的代币,SPC与BGV有何异同?

    近日,币圈又火热了起来,而这次火热是由NGK搅动的.原来,NGK官方空投了200万枚SPC,用于奖励NGK算力持有者.当前,已经有一部分算力持有者获得了SPC奖励,有的算力持有者获得的SPC数量惊人, ...

  9. NGK和USDN的应用

    一.NGK和USDN的发展方向 目前区块链将会朝着两个方向去发展,第一种是金融经济的衍生品,第二种是商业应用,快速支付的货币体系,NGK.IO公链是基于分布式应用设计的商用金融区块链操作系统,通过数字 ...

  10. Union international INC评德意志联邦投入十亿欧元重启文化娱乐产业

    当地时间6月4日,德国联邦政府宣布了一项名为"重启文化"(Neustart Kultur)的计划,将投入总计10亿欧元,用以支持德国文化及创意产业的恢复和重建. Union int ...