10714-ants (Greedy)

Source: Internet
Author: User


Title: 10714-ants


On a board with a length of L, there are a lot of ants distributed. The positions of each ant will be given, but the direction will not be set. If the two ants meet, in the opposite direction. Ask the shortest time and longest time for all the ants to fall onto the board.


Solution: If the shortest time is used, each ant will move toward the nearest two ends. Finally, the shortest time is used to obtain the maximum value of the nearest position. In this case, the two ants will not be met, because the ant in front of an ant is either in the same direction or in the opposite direction; the ant in the latter is also in the same situation. So it won't happen.

The longest time is actually the time spent by the ant, which is the farthest from the two ends. Although the two ants may encounter this problem, if the two ants encounter this problem, they will move in the opposite direction, in this way, the ant who met him helped him finish the road he was going to take, which is equivalent to a relay. (It can be like this many times, but eventually it is still the distance that the ant wants to take from both ends, but it is just the end of the relay ).


Code:

# Include <stdio. h> const int n = 1000005; int s [N]; int max (const int X, const int y) {return x> Y? X: Y;} int min (const int X, const int y) {return x <Y? X: Y;} int main () {int t; int L, N; scanf ("% d", & T); While (t --) {scanf ("% d", & L, & N); For (INT I = 0; I <n; I ++) scanf ("% d ", & S [I]); int minlen, maxlen; minlen = maxlen =-1; for (INT I = 0; I <n; I ++) {minlen = max (minlen, min (s [I], L-s [I]); maxlen = max (maxlen, max (s [I], l-S [I]);} printf ("% d \ n", minlen, maxlen);} 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.