题目描述:

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

代码如下:

  1. #include<iostream>
  2. int Max(int,int);
  3. int Min(int,int);
  4. int main()
  5. {
  6. using namespace std;
  7. int i;
  8. cin >> i;
  9. while(i--)
  10. {
  11. int l;
  12. int num;
  13. cin >> l >> num;
  14. int* pt = new int [num];
  15. for(int j = ;j < num;j++)
  16. cin >> pt[j];
  17. int min_ = ,max_ = ;
  18. for(int j = ;j < num;j++)
  19. min_ = Max(min_,Min(pt[j],l - pt[j]));//最小时间是蚂蚁爬向最近一端
  20. for(int j = ;j < num;j++)
  21. max_ = Max(max_,Max(pt[j],l - pt[j]));//无视不同蚂蚁的区别,可以认为是保持原样交错而过
  22. cout << min_ << " " << max_ << endl;
  23. delete [] pt;
  24. }
  25. return ;
  26. }
  27.  
  28. int Max(int a,int b)
  29. {
  30. return a > b ? a : b;
  31. }
  32.  
  33. int Min(int a,int b)
  34. {
  35. return b > a ? a : b;
  36. }

代码分析:

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

参考书籍:[挑战程序设计竞赛第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. iOS 首次启动画面,新装或更新用户可以通过它查看简介。

    // // GuideViewController.h // Guide // // Created by twb on 13-9-17. // Copyright (c) 2013年 twb. Al ...

  2. linux之vim配置

    代码自动补全和代码跳转阅读,应该是作为程序员最常用的功能之一了,具体二者是指什么我就不解释了.微软的Visual Studio就是靠这两样必杀技牢牢占据着广大windows程序员的心(这里面要有强大的 ...

  3. 嵌入式系统 Boot Loader

    基于嵌入式系统中的 OS 启动加载程序 ―― Boot Loader 的概念.软件设计的主要任务以及结构框架等内容.

  4. js秒数转换时分秒方法

    今天写一个东西的时候 发现给出的是秒数.实在找不到直接的工具去转换. 就去网上找了个转换方法(有现成的就不写了,以后再简化下代码). function formatSeconds(value) { v ...

  5. Android应用开发提高篇(4)-----Socket编程(多线程、双向通信)

    链接地址:http://www.cnblogs.com/lknlfy/archive/2012/03/04/2379628.html 一.概述 关于Socket编程的基本方法在基础篇里已经讲过,今天把 ...

  6. 视频(其他)下载+tomcat 配置编码+图片上传限制大小

    视频下载:前台 jsp function downVideo(value,row,index){ return '<a href="<%=basePath%>admin/v ...

  7. app微信支付服务器端php demo

    class Wxpay { /* 配置参数 */ private $config = array( 'appid' => "wxc92b12277f277355", /*微信 ...

  8. Git远程仓库的使用(三)

    1)git remote add : 添加远程仓库 git remote add origin git@github.com:用户名.仓库名.git 2) git push –u origin mas ...

  9. gdal 1.9+python 2.7开发环境配置

    最近项目使用Cesium平台基于WegGl做web地球,其中关于地形数据有一种支持格式为terrain的地形数据.这种格式可以通过一个python工具切dem来得到. 下面记录下配置gdal+pyth ...

  10. Primavera 6.0

    Primavera 6.0(原p3e/c)荟萃了P3软件20年的项目管理精髓和经验,采用最新的IT技术,在大型关系数据库Oracle和MS SQL Server上构架起企业级的.包涵现代项目管理知识体 ...