BNU 26579 Andrew the Ant issue

Source: Internet
Author: User
Tags time 0

Time Limit: 4000 msMemory Limit: 65536KB64-bit integer IO format: % lld Java class name: MainPrev Submit Status Statistics Discuss NextFont Size: Type: Andrew the Ant is fascinated by the behavior of his friends. thousands of them are marching their paths on and on. they can build highly organized ant-hills. sometimes, however, they act a little bit stupidly. recently, Andrew watched his fellow ants m Arching on top of a long piece of wood. he noticed their behavioral pattern is very simple: Each ant walks slowly forward with a constant speed of 1 cm per second. whenever it meets another ant, both of them only touch with their antennae and immediately turn around and walk the opposite direction. if an ant comes to the end of the wood, it falls down and does not affect other ants anymore. the pi Cture above shows an example of moving ants in time 0 s. in one second, the ants E and A meet at position 2 and change their directions. the ant A then meets B in the next 1.5 seconds. at the same time (2.5 seconds after the start), the ants C and D will meet too. all four of them change their directions ctions. in the next 0.5 second (time 3 s), the first ant (E) falls down off the left end, etc. your t Ask is to simulate the movement of ants. for simplicity, suppose that the ants have zero size (although the picture cocould suggest something else ). input The input consists of several scenarios. each scenario starts with a line containing two integer numbers L and A, separated by a space. L is the length of the wood in cms (1 ≤ L ≤ 99 999), and A is the number of ants at the beginning of the simula Tion (1 ≤ A ≤ L + 1 ). then there are A lines, each containing a positive integer Xi, one space, and an uppercase letter. the number (0 ≤ Xi ≤ L) specifies the position of the I-th ant and the letter its initial ction: either "L" for left (towards zero) or "R" for right. no two ants will start at the same position. output For each scenario, you shoshould print a single line containing the text "Th E last ant will fall down in T seconds-started at P. ", where T is the exact time when the last ant (or two) reaches the end of the wood, and P is the position where that particle ant has originally started in time 0. if two last ants fall down at the same time, print "started at P and Q", indicating both of their positions, P <Q. sample Input90000 10 R10 10 L14 53 L6 L13 L8 R1 RSample OutputTh E last ant will fall down in 90000 seconds-started at 0.The last ant will fall down in 0 seconds-started at 0.The last ant will fall down in 13 seconds-started at 6 and 8. hint (The last sample input scenario corresponds to the picture .) sourceCTU Open Contest 2012 meaning: the position of an ant and Its walking direction are given. If they touch each other, they both turn. If they reach the edge, they fall down and take one cell every second, ask when the last (or two) Ant financial fell down and where it was originally located. Train of Thought: (refer to my classmates) (on the background of A question of Liu rujia) Ant regards it as A particle, with A going left and E going right. Obviously After the collision with E, E goes left, and E's left walk is equivalent to A continuation of A's left walk. Similarly, A is like E's continuation, after n seconds, the positions of each ant are equal to those of every ant who does not turn to the positions after n seconds. However, at this time, only all the positions are determined, but I am not sure about the positions; in fact, the relative sequence of each ant is not changed because of the collision. The initial positions of the five ant are 1, 6, 8, and 13, respectively, then, when the five ants in the next three seconds are located at 4 0 3 5 10 (0 3 4 5 10), and finally determine which of the five ants are located, according to the original relative order, we know that E is 0, A is 3, B is 4, D is 5, and C is 10. If you want to find the time when the last one falls, then you need to find the ant on the rightmost to the left and the ant on the leftmost to the right. Just compare them. The code is my [cpp] # include <stdio. h ># include <algorithm> using namespace std; struct haha {int s; char flag;} ant [100000 ]; Int aa [100000]; int cmp (struct haha a, struct haha B) {return. s> B. s;} int main () {int I, len, n, time, left, right, cnt, n1, n2; while (scanf ("% d ", & len, & n )! = EOF) {right = len; left = 0; for (I = 0; I <n; I ++) {scanf ("% d % c ", & ant [I]. s, & ant [I]. flag);} sort (ant, ant + n, cmp); for (I = n-1; I> = 0; I --) if (ant [I]. flag = 'R') // run {right = ant [I] to the right. s; break;} for (I = 0; I <n; I ++) if (ant [I]. flag = 'l') // run {left = ant [I] to the left. s; break;} time = len-right> left? Len-right: left; for (I = 0; I <n; I ++) if (ant [I]. flag = 'R') aa [I] = ant [I]. s + time; else aa [I] = ant [I]. s-time; sort (aa, aa + n); cnt = 0; for (I = 0; I <n; I ++) {if (aa [I] = 0 | aa [I] = len) {cnt ++; if (cnt = 1) n1 = ant [n-1-i]. s; if (cnt = 2) n2 = ant [n-1-i]. s ;}} if (cnt = 1) printf ("The last ant will fall down in % d seconds-started at % d. \ n ", time, n1); else printf (" The last ant will fall down in % d seconds-started at % d and % d. \ n ", time, n1, n2);} 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.