poj1852

说了很久了,来填坑了,刷《挑战程序设计竞赛》顺便写下题解 =_=

本文地址:https://www.cnblogs.com/maplefighting/p/9102839.html 

题目名称:Ants

链接:http://poj.org/problem?id=1852

题意:n只蚂蚁在Lcm长度棍子上爬行,速度1cm/s,到两端时会掉下。每两只蚂蚁相遇时会反向爬回去。对于每只蚂蚁,我们知道它离棍子左端距离xi,但是不知道朝向,计算所有蚂蚁掉下去最短时间与最长时间。

思路:算是思维题吧,因为每只蚂蚁相遇后反向爬行,如果我们不理它,让它直接交错前进也不会有任何问题,知道这个就简单了,对每只蚂蚁左右判断下就行了。

代码如下:

 1 #include<cstdio>
 2 #include<iostream>
 3 using namespace std;
 4 int main(){
 5     int t;
 6     scanf("%d", &t);
 7     while(t--){
 8         int n, p;
 9         scanf("%d%d", &p, &n);
10         int mx=0, mi=0, s;
11         for(int i = 1; i <= n; i++){
12             scanf("%d", &s);
13             mx=max(mx, max(s, p - s));
14             mi=max(mi, min(s, p - s));
15         }
16         printf("%d %d\n",mi, mx);
17     }
18     return 0;
19 }
View Code

 

猜你喜欢

转载自www.cnblogs.com/maplefighting/p/9102839.html