【每日刷题】Ants

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/sd4567855/article/details/86659440

day27, Ants

题目来源:poj
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

解答:这道题是一道思维题。
考虑需要的最短时间,取所有蚂蚁都朝向较近的端点走。
考虑需要的最长时间,假象当两只蚂蚁相遇之后,它们不会转向而是继续前进,这样对结果并没有影响。因此最长时间为蚂蚁到端点的最大距离。

代码:

//吐槽,poj竟然还不支持<bits/stdc++.h>
int main(){
    int total;
    scanf("%d", &total);
    while( total--){
        int length, number;
        cin>>length>>number;
        int data;
        int result_min = 0, result_max = 0;
        while( number--){
            cin>>data;
            result_min = max(result_min, min( data, length - data));
            result_max = max(result_max, max( data, length - data));
        }
        cout<<result_min<<' '<<result_max<<endl ;
    }
return 0;}

运行结果:image.png-19.9kB


我的微信公众号

在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/sd4567855/article/details/86659440