1266 Ant

N ants crawl on a pole of length Lcm at a speed of 1cm per second. When the ant climbs to the end of the pole, it will drop. Because the pole is too thin, when two ants meet, they cannot pass through each other, and can only climb back in opposite directions. For each ant, we know its distance xi from the left end of the pole, but not its current orientation. Calculate the minimum and maximum time required for all the ants to drop their poles in each case.
 
 

For example: the length of the pole is 10cm, the position of 3 ants is 2 6 7, the shortest takes 4 seconds (left, right, right), and the longest takes 8 seconds (right, right, right).
Input
Line 1: 2 integers N and L, N is the number of ants, L is the length of the pole (1 <= L <= 10^9, 1 <= N <= 50000)
Lines 2 - N + 1: One integer A[i] per line, representing the ant's position (0 < A[i] < L)
Output
Output 2 numbers, separated by spaces, representing the shortest time and the longest time respectively.
Input example
3 10
2
6
7
Output example
4 8

The most important point is that after the two ants meet each other, they turn around, which can be seen as passing through each other's body.

The direction is uncertain, the ants can go left or right.

Therefore, the shortest time is the maximum of the shortest times from each ant to the two ends of the stick, and the longest time is the maximum of the longest time from each ant to the two ends of the stick.

#include <iostream>

using namespace std;

intmain()
{
    int n, l;
    int Min, Max;
    cin >> n >> l;
    int d;
    cin >> d;
    Max = max(d, l-d);
    Min = l-Max;

    int temp1, temp2;
    for(int i=2; i<=n; i++) {
       cin >> d;
       temp1 = max(ld, d);
       temp2 = l-temp1;
       if(temp1 > Max) {
          Max = temp1;
       }
       if(temp2 > Min) {
          Min = temp2;
       }
    }
    cout << Min << " " << Max;
}






Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324445679&siteId=291194637
ANT
ANT