题目描述:

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

代码如下:

 #include<iostream>
int Max(int,int);
int Min(int,int);
int main()
{
using namespace std;
int i;
cin >> i;
while(i--)
{
int l;
int num;
cin >> l >> num;
int* pt = new int [num];
for(int j = ;j < num;j++)
cin >> pt[j];
int min_ = ,max_ = ;
for(int j = ;j < num;j++)
min_ = Max(min_,Min(pt[j],l - pt[j]));//最小时间是蚂蚁爬向最近一端
for(int j = ;j < num;j++)
max_ = Max(max_,Max(pt[j],l - pt[j]));//无视不同蚂蚁的区别,可以认为是保持原样交错而过
cout << min_ << " " << max_ << endl;
delete [] pt;
}
return ;
} int Max(int a,int b)
{
return a > b ? a : b;
} int Min(int a,int b)
{
return b > a ? a : b;
}

代码分析:

这道题目的难度不在于用到什么算法,而在于想明白两只蚂蚁相遇然后返回,等同于无视蚂蚁的区别,保持原样交错而过。所以有的题目有时候想多了,反而不得解。

参考书籍:[挑战程序设计竞赛第2版]

Ants (POJ 1852)的更多相关文章

  1. POJ 1852 Ants || UVA 10881 - Piotr's Ants 经典的蚂蚁问题

    两题很有趣挺经典的蚂蚁问题. 1.n只蚂蚁以1cm/s的速度在长为L的竿上爬行,当蚂蚁爬到竿子的端点就会掉落.当两只蚂蚁相撞时,只能各自反向爬回去.对于每只蚂蚁,给出距离左端的距离xi,但不知道它的朝 ...

  2. POJ 1852 Ants(贪心)

    POJ 1852 Ants 题目大意 有n只蚂蚁在木棍上爬行,每只蚂蚁的速度都是每秒1单位长度,现在给你所有蚂蚁初始的位置(蚂蚁运动方向未定),蚂蚁相遇会掉头反向运动,让你求出所有蚂蚁都·掉下木棍的最 ...

  3. poj 1852&3684 题解

    poj 1852 3684 这两题思路相似就放在一起. 1852 题意 一块长为L长度单位的板子(从0开始)上有很多只蚂蚁,给出它们的位置,它们的方向不确定,速度为每秒一长度单位,当两只蚂蚁相遇的时候 ...

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

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

  5. POJ 1852:Ants

    Ants Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 11754   Accepted: 5167 Description ...

  6. 【弹性碰撞问题】POJ 1852 Ants

    Description An army of ants walk on a horizontal pole of length l cm, each with a constant speed of ...

  7. POJ 1852 Ants

    题目的意思是说一个长度为m的杆,上面有n个蚂蚁,告诉每个蚂蚁的初始位置,每个蚂蚁速度都是一样的,问所有的蚂蚁离开杆的最短和最长时间是多少. 模拟题,所有的蚂蚁看成一样的,可以这样理解,即使相撞按反方向 ...

  8. POJ 1852 Ants (等价思考)

    题意:在一根杆上有 n 只蚂蚁,速度为1,方向不定,如果相碰,则反向运动,问你最长的时间和最短时间,所有蚂蚁都掉下杆去. 析:换个方法想,如果两只蚂蚁相碰了,会有什么现象?其实就和没有碰撞是一样的,没 ...

  9. POJ 1852 Ants O(n)

    题目: 思路:蚂蚁相碰和不相碰的情况是一样的,相当于交换位置继续走. 代码: #include <iostream> #include <cstdio> #include &l ...

随机推荐

  1. Maven 添加Jetty

        <build>         <finalName>springmvc</finalName>         <plugins>       ...

  2. oracle 11g RAC ocfs2

    http://oracle-base.com/articles/linux/ocfs2-on-linux.php http://oracle-base.com/articles/11g/oracle- ...

  3. 成都Uber优步司机快速注册攻略(外地车牌也可加入,不用现场培训)

    我加入Uber司机有一段时间了,有一些经验和感想分享给大家,让大家少走些弯路.目前加入优步不收取任何费用,不需要抢单,时间安排自由灵活,使用便捷,深受大众喜爱. 加入人民优步拼车条件:购买运行5年之内 ...

  4. 如何注册成为uber司机 快速成为优步司机网上注册流程攻略 2015最新

    [目前开通Uber的城市]:北京.上海.天津.广州.成都.深圳.杭州.重庆.武汉.青岛.南京.苏州.长沙.宁波.西安.佛山等.济南,烟台和厦门正在秘密的招第一批司机. [车辆要求]:要求裸车价8万以上 ...

  5. S3C6410嵌入式应用平台构建(一)

    [2014-4/8~4/10]目前我们已经积累一定的嵌入式相关知识,对嵌入式的架构及开发过程有了大体了解,唯一缺的就是实践,通过自己的分析搭建自己的嵌入式系统.下面,我将从此处开始记录我和我同学一起分 ...

  6. Asp.net 提供程序模型

    需要说明一下几点 1.什么是提供程序? 2.ASP.NET 4.5 中的提供程序 3.配置提供程序 有一下几种存储状态的方式 1.应用程序状态 2.会话状态 3.高速缓存状态 4.cookie 5.查 ...

  7. 前缀 树 背单词(Remember the Word,LA 3942)

    已哭瞎. 搞了2个多小时的错误居然是在  没有初始化............教训:每个例子一定要考虑到初始化问题.! 每个节点存了一个数组  该数组记录的有26个大小  0-25分别表示记录表示'a' ...

  8. Mybatis学习之JDBC缺陷

    1.JDBC存在的问题 1.将sql语句硬编码到java代码中,如果修改sql语句,需要修改java代码,重新编译.系统可维护性不高. 设想如何解决?(将sql单独 配置在配置文件中) 2.数据库连接 ...

  9. C#中小函数的应用

    今天看到的一段函数 StringBuilder sb = new StringBuilder("sselect * from table where 1=1"); if(TextB ...

  10. ajax+php如何获取部分请求的信息显示在对应的div中

    我该如何把需要显示的信息显示在文章列表中呢???