POJ1852 Ants

Ants
Time Limit: 1000MS Memory Limit: 30000K
Total Submissions: 11910 Accepted: 5215

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

2
10 3
2 6 7
214 7
11 12 7 13 176 23 191

Sample Output

4 8
38 207

Source

Waterloo local 2004.09.19


分析:

①最短时间只需要找到位置最靠近杆中点的两个点,其中有陷阱(如果所有点都在中点的一侧),所以要求出这两个点到左右左右两边的最小距离的最大值。


②最长时间就是要看最后一只掉下去的Ant在棒子上呆的时间了:先给出几个结论:

a.最后一只蚂蚁一定是最中间的那一只;

b.最长时间情况下,这只蚂蚁会与其左右两边的蚂蚁碰面n-1次;

c.每次与其两边的蚂蚁碰面一次相当于把其最左边(右边)的那只蚂蚁赶下去。想象一下中间那只蚂蚁对两边使用隔山打牛,只有最边上的蚂蚁会接招,其他的蚂蚁只不过是在传递“能量”。这一段时间是可以推倒出来的,最终会得到一个常量(xr-xl)/2;也就是左右两边蚂蚁初始距离的一半,这就是赶走每一只蚂蚁的时间。

d.最后最后的那只蚂蚁自己也要下去了,由于他赶走别人的时间是常量,所以如果我们最终能够推倒出他最后的位置的话,那么我们就可以自主选定一个方向,使他从这开始往这个方向走的时间最长才掉下去,由此我们还要得出中间的蚂蚁最后会在哪一个位置。发现最终位置也是可以推倒的,就是x=(xr+xl)/2;由此只需要计算max(x, len-x)即可。


我的代码:

#include <iostream>
#include <algorithm>
using namespace std;

//[x, y)
int max(int x, int y) { return x > y ? x : y; }
int min(int x, int y) { return x < y ? x : y; }

const int Maxn = 1000000;
int pos[Maxn];

//make mistake : if all of ants is at one side of povit.
int mini(int len, int n)
{
int x = 0, y = n - 1;
int povit = len / 2;

while(x + 1 < y)
{
int m = (x + y) / 2;
if(pos[m] > povit) y = m;
else x = m;
}

//y must at the right of povit.
return max(min(len - pos[y], pos[y]), min(pos[x], len - pos[x]));
}

int main()
{
int t, l, n, i;
cin>>t;


while( t-- )
{
cin>>l>>n;
for(i = 0; i < n; i++) cin>>pos[i];
sort(pos, pos + n);
cout<<mini(l, n)<<" "<<max(pos[n-1], l - pos[0])<<endl;
}


return 0;
}


两次解决的,由于忽视了那个陷阱。

最后我有发现了一个更好的解决方案,其实我没有必要去开那个数组,方案如下。


  1. #include<iostream>  
  2. #include<cstring>  
  3. #include<algorithm>  
  4. #include<cstdio>  
  5.   
  6. using namespace std;  
  7.   
  8.   
  9. int main()  
  10. {  
  11.     int n,l,cs,mx,mi,pos;  
  12.     cin>>cs;  
  13.     while(cs--)  
  14.     {  
  15.         mx=0,mi=0;  
  16.         cin>>l>>n;  
  17.         for(int i=0;i<n;i++)  
  18.         {  
  19.             cin>>pos;  
  20.             mx=max(mx,max(pos,l-pos));  
  21.             mi=max(mi,min(pos,l-pos));  
  22.         }  
  23.         cout<<mi<<" "<<mx<<endl;  
  24.     }  
  25.     return 0;  
  26. }  

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值