【POJ】1852 - Ants(数学问题,思路)
来源:互联网 发布:ndcg优化 编辑:程序博客网 时间:2024/05/16 10:41
Ants
Time Limit: 1000MS Memory Limit: 30000KTotal Submissions: 13903 Accepted: 6063
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
210 32 6 7214 711 12 7 13 176 23 191
Sample Output
4 838 207
Source
Waterloo local 2004.09.19
题意:一根竹竿上有若干蚂蚁,蚂蚁在竹竿任意一头都会掉下去,现在给你各个蚂蚁距离竹竿左边的距离,但是不知道蚂蚁的行进方向,若蚂蚁相遇则各自掉头往回走。
思路很不错啊。
最少时间能想到,就是所有蚂蚁所需要的最短时间(向左走向右走总有一个最短的时间),这些时间中取最大值就行了(保证所有蚂蚁都能掉下去)。
最长时间其实不难想,只是当时纠结到蚂蚁如何调头的问题上了,上午看题解想通了,其实蚂蚁相遇的时候不考虑调头的问题,就当没事发生一样,各走各的路,反正又不是问哪只蚂蚁最后掉下去,不必理会蚂蚁各自的方向,走就行了。
代码如下:
#include <cstdio>int max (int a,int b){return a > b ? a : b;}int min (int a,int b){return a < b ? a : b;}int main(){int u;int l,n;int ans_min,ans_max;scanf ("%d",&u);while (u--){scanf ("%d %d",&l,&n);ans_min = -1;ans_max = -1; for (int i = 1 ; i <= n ; i++){int t;scanf ("%d",&t);ans_min = max (ans_min,min(t,l-t));ans_max = max (ans_max,max(t,l-t));}printf ("%d %d\n",ans_min,ans_max);}return 0;}
0 0
- 【POJ】1852 - Ants(数学问题,思路)
- POJ 1852 Ants(思路题)
- poj Ants 1852 (简单数学规律)
- poj 1852Ants问题
- Ants(POJ 1852)
- POJ 1852 Ants (贪心)
- POJ 1852 Ants(机智)
- poj 1852 脑洞大开(Ants)
- POJ 1852 Ants (贪心)
- POJ 1852 Ants(思维)
- poj 1852 Ants(贪心)
- Ants(POJ NO.1852)
- POJ 1852 Ants(弹性碰撞问题)
- poj 1852 Ants 发现问题,解决问题
- uva - 10714 - Ants(数学推导、蚂蚁问题)
- Ants(POJ No.1852) 脑筋急转弯
- poj 1852 ants(模拟+想象力)
- POJ 1852 Ants (思维技巧 + 贪心)
- 查看Linux是32位还是64位
- 【ssh-keygen】生成、管理和转换认证密钥
- java 回调函数
- 彭鑫博士:黑芝麻补精粥
- css中float和clear属性
- 【POJ】1852 - Ants(数学问题,思路)
- HDOJ 3836 Equivalent Sets
- Redis运行多个实例以充分发挥多核处理器的能力
- win7开机后svchost..exe占用CPU和内存很高的解决方法
- 工控系统蜜罐建设与协议仿真技术分享
- 用CornerStone配置SVN,HTTP及svn简单使用说明
- 【awk】文本处理
- 常用正则表达式
- POJ-2752(KMP(Seek the Name, Seek the Fame))