Ants
Time Limit: 1000MS   Memory Limit: 30000K
Total Submissions: 12893   Accepted: 5637

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
    题解:水题,就是一个木棒在水里,蚂蚁在这个木棒上可以右走,也可以左走,1cm/s,问最小最大多长时间都掉到水里;
    代码:
  1. #include<cstdio>
  2. #include<cstring>
  3. #include<cmath>
  4. #include<algorithm>
  5. using namespace std;
  6. const int MAXN=1000010;
  7. int a[MAXN];
  8. int main(){
  9. int T,l,n,x,mx;
  10. scanf("%d",&T);
  11. while(T--){
  12. scanf("%d%d",&l,&n);
  13. mx=-1;
  14. for(int i=0;i<n;i++){
  15. scanf("%d",&x);
  16. mx=max(mx,x);
  17. mx=max(mx,l-x);
  18. a[i]=min(x,l-x);
  19. }
  20. printf("%d %d\n",*max_element(a,a+n),mx);
  21. }
  22. return 0;
  23. }

  

Ants(思维)的更多相关文章

  1. UVA.10881 Piotr's Ants (思维题)

    UVA.10881 Piotr's Ants (思维题) 题意分析 有一根长度为L cm的木棍,上有n只蚂蚁,蚂蚁要么向左爬,要么向右,速度均为1cm/s,若2只蚂蚁相撞,则蚂蚁同时调头.求解第T秒时 ...

  2. 思维题 UVA 10881 Piotr's Ants

    题目传送门 /* 题意:在坐标轴上一群蚂蚁向左或向右爬,问经过ts后,蚂蚁的位置和状态 思维题:本题的关键1:蚂蚁相撞看作是对穿过去,那么只要判断谁是谁就可以了 关键2:蚂蚁的相对位置不变 关键3:o ...

  3. poj 2565 Ants (KM+思维)

    Ants Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 4125   Accepted: 1258   Special Ju ...

  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. UVA 10881 - Piotr's Ants【模拟+思维】

    题目链接:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem& ...

  6. poj1852 Ants(思维)

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

  7. 【思维题】AGC013C - Ants on a Circle

    妙妙技巧题 题目描述 题目大意 一个圆环上有n只蚂蚁,它们会按照顺时针或者逆时针行走.如果有蚂蚁相遇它们就会掉头(不一定在整数时间掉转).问最后每只蚂蚁的位置. 题目分析 以前在luogu上做过一道类 ...

  8. Ants,小小思维题。

    题目链接 题意: 蚂蚁在一个杆子上行走,每个蚂蚁有开始的位置,且速度都是1,如果蚂蚁“相撞”就会各自回头,以原速度继续行走,走到杆子边上就会掉下去,请问最快都掉下去的时间和最慢都掉下去的时间. 题目分 ...

  9. poj 1852 ants 题解《挑战程序设计竞赛》

    地址  http://poj.org/problem?id=1852 题目描述 Description An army of ants walk on a horizontal pole of len ...

随机推荐

  1. Oracle游标使用详解

    转自:http://www.cnblogs.com/sc-xx/archive/2011/12/03/2275084.html 声明游标:CURSOR cursor_name IS select_st ...

  2. ActiveMQ的入门demo

    步骤: 1 :下载ActiveMQ 官网:http://activemq.apache.org/ 2 :解压AcitveMQ, 根据自己的操作系统选择运行win64或者win32下的activemq. ...

  3. Asp MVC 中处理JSON 日期类型

    注:时间有点忙,直接copy 过来的,要查看原址: http://www.developer.com/net/dealing-with-json-dates-in-asp.net-mvc.html I ...

  4. table常用

    <style> table,table td { border: 1px solid #ccc; border-collapse:collapse; } </style> 注意 ...

  5. Umbraco学习2------数据类型

    一.基础概念 在使用Umbraco这类CMS制作网站之前,先要搞清楚的是,和概念中网站制作的区别. 暂时忘掉所谓的ADO.NET存储.忘掉ASP.NET.忘掉多层架构什么的. 只需要关注:要显示什么. ...

  6. gcc 的include path和lib path调整

    `gcc -print-prog-name=cc1plus` -v `g++ -print-prog-name=cc1plus` -v   ------------------------------ ...

  7. JqGrid使用经验

    一.更改分页组件的样式 分页组件中

  8. TP的SDK的调用

    1,SDK简介 本SDK是基于ThinkPHP开发类库扩展,因此只能在ThinkPHP平台下使用(ThinkPHP版本要求2.0以上).DEMO中用到了控制器分层,因此运行DEMO需使用ThinkPH ...

  9. 使用COCOS2D-X JSB开发,在js页面中设置iOS键盘模式

    XYSDK.h void setKeyboardType(int type); XYSDK.cpp voidXYSDK::setKeyboardType(int type) { #if (CC_TAR ...

  10. 黑客瑞士军刀NC使用教程

    ###################################################################### 1. 写在前面的话 ################### ...