Time Limit: 1000MS   Memory Limit: 30000K
Total Submissions: 12431   Accepted: 5462

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

Thinking

  如果这个题使用穷竭搜索的话,时间复杂度O(2^n);显然不行,我们可以采取一种更为巧妙的方法。如果两只蚂蚁碰头以后,他们互相折返,能不能把他们看成是向左的蚂蚁按照的向右的方向向右走,向右的亦是如此,实质上就是两个蚂蚁继续按照原方向行进。所以我们维护一个最小值,一个最大值即可。

var n,m,i,j,mint,maxt,t:longint;
a:array[..] of longint;
function min(x,y:longint):longint;
begin
if x>y then exit(y) else exit(x);
end;
function max(x,y:longint):longint;
begin
if x>y then exit(x) else exit(y);
end;
procedure main;
var i:longint;
begin
readln(m,n);
for i:= to n do
read(a[i]);
mint:=;
maxt:=;
for i:= to n do
begin
mint:=max(mint,min(a[i],m-a[i]));
maxt:=max(maxt,max(a[i],m-a[i]));
end;
writeln(mint,' ',maxt);
end;
begin
readln(t);
for i:= to t do main;
end.

[POJ1852]Ants的更多相关文章

  1. poj1852 Ants ——想法题、水题

    求最短时间和最长时间. 当两个蚂蚁相遇的时候,可以看做两个蚂蚁穿过,对结果没有影响.O(N)的复杂度 c++版: #include <cstdio> #define min(a, b) ( ...

  2. poj1852 Ants(思维)

    https://vjudge.net/problem/POJ-1852 最短时间显然是各自往靠近端点的地方走. 最长时间关键是要想到,折返和擦肩其实是一样的,可以当成两只蚂蚁换了个位子,最终求max是 ...

  3. POJ1852 Ants 题解

    题目 An army of ants walk on a horizontal pole of length l cm, each with a constant speed of 1 cm/s. W ...

  4. [POJ1852] Ants(思维题)

    题干 An army of ants walk on a horizontal pole of length l cm, each with a constant speed of 1 cm/s. W ...

  5. [POJ3684]Physics Experiment

      Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 1363   Accepted: 476   Special Judge ...

  6. 使用ANTS Performance Profiler&ANTS Memory Profiler工具分析IIS进程内存和CPU占用过高问题

    一.前言 最近一段时间,网站经常出现两个问题: 1.内存占用率一点点增高,直到将服务器内存占满. 2.访问某个页面时,页面响应过慢,CPU居高不下. 初步判断内存一点点增多可能是因为有未释放的资源一直 ...

  7. Uva10881 Piotr's Ants

    蚂蚁相撞会各自回头.←可以等效成对穿而过,这样移动距离就很好算了. 末状态蚂蚁的顺序和初状态其实是相同的. 那么剩下的就是记录每只蚂蚁的标号,模拟即可. /*by SilverN*/ #include ...

  8. Poj1852

    题目求的是:所有蚂蚁用最短时间从木棍上走下来的最大值(也就是最后一个蚂蚁什么时候走下来的) 所有蚂蚁中,用时最长的情况 PS:根本不用考虑两只蚂蚁相遇又折返的情况(可以直接认为是他两互不影响的走) # ...

  9. [ACM_模拟] UVA 10881 Piotr's Ants[蚂蚁移动 数组映射 排序技巧]

    "One thing is for certain: there is no stopping them;the ants will soon be here. And I, for one ...

随机推荐

  1. Sersync实时同步企业应用配置实战

    一.实验环境 CentOS版本: 6.6(2.6.32.-504.el6.x86_64) Rsync版本:  Rsync-3.0.6(系统自带) Sersync版本:sersync2.5.4_64bi ...

  2. merge into 和 update 的效率对比

    以前只考虑 merge into 只是在特定场合下方便才使用的,今天才发现,merge into 竟然会比 update 在更新数据时有这么大的改进.其实呢,merge into部分的update和u ...

  3. uCGUI字符串显示过程分析和uCGUI字库的组建

    为什么要分析字符串的显示过程? 学习uCGUI主要是学习如何使用的,为何要深究到源码的层次呢? 就分析字符串显示过程的原因来说,是因为移植汉字字库的需要.uCGUI并么有合适的汉字字库,而且完整的汉字 ...

  4. python与编码

    Python中的文字对象 Python 3.x中处理文字的对象有str, bytes, bytearray. bytes和bytearray可以使用除了用作格式化的方法(format, format_ ...

  5. [Jquery] Jquery获取浏览器宽高的代码

    <script type="text/javascript"> $(document).ready(function() { alert($(window).heigh ...

  6. [JavaScript] 初中级Javascript程序员必修学习目录

    很多人总感觉javascript无法入门,笔者在这里写一下自己的学习过程,以及个人认 为的最佳看书过程,只要各位能按照本人所说步骤走下去,不用很长时间,坚持 个3个月,你的js层级会提高一个档次,无他 ...

  7. 总结: Struts2 + Spring 的线程安全问题

    1. 首先Struts2 本身是安全的 其原理大概是:Strtus2会获取到用户的http请求,然后负责给每个请求实例化一个Action 对象,但是大家注意,这里的action对象和Struts1里面 ...

  8. packinfo-java的作用

    package-info.java 包的作用 1. 为标注在包上的Annotation提供便利 2. 声明包的私有类和常量 3. 提供包的整体注释说明   代码说明: package-info.jav ...

  9. 在C++中子类继承和调用父类的构造函数方法

    构造方法用来初始化类的对象,与父类的其它成员不同,它不能被子类继承(子类可以继承父类所有的成员变量和成员方法,但不继承父类的构造方法).因此,在创建子类对象时,为了初始化从父类继承来的数据成员,系统需 ...

  10. libvlc 双击,鼠标事件消息响应

    基于vlc 2.1 动态库实现接收双击消息的接收,使双击vlc播放画面可以全屏显示. 需要其他版本的vlc可以与我联系(有偿进行修改) 下载地址:http://download.csdn.net/de ...