Ants
Time Limit: 1000MS |
|
Memory Limit: 30000K |
Total Submissions: 21297 |
|
Accepted: 8744 |
Description An army of ants walk in 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 the ants meet they turn back and the start walking in opposite directions. We know the original positions of ants on the pole, unfortunately, we don't know the directions in which the ants is Wal King. 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 both integer numbers:the length of the pole (in cm) and N, the number of ants residing o n the pole. These numbers is followed by n integers giving the position of all ant on the pole as the distance measured from the Left end of the pole, in no particular order. All input integers is not bigger than 1000000 and they is separated by whitespace.
Output for each case of input, output of the numbers separated by a single space. The first number is the earliest possible time, all ants fall off, the pole (if the directions of their walks are chose n appropriately) and the second number is the latest possible such time.
Sample Input
2
3
2 6 7
214 7
11 12 7 13 176 23 191
Sample Output
4 8
38 207
The general meaning is the ant to walk the pole, two ants meet will walk backwards, walk to the head will fall down, ask all ants fall to the maximum and minimum time.
Thinking topic, it is necessary to think of a point is two ants at the meeting when the distance of the opposite walk and two ants straight ahead of the same distance, that is, two ants in the back of the meeting when the opposite walk can be seen as two ants to keep the original direction to continue to walk, and so on, the problem will be converted to each ant is the longest So the shortest time for each ant to take the shortest distance in the collection of the maximum, the longest time for each ant to take the longest distance set of the maximum value.
The code is as follows
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <cstring>
using namespace std;
int a[1000005];
int Min (int a,int b)
{
return a<b?a:b;
}
int Max (int a,int b)
{
return a>b?a:b;
}
int main ()
{
int n;
int l;
int x;
scanf ("%d", &x);//There are several sets of data that need to be computed while
(x--)
{
scanf ("%d%d", &l,&n);//length and number of groups
int mit=0 , mat=0;//maximum time and minimum time for
(int i=0; i<n; i++)
{
scanf ("%d", &a[i]);
Mit=max (Mit,min (A[i],l-a[i]));//Find the minimum time
Mat=max (Mat,max (A[i],l-a[i]));//Max Time
}
printf ("%d%d\n", Mit,mat);
}
return 0;
}
One of the problems to be aware of is that this problem is timed out with CIN and can be passed with scanf.