2376_ants(巧妙避开了申请大容量数组,思路上采用单个蚂蚁独立分析,而非将全部数据输入后才统一比较)
2014-06-16 22:26
423 查看
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
***************************************************************************************************************************************************************
/*
题目要点:
杆长一定,各只蚂蚁初始方向任意,蚂蚁速度均相同,到达杆边即坠落,两只蚂蚁
相遇立刻反相继续移动。
引理:
由于蚂蚁的速度都相同,且相遇之后马上反相继续移动,所以从开始到一特定时刻
所有蚂蚁的已行路程是相同的(除已经掉下横杆的)。
定理:
当任意两只蚂蚁相遇时,各自反相继续运动,相当于各自继续向前移动,但两者交
换各自已已行路程,由引理可知,在两只蚂蚁相遇这一时刻,两者已行路程是相等的,
由此得证,两只蚂蚁相遇后,各自继续向前与各自反相最终所行路程相等。(横杆宽度
不够等因素没有意义,所以忽略)。
回到题目,设杆长L,某只蚂蚁位置X,由于相遇不影响结果,求最长时间只需考虑那只可
以离边界最大值最大的蚂蚁,求最短时间只需考虑那只离边界最小值最大的蚂蚁,所以
最长时间为Max(n只蚂蚁的Max(X,L-X)),最短时间为Max(n只蚂蚁的Min(X,L-X))。
*/
//转载
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
***************************************************************************************************************************************************************
/*
题目要点:
杆长一定,各只蚂蚁初始方向任意,蚂蚁速度均相同,到达杆边即坠落,两只蚂蚁
相遇立刻反相继续移动。
引理:
由于蚂蚁的速度都相同,且相遇之后马上反相继续移动,所以从开始到一特定时刻
所有蚂蚁的已行路程是相同的(除已经掉下横杆的)。
定理:
当任意两只蚂蚁相遇时,各自反相继续运动,相当于各自继续向前移动,但两者交
换各自已已行路程,由引理可知,在两只蚂蚁相遇这一时刻,两者已行路程是相等的,
由此得证,两只蚂蚁相遇后,各自继续向前与各自反相最终所行路程相等。(横杆宽度
不够等因素没有意义,所以忽略)。
回到题目,设杆长L,某只蚂蚁位置X,由于相遇不影响结果,求最长时间只需考虑那只可
以离边界最大值最大的蚂蚁,求最短时间只需考虑那只离边界最小值最大的蚂蚁,所以
最长时间为Max(n只蚂蚁的Max(X,L-X)),最短时间为Max(n只蚂蚁的Min(X,L-X))。
*/
//转载
#include<iostream> using namespace std; int main() { int l,n; int t; int min,max; int r,s; cin >> l; while(cin >> l >> n) { min = -1; max = -1; for(int i = 0;i < n;i++) { cin >> t; r = l - t; if(r > t) s = t,t = r,r = s;//如此操作后:r为此蚂蚁到两端的最短距离,t为最长距离 if(min < r)//求n个蚂蚁r的最大值 min = r; if(max < t)//求n个蚂蚁t的最大值 max = t; } cout << min << " " << max << endl; } return 0; }
相关文章推荐
- 采用指针访问方式从键盘给数组a[N]输入数据,然后对元素值重新按逆序存放并输出
- 采用指针访问方式从键盘给数组a[N]输入数据,然后对元素值重新按逆序存放并输出 .
- JAVA学习心得--基本数据类型,输入、输出,数组
- 从键盘输入20个整数将奇数&偶数分别存在不同的数组中并按先奇数后偶数的顺序输出这两个数组中的数据
- 做项目时,如何做比较美观大方的数据输入窗体
- 蛇形矩阵的打印;开拓思路与设计方法的面试题;编程技巧比较巧妙;转自CSDN论坛;
- 界面输入值和数据库值进行比较
- 一份5000万独立用户的网站数据分析(http://home.donews.com/donews/article/8/88549.html)
- jQuery备忘之(一):jQuery处理.Net后台返回的Xml格式与Json格式的数据的比较分析
- 数组排序方法的性能比较(2):Array.Sort<T>实现分析
- 第二章 基本数据类型,输入、输出,数组
- 海量数据(数据量比较大时)的处理分析
- 从终端输入一组数据进行进栈、出栈操作并将其结果打印出来(采用顺序栈的方式)
- 标准功能模块组件 -- “文档管理组件,网络文档管理,网络存储”,B/S版本组件可独立运行,也可集成到其他项目里,数据结构清晰思路严谨
- 标准功能模块组件 -- “文档管理组件,网络文档管理,网络存储”,B\S版本组件可独立运行,也可集成到其他项目里,数据结构清晰思路严谨
- 数组排序方法的性能比较(3):LINQ排序实现分析
- 比较一个数组内的数据是否相等
- 内网安全数据保密技术分析与比较
- 不同数据整合方案的比较分析
- 海量数据(数据量比较大时)的处理分析