Uvalive6662
PDF questions
Question:The length of the tunnel is l, the number of ants is N, the position pI of each ant, and the forward direction di are all integers (forward direction is L or R), and the ant speed is 1cm per second, if the two ants encounter each other at an integer point, they both return to the opposite direction. If they do not encounter an integer point, they continue to move forward. Find the number of the last ant to exit the tunnel. Ant financial Id 1 ~ Given by N, the tunnel head and tail are 0 and L.
Question:
Simulation.
Of course, we do not need to simulate real-time walking by ANT financial. We only need to calculate when the ant financial will hit.
Create a struct for ant financial, including numbers, locations, and directions for future operations. Because the starting position of ant financial is an integer, it will always encounter at the integer point, so all data can be stored as integers.
First, sort the ants in order, the first keyword position, the second keyword direction, the smaller the position value after the sorting, the more front the array, the same location L in front of the R. Then we conduct research on each ant, and look forward to it for the first ant that reversely matches it and will meet it at the integer point. Record the minimum integer point collision time mint. After determining all the ant financial, let the ant financial pass through the mint, and then sort it to shift the ant financial in the same position.
Repeat the above operation until no ant will collide. Calculate the departure time of each ant, find the final ant, and output its number and total time.
The questions ensure that the ant will go out and will not be killed in it.
I didn't tell you what would happen when more than two ants hit each other. I also thought about it. I don't know if there is such data.
1 // # pragma comment (linker, "/Stack: 102400000,102400000 ") 2 # include <cstdio> 3 # include <cmath> 4 # include <iostream> 5 # include <cstring> 6 # include <algorithm> 7 # include <cmath> 8 # include <map> 9 # include <set> 10 # include <stack> 11 # include <queue> 12 using namespace STD; 13 # define ll _ int64 14 # define USINT unsigned int 15 # define MZ (array) memset (array, 0, sizeof (array) 16 # define minf (array) MEm Set (array, 0x3f, sizeof (array) 17 # define rep (I, n) for (INT I = 0; I <(n); I ++) 18 # define for (I, X, n) for (INT I = (x); I <= (n); I ++) 19 # define RD (X) scanf ("% d", & X) 20 # define RD2 (x, y) scanf ("% d", & X, & Y) 21 # define rd3 (x, y, z) scanf ("% d", & X, & Y, & Z) 22 # define Wn (X) printf ("% d \ n", x); 23 # define re freopen ("D. in "," r ", stdin) 24 # define we freopen (" 1biao. out "," W ", stdout) 25 26 const int INF = 0x3f3f3f; 27 co NST int maxn = 22; 28 29 struct ant {30 int num; 31 char D; 32 int P; 33}; 34 35 int n, l; 36 ant a [maxn]; 37 38 bool operator <(ant x, ant y) {39 if (X. p! = Y. p) return X. P <Y. p; 40 else return X. d <Y. d; 41} 42 43 void del (ant a [], const Int & I, Int & M) {44 for (Int J = I; j <m-1; j ++) 45 A [J] = A [J + 1]; 46 m --; 47} 48 49 void check (INT m) {50 for (INT q = 0; q <m; Q ++) 51 printf ("% d. (% C, % d) ", a [Q]. num, a [Q]. d, a [Q]. p); 52 puts (""); 53} 54 55 int main () {56 int I, j, k; 57 while (scanf ("% d ", & N, & L )! = EOF) {58 If (n = 0 & l = 0) break; 59 60 for (I = 0; I <n; I ++) {61 scanf ("% C % d", & A [I]. d, & A [I]. p); 62 A [I]. num = I + 1; 63} 64 65 sort (A, A + n); 66 int M = N; 67 int time = 0; 68 while (M> = 1) {// ant 69 int mint = inf; 70 for (I = 0; I <m; I ++) 71 if (a [I]. D = 'l') {72 for (j = I-1; j> = 0; j --) 73 if (a [J]. D = 'R' & (A [J]. P + A [I]. p) & 1) = 0) {74 int TT = ABS (A [J]. p-A [I]. p)/2; 75 if (TT <mint) mint = tt; // the minimum time for a record to hit 76 break; 77} 78} else // A [I]. D = 'R' 79 for (j = I + 1; j <m; j ++) 80 if (a [J]. D = 'l' & (A [J]. P + A [I]. p) & 1) = 0 ){///(.. & 1) = 0, parentheses will tease, & the priority is very low look 81 int TT = ABS (A [J]. p-A [I]. p)/2; 82 If (TT <mint) mint = tt; 83 break; 84} 85 if (mint! = Inf) {// an ant will hit 86 for (I = 0; I <m; I ++) /// let the ant's time pass through the mint hit 87 if (a [I]. D = 'l') A [I]. p-= mint; 88 else a [I]. P + = mint; 89 sort (A, A + M); // sort, move the ants in the same position together 90 for (I = s-1; I> = 0; I --) // Delete the ant 91 If (A [I]. P <0) | (A [I]. p> L) del (A, I, m); 92 I = 1; 93 while (I <m) {// convert all the ants in the same position to 94 if (a [I]. P = A [I-1]. P & A [I]. d! = A [I-1]. d) {95 J = I + 1; 96 while (j <M & A [J]. P = A [I]. p) J ++; 97 for (k = I-1; k <j; k ++) 98 If (A [K]. D = 'l') A [K]. D = 'R'; 99 else a [K]. D = 'l'; 100 I = J + 1; 101} else I ++; 102} 103 // check (m); 104 sort (A, A + M ); /// arrange the order so that the ants with the same position are on the left and on the right without hitting 105 time + = mint; /// statistics of the elapsed time 106} else {// No ant will collide, find out the ant 107 int Maxi =-1 that ran out the last time; 108 int maxt =-1; 109 for (I = 0; I <m; I ++) {110 if (a [I]. D = 'l' & A [I]. p> = maxt) {111 maxt = A [I]. p; 112 Maxi = I; 113} else if (a [I]. D = 'R' & (L-A [I]. p> maxt) {114 maxt = L-A [I]. p; 115 Max I = I; 116} 117 // printf ("% F, % d", maxt, Maxi ); 118} 119 printf ("% d \ n", maxt + time, a [Maxi]. num); 120 m = 0; 121} 122} 123 return 0; 124}View code