poj 1852 Ants
来源:互联网 发布:如何制作淘宝首页模板 编辑:程序博客网 时间:2024/06/02 02:33
Ants
Time Limit: 1000MS Memory Limit: 30000KTotal Submissions: 18552 Accepted: 7808
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
分析:这个题目看起来很棘手,数据太大,没法模拟,没法排序。但是可以忽略过程,找到决定最短时间和最长时间的关键点。
详见代码:
#include<iostream>#include<cstdio>#include<algorithm>using namespace std;int main(){int t;int L,N;int data;int MIN,MAX;scanf("%d",&t);while(t--){MAX=-1;MIN=-1;scanf("%d%d",&L,&N);for(int i=0;i<N;i++){scanf("%d",&data);MAX=max(MAX,max(data,L-data));//在所有的最大距离中取一个最大值。 MIN=max(MIN,min(data,L-data));//在所有的最小距离中取一个最小值。 }printf("%d %d\n",MIN,MAX);}return 0;}
阅读全文
0 0
- poj 1852 Ants
- POJ 1852 Ants
- poj 1852Ants
- POJ 1852 Ants 蚂蚁
- POJ 1852 Ants
- POJ 1852 Ants
- POJ-1852-Ants
- POJ 1852 Ants
- [ACM] POJ 1852 Ants
- POJ 1852 Ants
- POJ 1852 Ants 模拟
- POJ 1852 ANTS
- poj 1852 ants
- POJ 1852 Ants
- POJ 1852 Ants
- POJ 1852 Ants
- POJ 1852 Ants
- Ants(POJ 1852)
- ueditor和xss的二三事
- Linux基础—目录结构及相应用途
- Android深入理解JNI(二)类型转换、方法签名和JNIEnv
- 尼科彻斯定理
- 单链表的基本操作
- poj 1852 Ants
- React Native 学习笔记(二)
- Objective-C中的协议(protocol)与委托
- 机器学习基础,入门介绍
- 欢迎使用CSDN-markdown编辑器
- java对象 父对象 的 对应关系
- 有序的数组中找到某一目标值首次出现的下标
- JavaScript Array 对象的方法
- sublime text3格式化插件HTML-CSS-JS Prettify