acm freshman season - Ants

1442: Ants

Time limit: 1 Sec   memory limit: 128 MB
submitted: 37   Resolution: 8
[ Submit ] [ state ] [ discussion Edition ]

Title Description

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)

Entry

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 line: Per a row integer a [i], the ant position (0 <a [i] <L)
 

Export

2 the number of output, separated by spaces, respectively, minimum time and maximum time.
 

Sample input

3 10 
2 
6 
7 

Sample Output

4 8

 

 

 

Solution: When two ants collide, both sides turn around and directly across from the results and there is no difference.

code show as below

 

#include<iostream>
using namespace std;
int main()
{
int i,j,n,l,p,q;
p=q=0;
cin>>n>>l;
int a[n];
for (i=0;i<n;i++)
{
cin>>a[i];
j=l-a[i];
if (q<a[i])
q=a[i];
if (q<j)
q=j;
if (j>a[i])
j=a[i];
if (p<j)
p=j;
}
cout <<p<<' '<<q;
return 0;
}

Guess you like

Origin www.cnblogs.com/false0/p/11616512.html