Monday, March 2, 2015

ACM 1638. Bookworm/ Книжный червь

 This task has a difficulty of only 84  and yet almost 2/3 of the total submissions are not accepted. I liked this one really. It is a very tricky task. Let's start with examining the input. Some people say that it is wrong, it looks wrong at first but eventually you will understand. So here is the input.

10 1 1 2

We Expect the answer to be 22, because we expect that the from the first page of book 1 to the last page of book 2 is a total distance of 2 covers and 2 volumes. But it's not correct because we need to imagine the book shelf in our minds and see for ourselves. I made a little picture to illustrate my point below. Have a quick look.



 

I think now everything is settled. As you can see it takes only 2 covers to travel from the beginning of the first book to the end of the second book which buildup our generaly formula  (end-start)*2*cover + (end-start-1)*volume. 
But there are 2 more tricky cases. It is not guaranteed that the starting number is always less then the ending number. If they are equal then the worm passes through the whole volume and if the second one is smaller than the first one then the worm passes though 2 more additional volumes. 



No comments:

Post a Comment