Race 19 newborn ant problem

Description Title: n ant at a rate of 1cm in length per second on the pole Lcm crawling. When the endpoint of the ants climb up the pole will fall. Since the pole is too thin, when two ants meet, they can not be staggered by only reverse their climb back. For each ant, we know that the distance from the left end of the pole xi, but do not know its current orientation. Calculate among all cases, the minimum time required for all the ants falling pole and the longest time. For example: the pole length 10cm, 3 ants position 267, the minimum required 4 seconds (left, right, right), take up to eight seconds (right, right, right).
Input format: Line 1: two integers N and L, N is the number of ants, L is the length of the pole (1 <= L <= 10 ^ 9, 1 <= N <= 50000) of 2 - N + 1 lines: a line for each integer a [i], represents the position ants (0 <a [i] < L)
input format: output number 2, separated by spaces, respectively, minimum time and maximum time.
Sample
input 310 267
output 48

 

Code

#include<bits/stdc++.h>
using namespace std;
int a[50005];
int main() {
 int n,l;
 scanf("%d%d",&n,&l);
 for(int i=1; i<=n; i++) {
  scanf("%d",&a[i]);
 }
 int maxn=-1,minn=-1;
 for(int i=1; i<=n; i++) {
   maxn=max(maxn,a[i]);
  maxn=max(maxn,l-a[i]);     //取最大时间
  minn= max(minn,min(l-a[i],a[i]));      //取最小时间
 }
 printf("%d ",minn);
 printf("%d",maxn);
 return 0;
}
 
 
 
 
I began to think the problem is too complicated, in fact, do not think too much about the collision, the longest direct count the shortest time possible.
c language direct access to add max min max min using namespace std; header files
 

Guess you like

Origin www.cnblogs.com/QingyuYYYYY/p/11616227.html