POJ 1852:ants Law

Source: Internet
Author: User
Tags integer numbers
Ants
Time Limit: 1000MS Memory Limit: 30000K
Total Submissions: 12914 Accepted: 5654

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 main idea: a lot of ants in the length of the L (CM) arm crawling, their speed is 1cm/s, to the stick terminal, the ants will fall down. If you encounter other ants while crawling, the direction of the two ants will be reversed. It is known that the ants are in the coordinates of the initial position of the stick, but we do not know which direction they will crawl. Ask for the shortest time and the longest time all ants fall down.

Problem Analysis: Although the number of ants when the situation will be many, but first consider a small number of analysis can find a solution: If only two, then the shortest time is two ants distance from the two ends of the distance of the smaller distances to take large is the shortest time required, And the longest time is two ants distance from the two ends of the larger person is the maximum time required, for example, the length of 10, one in the distance from the left side of the position of 2, a distance from the left side 6, the shortest time is Max (min (2,10-2), Min (6,10-6)) is 4, The maximum time for Max (Max (2,10-2), Max (6,10-6)) is 8 is actually two opposite to the line, when the encounter, all turned to reverse, then the time from the meeting point to the end of the distance between the large and meet the time required, the analysis is actually 2 to 10 distance, when the number of ants increased, the same situation

What takes the longest is the time it takes to get the nearest ant from the endpoint to the other end (farthest).
That is, just find out how all ants compare to the far end, and then find out the maximum is the maximum time required.
Find the biggest side on the short side of the distance. You can find the ants farthest from the end point. The time to crawl to the endpoint Ac-code:

#include <cstdio>
#define INF 0x3f3f3f3f
#define MIN (A, b) a>b?b:a
#define MAX (b) a<b?b:a
int main ()
{
	int t,n,m,a,mmin,k,mmax;
	scanf ("%d", &t);
	while (t--)
	{
		scanf ("%d%d", &n,&m);
		mmin=0;
		mmax=0;
		while (m--)
		{
			scanf ("%d", &a);
			K=min (n-a,a);
			Mmin=max (mmin,k);
			K=max (n-a,a);
			Mmax=max (mmax,k);
		}
		printf ("%d%d\n", Mmin,mmax);
	}
	return 0;
}


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.