Ant question solution: Explanation of antbuild. xml
Description:
There are N ants on a wooden stick with a length of l cm. each ant is either left or right at a speed of 1 cm/second. When two ants collide, they will turn around at the same time (the turn time is not counted) to give each ant the distance from the left end of the wooden rod, after how many seconds, just all the ants fell from the wooden rod.
N and L cannot exceed 1000
Enter two integers, N and L, respectively.
In the next N rows, each line starts with a character, L or R, representing the Left or Right direction, and then an integer x, representing the distance of the ant from the left end of the wooden stick.
Sample input:
4 10
R 1
R 5
L 3
R 9
Sample output:
9
Difficulty: 0
Hint:
Suppose you observe the two ants moving in the distance. What is the difference between them turning around after the collision and directly passing through?
At the beginning, I was a little scared to see the question ~.~ After careful analysis, we will find that what we need is the time when the last ant falls down, which is actually the length of its journey. What is the difference between turning around and not turning around? The figure below shows whether or not the turn-around has no impact on the length of the last ant's journey. Is it amazing ~~ Do you want to think about it?
Code:
# Include <stdio. h> int main () {int N, L; int x, max = 0, I; char a; scanf ("% d", & N, & L ); for (I = 1; I <= N; I ++) {// I from 0 to n-1 may be more commonly used scanf ("% s % d", &, & x); if (a = 'R') {x = L-x; // if you are an ant walking to the right, you can ignore the turning problem, the length of the walk is the length of the wooden rod minus the value from the left end} if (x> max) {max = x ;}} printf ("% d \ n", max ); return 0 ;}
Reflection after question: you can think more about the examples given by doing the questions at ordinary times and simulate the process by yourself. It is important to do it first. Sometimes it is useless to simply think about it ~~
(The first time I wrote question solutions, I gradually accumulated experience ~.~)