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 kno

来源:学生作业帮助网 编辑:作业帮 时间:2024/04/28 03:00:40
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 kno

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 kno
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.
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.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
#include
using namespace std;
int main()
{
int n;
double num[100];
cin>>n;
for(int i=1;i>a,b;
for(int t=1;t>e;
num[t]=e;
}
double c=0;
double d=1000000;
double m=a/2;
for (int t=1;tnum[t])
num[t]=m-num[t];
else num[t]=num[t]-m;
}
for (int t=1;tc)
c=num[t];
}
for (int t=1;t

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 kno
虽然当蚂蚁数量很多的时候情况会有很多种,但是先考虑小数量的分析就可以找到解决方法:如果只有两只的话,那么最短时间就是两只蚂蚁距离两端点距离较小的距离中取大者就是所需最短时间,而最长时间就是两只蚂蚁距离两端点距离较大者中取大者就是所需最长时间,例如,长度为10,一只在距离左端2的位置,一只在距离左端6的位置,则最短时间为max(min(2,10-2),min(6,10-6))为4,最长时间为max((max(2,10-2),max(6,10-6)))为8其实就是两只相向而行,当相遇后,都转为逆向,则时间为从相遇点到端点距离大者与相遇前所需时间,分析实际就是2到10的距离,当蚂蚁数量增加时,情况相同
则需要时间最长的的就是让距离端点最近的蚂蚁爬到另一个端点(最远)所需要的时间.
也就是说,只要找出所有蚂蚁与较远端比较,然后找出最大值就是所需要的最大时间
在距离短的一边中求出最大的一边.就能找到距离端点最远的蚂蚁需要爬到端点的时间
AC代码:
#include<iostream>
#include<cstdio>
using namespace std;
#define Max(a,b) a>b?a:b;
int main()
{
    int T,m,n,a,b,k;
    scanf("%d",&T);
    while(T--)
    {
        scanf("%d%d",&m,&n);
        for(a=b=0;n--;)
        {
            scanf("%d",&k);
            k=Max(m-k,k);    //距离最长的一边
            b=Max(b,k);        //最长的距离所需要的时间也最大
            a=Max(a,m-k);    //m-是距离最短的一边
        }
        printf("%d %d\n",a,b);
    }
    return 0;
}