题目描述:

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. Android 流媒体系列(二)

    import java.io.IOException; import android.app.Activity; import android.content.ContentResolver; imp ...

  2. 男装电子零售商East Dane即将面世_衣装_YOKA时尚网

    男装电子零售商East Dane即将面世_衣装_YOKA时尚网 男装电子零售商East Dane即将面世

  3. js导出成excel

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  4. UVA - 297Quadtrees(四分图)

    Quadtrees Time Limit: 3000MS   Memory Limit: Unknown   64bit IO Format: %lld & %llu Submit Statu ...

  5. Android系统的“程序异常退出”[转]

    在应用运行过程中,有很多异常可能会发生,而我们希望在异常发生的时候第一时间的保存现场. 如何处理未捕获的异常呢? 首先我们要实现一个接口  java.lang.Thread.UncaughtExcep ...

  6. xcode 资源管理

    我个人觉得这么理解就够了 其他的以后再说

  7. string s = HttpContext.Current.Server.MapPath("");

    string s = HttpContext.Current.Server.MapPath(""); 获取当前文件夹路径 而后用相对路径读取图片

  8. ztree树形插件

    在开发项目中需要用到树插件,近期研究了几款树插件,好记性不如烂笔头 ,写下来  以后好查 MzTreeView(梅花雪) 很经典的树形菜单脚本控件 菜单树展示加载速度快 支持1w条以上大数据 缺点-- ...

  9. Unity 通过NGUI 完成单摄像机 制作地图

    本次思想主要是通过 Ngui的Scroll View 主要是UIPanel的Clipping属性的Alipha Clip 调节窗口大小,遮蔽地图试地图实现在屏幕的部分显示.此方法的好处是不用担心sha ...

  10. window.parent与window.opener的区别与使用

    window.parent 是iframe页面调用父页面对象 举例: a.html 如果我们需要在b.html中要对a.html中的username文本框赋值(就如很多上传功能,上传功能页在ifrma ...