Ant financial
There are n ants on a wooden stick with a length of 1 cm. Each ant crawls either left or right at a speed of 1 cm/second. When two ants collide, the two turn around at the same time (the turn-around time is negligible ). The initial position and orientation of each ant are given, and the position of each ant is calculated after T seconds.
Input Format:
The number of first behavior data groups. The first behavior of each group of data is 3 positive integers (L, T, n (0 <= n <= 10000). The following n rows describe the initial position of an ant, where, integer x indicates the distance (in centimeters) between the ant financial and the left end of the wooden stick. letters indicate the initial orientation (L indicates the left direction, and R indicates the right direction ).
Output Format:
For each group of data, n rows are output, and the positions and orientations of each ant are output in the input Order (Turing indicates a collision ). The ant that had dropped the wooden stick before the second T (not counted as it climbed to the edge of the wooden stick) output Fell off.
Sample input:
2
10 1 4
1 R
5 R
3 L
10 R
10 2 3
4 R
5 L
8 R
Sample output:
Case #1:
2 Turing
6 R
2 Turing
Fell off
Case #2:
3 L
6 R
10 R
Solution:
# Include <cstdio> # include <algorithm> using namespace std; const int maxn = 10000 + 5; const char dirName [] [10] = {"L", "Turing ", "R"}; int order [maxn]; // The entered I-th Ant is the left-th order [I] In the final state. Only Ant struct Ant {int id; // input order int p; // position int d; // orientation. -1: Left; 0: Turning; 1: Right bool operator <(const Ant & a) const {return p <. p ;}} before [maxn], after [maxn]; int main () {int K; scanf ("% d", & K); for (int ki = 1; ki <= K; ki ++) {int L, T, n; scanf ("% d", & L, & T, & n ); for (int I = 0; I <n; I ++) {int p, d; char c; scanf ("% d % c", & p, & c ); d = (c = 'l ')? -1: 1; before [I]. id = I; before [I]. p = p; before [I]. d = d; after [I]. id = 0; after [I]. p = p + T * d; after [I]. d = d; // The id is unknown. Use 0 instead.} // calculate the sequence array sort (before, before + n); for (I = 0; I <n; I ++) order [before [I]. id] = I; // calculate the final state, retain the original state or other state sort (after, after + n); for (I = 0; I <n-1; I ++) if (after [I]. p = after [I + 1]. p) after [I]. d = after [I + 1]. d = 0; // output result printf ("Case # % d: \ n", ki); for (I = 0; I <n; I ++) {int a = order [I]; // restore the input order if (after [a]. p <0 | after [a]. p> L) printf ("Fell off \ n"); else printf ("% d % s \ n", after [a]. p, dirName [after [a]. d + 1]);} printf ("\ n");} return 0 ;}
Running result: