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

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>
#include <algorithm>
#include <cmath>
using namespace std; int main()
{
int test;
cin>>test; while(test--)
{
int length,ant;
int min_val=-1,max_val=-1;
cin>>length>>ant; while(ant--)
{
int dis;
cin>>dis;
min_val=max(min_val,min(dis,length-dis));
max_val=max(max_val,max(dis,length-dis));
}
cout<<min_val<<" "<<max_val<<endl;
}
return 0;
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

POJ 1852:Ants的更多相关文章

  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. .NET性能调优之一:ANTS Performance Profiler的使用

    .NET性能调优系列文章 系列文章索引 .NET性能调优之一:ANTS Performance Profiler的使用 .NET性能调优之二:使用Visual Studio进行代码度量 .NET性能调 ...

  4. POJ 3321:Apple Tree + HDU 3887:Counting Offspring(DFS序+树状数组)

    http://poj.org/problem?id=3321 http://acm.hdu.edu.cn/showproblem.php?pid=3887 POJ 3321: 题意:给出一棵根节点为1 ...

  5. poj 1852&3684 题解

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

  6. POJ 3252:Round Numbers

    POJ 3252:Round Numbers Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 10099 Accepted: 36 ...

  7. 弹性碰撞问题:Ants+Linear world

    题目一:Ants 传送门 题目描述 输入 输出 样例 样例输入 样例输出 分析 一句话题意:有n只蚂蚁在木棍上爬行,每只蚂蚁的速度都是每秒1单位长度,现在给你所有蚂蚁初始的位置(蚂蚁运动方向未定),蚂 ...

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

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

  9. Ants (POJ 1852)

    题目描述: Description An army of ants walk on a horizontal pole of length l cm, each with a constant spe ...

随机推荐

  1. Mysql 锁定 读情况

    在一个事务中,标准的SELECT语句是不会加锁,但是有两种情况例外. SELECT ... LOCK IN SHARE MODE SELECT ... FOR UPDATE SELECT ... LO ...

  2. 3 (mysql实战) 事务隔离

    提到事务,你肯定不陌生,和数据库打交道的时候,我们总是会用到事务.最经典的例子就是转账,你要给朋友小王转 100 块钱,而此时你的银行卡只有 100 块钱. 转账过程具体到程序里会有一系列的操作,比如 ...

  3. JS拖拽-面向对象拖拽-继承

    //普通拖拽 /* * CSS */ #div1{ width:100px; height:100px; position:absolute; background:red; cursor:move; ...

  4. MyBatis 配置文件详解

    根元素<configuration>,子元素: <properties> <setttings> <typeAliases> <typeHandl ...

  5. 2.Git知识

    Git安装 Git在不同的操作系统上安装是不一样的,这里只讲解我们需要的,也就是在Windows下的安装,主要安装msysGit和TortoiseGit 安装msysGit,到https://code ...

  6. day6 作业 购物车

  7. springmvc教程(1)

    idea搭建springmvc maven项目 jdk:1.8 maven:Bundled (Maven 3) idea版本: 开始搭建第一个springmvc maven项目 1.点击File-&g ...

  8. Python数据分析在互联网寒冬下,数据分析师还吃香吗?

    伴随着移动互联网的飞速发展,越来越多用户被互联网连接在一起,用户所积累下来的数据越来越多,市场对数据方面人才的需求也越来越大,由此也带火了如数据分析.数据挖掘.算法等职业,而作为其中入门门槛相对较低. ...

  9. Linux之文件传输

    本文借鉴<Linux命令大全> 1. bye命令 功能:终端FTP连线并结束程序 语法:bye 补充:在ftp模式下,输入bye即可中断目前的连线作业,并结束ftp的执行. 2. ftp命 ...

  10. vb.net导出CSV文件

    Public Function WriteToCSV(ByVal dataTable As DataTable, ByVal filePath As String, ByVal records As ...