POJ 1852 ANTS

Source: Internet
Author: User
Tags integer numbers min
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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.