Ultraviolet A 10714 ants (Greedy)

Source: Internet
Author: User

Question connection: 10714-ants


There are n ants on a stick. These ants can start to move in one direction, either left or right, at the same speed, however, once an ant encounters another ant, the two will immediately change their direction and move in the opposite direction. Find the minimum and longest time for all ants to leave the wooden stick.


Solution: in fact, the two ants in the question may be confused, because at first all the ants are moving in one direction, and the speed is the same, and it is impossible to catch up with each other, so you don't have to consider just one point. In this way, you only need to calculate the maximum time to left and right respectively. The maximum time is the maximum time, and the minimum time is the minimum time (which can be summed up)


# Include <stdio. h> int max (const Int & A, const Int & B) {return A> B? A: B;} int min (const Int & A, const Int & B) {return A> B? B: A;} int main () {int cas, N, Len, Max, Min, DIST; scanf ("% d", & CAS); While (CAS --) {max = min = 0; scanf ("% d", & Len, & N); For (INT I = 0; I <n; I ++) {scanf ("% d", & Dist); max = max (Max, Max (Dist, len-Dist); min = max (Min, min (Dist, len-Dist);} printf ("% d \ n", Min, max);} 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.