博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Ants
阅读量:7211 次
发布时间:2019-06-29

本文共 2063 字,大约阅读时间需要 6 分钟。

Description

An army of ants walk on a horizontal pole of length l cm, each with a constant speed of 1 cm/s. When a walking ant reaches an end of the pole, it immediatelly falls off it. When two ants meet they turn back and start walking in opposite directions. We know the original positions of ants on the pole, unfortunately, we do not know the directions in which the ants are walking. Your task is to compute the earliest and the latest possible times needed for all ants to fall off the pole.

 

Input

The first line of input contains one integer giving the number of cases that follow. The data for each case start with two integer numbers: the length of the pole (in cm) and n, the number of ants residing on the pole. These two numbers are followed by n integers giving the position of each ant on the pole as the distance measured from the left end of the pole, in no particular order. All input integers are not bigger than 1000000 and they are separated by whitespace.
 

Output

For each case of input, output two numbers separated by a single space. The first number is the earliest possible time when all ants fall off the pole (if the directions of their walks are chosen appropriately) and the second number is the latest possible such time.
 

Sample Input

2

10 3

2 6 7

214 7

11 12 7 13 176 23 191

 

Sample Output

4 8

38 207

 

Source

 
思路:
假设每一只蚂蚁都不回头直接继续而行,因此最大的距离就是蚂蚁距离两边距离的最大值,最小值就是距离两边的距离的最小值中的最大值
 
代码:
1 #include
2 #include
3 using namespace std; 4 int main() { 5 int n; 6 cin >> n; 7 while (n--) { 8 int length, antNumber; 9 int mx = 0, leftLength, mn = 0;10 cin >> length >> antNumber;11 while (antNumber--) {12 //求最大距离13 cin >> leftLength;14 mx = max(max(length - leftLength, leftLength), mx);15 //求最小距离16 mn = max(min(length - leftLength, leftLength), mn);17 }18 cout << mn << " " << mx << endl;19 }20 return 0;21 }
View Code

 

转载于:https://www.cnblogs.com/Luckykid/p/9741986.html

你可能感兴趣的文章
关于代码评审的微博讨论汇集
查看>>
PHP vs Java
查看>>
C# 连接SQL Server数据库的几种方式--server+data source等方式
查看>>
Qt控件中的属性sizePolicy说明
查看>>
针对Properties中实时性要求不高的配置参数,用Java缓存起来
查看>>
Flex读取txt文件里的内容(二)
查看>>
mysql 变量set
查看>>
Deep Learning(深度学习)学习笔记整理系列
查看>>
【C解毒】缘木求鱼
查看>>
lua对模块接口扩展的一种方法
查看>>
DB,Cache和Redis应用场景分析
查看>>
CGI(通用网关接口)
查看>>
Cocos2d-x教程(28)-ttf 字体库的使用
查看>>
Mysql group by,order by,dinstict优化
查看>>
Notepad++ 经常使用快捷键 (MEMO)
查看>>
nyoj116士兵杀死(两)段树单点更新
查看>>
《Programming WPF》翻译 第3章 2.处理输入
查看>>
cdoj 1252 24点游戏 dfs
查看>>
【问题汇总】ListView的FooterView设置可见性的问题
查看>>
JAVA中int、String的类型转换
查看>>