poj 1852
来源:互联网 发布:金融 大数据 微线索 编辑:程序博客网 时间:2024/05/19 15:41
这道题不需要什么著名算法,只要注意一点:蚂蚁相遇后转向或直接爬过对时间没有影响。
基于上面这个事实,最短时间就是蚂蚁距边缘的最短距离的最大值,最长时间就是蚂蚁距边缘的最长距离的最大值。
此外,还可以参考《挑战程序设计竞赛》 人民邮电出版社 P18对此题的详解。不过实话说,我真的想不到!
代码(C++):
#include <iostream>#include <cstdio>using namespace std;int main(){ //freopen("in.txt","r",stdin); int t,len,n,i,manT,minT,dis; scanf("%d",&t); while(t--) { scanf("%d %d",&len,&n); manT=minT=0; for(i=0;i<n;i++) { scanf("%d",&dis); manT=max(manT,max(dis,len-dis)); minT=max(minT,min(dis,len-dis)); } printf("%d %d\n",minT,manT); } return 0;}
题目(http://poj.org/problem?id=1852):
Ants
Time Limit: 1000MS Memory Limit: 30000K
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
0 0
- poj 1852
- POJ---1852
- POJ-1852
- poj 1852
- poj 1852
- poj 1852
- poj-1852
- POJ 1852
- POJ 1852
- 1852poj
- poj-1852
- POJ 1852
- poj 1852
- poj(1852)
- POJ 1852
- poj 1852 Ants
- POJ 1852 Ants
- poj 1852Ants
- Candies
- 进程和线程有什么区别
- CGI入门
- 微乐是什么、微乐怎么赚钱、微乐邀请码、微乐是传销吗?
- android:configChanges属性
- poj 1852
- Node.js+socket.io简单在线聊天
- Caused by: java.lang.NoSuchMethodError: javax.servlet.http.HttpServletRequest.getServletContext()Lja
- QML类型说明-Colorize
- C#调用WebService并解析返回的XML
- 给SAP登录界面添加文字,附截图
- 多线程编程3.3-----synchronzied关键字为类名.class
- Android的Touch事件处理机制
- 左连接、右连接、交叉连接、全外连接