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

2
10 3
2 6 7
214 7
11 12 7 13 176 23 191

Sample Output

4 8
38 207
题解:水题,就是一个木棒在水里,蚂蚁在这个木棒上可以右走,也可以左走,1cm/s,问最小最大多长时间都掉到水里;
代码:
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
using namespace std;
const int MAXN=1000010;
int a[MAXN];
int main(){
int T,l,n,x,mx;
scanf("%d",&T);
while(T--){
scanf("%d%d",&l,&n);
mx=-1;
for(int i=0;i<n;i++){
scanf("%d",&x);
mx=max(mx,x);
mx=max(mx,l-x);
a[i]=min(x,l-x);
}
printf("%d %d\n",*max_element(a,a+n),mx);
}
return 0;
}

  

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. Hive学习之动态分区及HQL

    Hive动态分区 1.首先创建一个分区表create table t10(name string) partitioned by(dt string,value string)row format d ...

  2. classpath的总结

    转自:http://blog.csdn.net/javaloveiphone/article/details/51994268   版权声明:本文为博主原创文章,未经博主允许不得转载. 1.src不是 ...

  3. Notes常用事件整理

    ①      ボタンのクリック事件: Sub Click(Source As Button) Dim ws As New NotesUIWorkspace Dim uidoc As NotesUIDo ...

  4. zoj 2587 Unique Attack 最小割判定

    题目链接 让你判断最小割是否唯一. 判断方法是, 先求一遍最大流, 然后从源点dfs一次, 搜索未饱和边的数目. 从汇点dfs一次, 同样也是搜索未饱和边的数目, 看总和是否等于n. 如果等于n那么唯 ...

  5. LaTeX空格

    由于LaTeX 采用的是源文件编译方式,  默认LaTeX会忽略多余的空格, 如果需要产生一个空格,可以使用 命令 \, 注意代表的是空间键. 例如: Jones, et al.\  (1993), ...

  6. 【LeetCode题意分析&解答】41. First Missing Positive

    Given an unsorted integer array, find the first missing positive integer. For example,Given [1,2,0]  ...

  7. 通过web远程访问服务器的ipython

    如果想同过一个Web浏览器的方式远程访问服务器上的ipython notebook sever,可通过下面的步骤实现. 服务器:ubuntu14.04 server 客户端:windows/unix/ ...

  8. ECC(Error Checking and Correction)校验和纠错

    ECC的全称是 Error Checking and Correction or Error correction Coding,是一种用于差错检测和修正的算法.上一节的BBM中我们提到过,NAND闪 ...

  9. 害人的VS2008,manifest导致“应用程序配置不正确,应用程序未能启动”

    在VC++2008的项目中,如何显示地指定要使用的C++库的版本? 开发环境:VS2008 SP1 + Win2003 SP2         因为我的VS2008安装了SP1补丁,所以有了9.0.3 ...

  10. TCP的核心系列 — SACK和DSACK的实现(一)

    TCP的实现中,SACK和DSACK是比较重要的一部分. SACK和DSACK的处理部分由Ilpo Järvinen (ilpo.jarvinen@helsinki.fi) 维护. tcp_ack() ...