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

  1. 2
  2. 10 3
  3. 2 6 7
  4. 214 7
  5. 11 12 7 13 176 23 191

Sample Output

  1. 4 8
  2. 38 207

Source

Thinking

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

  1. var n,m,i,j,mint,maxt,t:longint;
  2. a:array[..] of longint;
  3. function min(x,y:longint):longint;
  4. begin
  5. if x>y then exit(y) else exit(x);
  6. end;
  7. function max(x,y:longint):longint;
  8. begin
  9. if x>y then exit(x) else exit(y);
  10. end;
  11. procedure main;
  12. var i:longint;
  13. begin
  14. readln(m,n);
  15. for i:= to n do
  16. read(a[i]);
  17. mint:=;
  18. maxt:=;
  19. for i:= to n do
  20. begin
  21. mint:=max(mint,min(a[i],m-a[i]));
  22. maxt:=max(maxt,max(a[i],m-a[i]));
  23. end;
  24. writeln(mint,' ',maxt);
  25. end;
  26. begin
  27. readln(t);
  28. for i:= to t do main;
  29. 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. 转.....IOC 和DI

    引述:IoC(控制反转:Inverse of Control)是Spring容器的内核,AOP.声明式事务等功能在此基础上开花结果.但是IoC这个重要的概念却比较晦涩隐讳,不容易让人望文生义,这不能不 ...

  2. javascript操作html元素CSS属性

    下面先记录一下JS控制CSS所使用的方法. 1.使用javascript更改某个css class的属性... <style type="text/css"> .ori ...

  3. call_user_func_array

    call_user_func_array — 调用回调函数,并把一个数组参数作为回调函数的参数 参数 callback 被调用的回调函数. param_arr 要被传入回调函数的数组,这个数组得是索引 ...

  4. Delphi XE2及以后的版本编译后的程序大小问题

    说说Delphi XE2及以后的版本编译后的程序大小问题. 其实最终得到的程序并不大,由于编译器的变化,XE2里Debug版程序比Release版程序大很多,要减小程序体积,就使用Release版.下 ...

  5. ubuntu 12 64 桌面版Oracle11g 安装

    1.Creating the Oracle Inventory Group sudo groupadd oinstall sudo groupadd dba sudo groupadd oper su ...

  6. Design Tutorial: Learn from Life

    Codeforces Round #270 B:http://codeforces.com/contest/472/problem/B 题意:n个人在1楼,想要做电梯上楼,只有1个电梯,每次只能运k个 ...

  7. Uva 12361 File Retrieval 后缀数组+并查集

    题意:有F个单词,1 <= F <=60 , 长度<=10^4, 每次可以输入一个字符串,所有包含该字串的单词会形成一个集合. 问最多能形成多少个不同的集合.集合不能为空. 分析:用 ...

  8. HDU Traffic Real Time Query System

    题目大意是:对于(n, m)的图,给定边a, b查询从a到b要经过的割点的最少数目. 先tarjan算法求双连通然后缩点,即对于每个割点将周围的每个双连通看成一个点与之相连.然后求解LCA即可,距离d ...

  9. LINQ TO SQL 怎样 执行存储过程并返回存储过程的临时表

    查了GOOGLE一下,这种办法可以解决 我的存储过程是这样的: CREATE procedure cal_month_dep_fast  @begdt datetime,@endt datetime ...

  10. Qt for PC,Qt for iOS,Qt for Android (居士的博客)

    http://blog.csdn.net/Esonpo/article/details/38081607 http://blog.csdn.net/Esonpo/article/details/380 ...