码迷,mamicode.com
首页 > 其他好文 > 详细

51Nod - 1266 蚂蚁

时间:2017-05-08 22:02:27      阅读:120      评论:0      收藏:0      [点我收藏+]

标签:cstring   http   online   names   line   示例   nod   png   img   

51Nod - 1266 蚂蚁

 
n只蚂蚁以每秒1cm的速度在长为Lcm的竿子上爬行。当蚂蚁爬到竿子的端点时就会掉落。由于竿子太细,两只蚂蚁相遇时,它们不能交错通过,只能各自反向爬回去。对于每只蚂蚁,我们知道它距离竿子左端的距离xi,但不知道它当前的朝向。请计算各种情况当中,所有蚂蚁落下竿子所需的最短时间和最长时间。
 
技术分享 
 
例如:竿子长10cm,3只蚂蚁位置为2 6 7,最短需要4秒(左、右、右),最长需要8秒(右、右、右)。
Input
第1行:2个整数N和L,N为蚂蚁的数量,L为杆子的长度(1 <= L <= 10^9, 1 <= N <= 50000)
第2 - N + 1行:每行一个整数A[i],表示蚂蚁的位置(0 < A[i] < L)
Output
输出2个数,中间用空格分隔,分别表示最短时间和最长时间。
Input示例
3 10
2
6
7
Output示例
4 8

 

题解: 

    看上去挺复杂的,但是追究其核心,蚂蚁相遇转向不能改变整体的爬行时间。只会将各自剩下的爬行时间相互交换。

 

 

#include <iostream> 
#include <cstdio> 
#include <cstdlib> 
#include <cstring> 
using namespace std; 
const int MAXN = 50005; 
int n, L, num[MAXN]; 
int main(){
	int min_val, max_val; 
	while(scanf("%d %d", &n, &L) != EOF){
		min_val = 0; 
		max_val = 0; 
		for(int i=0; i<n; ++i){
			scanf("%d", &num[i]); 
			if(min_val < min(num[i], L - num[i])){
				min_val = min(num[i], L - num[i]); 
			}
			if(max_val < max(num[i], L - num[i])){
				max_val = max(num[i], L - num[i]); 
			}
		}
		printf("%d %d\n", min_val, max_val );
	}
	return 0; 
}

  

 

51Nod - 1266 蚂蚁

标签:cstring   http   online   names   line   示例   nod   png   img   

原文地址:http://www.cnblogs.com/zhang-yd/p/6827526.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!