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. KISSY学习笔记(更新中)

    序:身为一个JAVA开发工程师,前端代码我尽量是使用原生的JS来写的,或是使用一些JQ的开源组件(但是也只是使用,没有好好去研究过JQ这个框架).目前由于工作需要,必须要使用KISSY,打算借此机会, ...

  2. emctl start dbconsole OC4J_dbconsole*** not found

    C:\windows\system32>emctl start dbconsole OC4J Configuration issue. D:\app\product\\db_1/oc4j/j2e ...

  3. Java学习-Overload和Override的区别

    1.Overload是重载的意思,Override是覆盖的意思,也就是重写. 2.重载Overload表示同一个类中可以有多个名称相同的方法,但这些方法的参数列表各不相同(即参数个数或类型不同). 3 ...

  4. JAVA TCP/IP Socket通信机制以及应用

    关于局域网通信(同一wifi下,自己电脑当服务端,同一网络段) 1.例如192.168.1.x,只有x位不相同视为同一网络段 2.当具备了以上条件,即可编写服务端代码,服务端的机制. 3.Server ...

  5. 基于Python的TCP阻塞式echo服务器

    上述问题的出现是因为没有设置listen函数 from socket import * from time import ctime HOST = '' PORT = 21567 BUFSIZ = 1 ...

  6. YARN加载本地库抛出Unable to load native-hadoop library解决办法

    YARN加载本地库抛出Unable to load native-hadoop library解决办法 用官方的Hadoop 2.1.0-beta安装后,每次hadoop命令进去都会抛出这样一个War ...

  7. JAVA自学之-----FileInputStream类

    1, FileInputStream类函数创建: package coreJava; import java.io.FileInputStream; import java.io.IOExceptio ...

  8. loadmore & scroll

    loadmore <link rel="stylesheet" href="http://code.jquery.com/mobile/1.0.1/jquery.m ...

  9. 周末“干活”之 Mesos Meetup

    周末两天都是大雾霾天,作为运营也不能在家宅,告别了技术就得腿儿勤点儿. 非常感谢 Linker 的 Sam Chen 和 数人科技 的 CTO 共同组织的Mesos Meetup,OneAPM 最帅的 ...

  10. POJ3663

    题意简单. 关键:记录每头牛的val值,每次寻找和某头牛匹配的牛时候,可以通过刚刚记录的值来计算. #include<stdio.h> #include<string.h> # ...