Links: http://acm.hust.edu.cn/vjudge/problem/25979
Analysis: N ants in the climb, will n ants distance from the left side of the stick from small to large sorted after their relative order is constant, because encounter another ant two ants will turn around, the ant is like a pinball back and forth, but the overall "U-turn" equivalent to "wear over", but the topic requires the input sequence output, So not all ants are the same, but also to recognize who is who, so if the ant 1 Initial state is (3,R) two seconds later in the position (5,R) will appear an ant but not necessarily ant 1, but can be sure (5,r) position of the ants relative to the other ant's order and the initial state of the relative order is the same , so the initial state and the state of T seconds after the POS from small to large order, the input order map to the initial state of the relative order, according to the initial relative order (equal to the final relative order) the state of the various ants after the t seconds, but also to deal with the collision situation, the final printout is good.
1#include <cstdio>2#include <algorithm>3 using namespacestd;4 5 Const intMAXN =10000+5;6 7 structAnt {8 intID, pos, dir;9 BOOL operator< (Constant& RHS)Const {Ten returnPOS <Rhs.pos; One } A } BEFORE[MAXN], AFTER[MAXN]; - - Const Chardirname[3][Ten] = {"L","Turning","R"}; the intORDER[MAXN]; - - intMain () { - intT; +scanf"%d", &T); - for(intKase =1; Kase <= T; kase++) { + intL, t, N; Ascanf"%d%d%d", &l, &t, &n); at for(inti =0; I < n; i++) { - CharChintX, D; scanf"%d%c", &x, &ch); d = (ch = ='L'? -1:1); -Before[i].id = i, Before[i].pos = x, Before[i].dir =D; -After[i].id =0, After[i].pos = x + t * d, After[i].dir =D; - } -Sort (before, before +n); in for(inti =0; I < n; i++) -Order[before[i].id] =i; toSort (after, after +n); + for(inti =0; I < n-1; i++) - if(After[i].pos = = After[i +1].pos) After[i].dir = After[i +1].dir =0; theprintf"Case #%d:\n", Kase); * for(inti =0; I < n; i++) { $ intnum =Order[i];Panax Notoginseng if(After[num].pos <0|| After[num].pos > L) printf ("Fell off\n"); - Elseprintf"%d%s\n", After[num].pos, Dirname[after[num].dir +1]); the } +printf"\ n"); A } the return 0; +}
UVa10881 Piotr ' s Ants (thinking)