UVa 10714:ants

Source: Internet
Author: User
Tags integer integer numbers min


Http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=113&page=show_ problem&problem=1655

Original title:

A army of ants walk on a horizontal pole of length l cm, each with a constant speed of 1 cm/s. When a walking ant reaches the pole, it immediatelly falls off it. When two ants meet they turn back and start walking in opposite. We know the original positions of ants on the pole, unfortunately, we don't know the directions in which the ants are King. The Your task is to compute the earliest and the latest possible times needed for all ants to the fall.

The ' A ' of input contains one integer giving the number of cases that follow. The data for each case start with two integer numbers:the length of the pole (in cm) and N, the number of ants residing o n the pole. These two numbers are followed by n integers giving the position of all ant on the pole as the distance measured from the The pole, in no particular order. All input integers are not bigger than 1000000 and they are by separated.

For each case of input, output two numbers separated by a. The "The" is the "earliest possible time" all ants fall off the pole (if the directions of the their walks are chose n appropriately) and the second number is the latest possible such time.

Sample input

2 6 7
214 7
11 12 7 13 176 23 191

Output for sample input

4 8
38 207

The main effect of the topic:

There are ants on a stick, and the ants are walking in one direction at the same speed (one unit length per second), and once the ants are in the same direction as the ants, the two ants will go in opposite directions immediately. Ask for the minimum and maximum time that all ants will walk down the stick.

Analysis and Summary:

For each ant, it is above the stick, the distance between the left and right endpoints may be equal or different. For the time being, the far end is called the distal end, and the near end is called the proximal one.

So in order to make all the time the shortest, so that all the ants are going to the near end, the final total depends on all the ants near the farthest of the ant, its time is the minimum time spent.

The same can be pushed, to make it take the longest time, so that all the ants go to the far end, so that the last long time depends on all the farthest from the ant ant.

More Wonderful content: http://www.bianceng.cnhttp://www.bianceng.cn/Programming/sjjg/

You don't have to think about the two ants that meet, because the farthest away from the far end must be the ant on the right or the left, then when it goes in the direction of the distance, it may come up against the oncoming, then he goes back, and the opposite is also a walk, which is equivalent to the "relay" process, That's the ant on the other side for him. The journey that should have been done by him (the whole process may have countless "relays", but the end result is the same, but this is the farthest distance into a lot of ants together).

Description of a bit of a setback, slowly feel it.


 * *  uva:10714  Ants * result:accept * 
 time:0.072s *  author:d_double 
 * /
#include <iostream>  
#include <cstdio>  
#include <cstring>  
using namespace STD;  
inline int max (int a,int b) {return a>b?a:b;}  
inline int min (int a,int b) {return a<b?a:b;}  
int main () {  
    int ncase, Len, N, Dist;  
    scanf ("%d", &ncase);  
    while (ncase--) {  
        scanf ("%d%d", &len, &n);  
        int minlen = -100, maxlen = -100;   
        for (int i=0; i<n; ++i) {  
            scanf ("%d", &dist);  
            MaxLen = Max (MaxLen, Max (Dist, len-dist));  
            Minlen = Max (Minlen, Min (Dist, len-dist));  
        printf ("%d%d\n", Minlen, MaxLen);  
    return 0;  

Author: csdn Blog shuangde800

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.