Monday, April 8, 2013

Thoughts on UVA: 10137: The trip

The trip is a very interesting problem, but the fact for me is that I haven't been able to solve it even after a long time. I have collected all the test cases available in the board and all except 1 test case fails and I have no idea how to solve it. Here's the failing test case:
15
0.01
0.03
0.03
0.03
0.03
0.03
0.03
0.03
0.03
0.03
0.03
0.03
0.03
0.03
0.03
The expected answer is: $0.02, while my solution says $0.01
To me, it looks like there is some key piece in the problem statement that is missing for me to correctly figure out the answer. I also felt that arriving at my current solution was also not very straight forward. I believe that clearing up the problem statement will help people to understand it better and solve it. The problem's success rate is average probably because of this reason.
My learnings:
1. Program to use money like the way it is being by people. Do not go for floats and doubles. That will help in avoiding magic like floor, ceiling. I got this hint from Prof.Skiena's programming challenges videos. The original link seems to be dead :(
2. The way to compute average is very key to be able to get to a solution and to be able to explain it to others. What is a solution if it cannot be understood by others? What is a solution if it cannot be explained to others?

Friday, April 5, 2013

UVA: 11942: Lumberjack Sequencing and 11461: Square numbers

Got both problems Accepted.
11942: Lumberjack Sequencing - tried binary search and found it to be tedious to program and difficult to get right. Switched to linear search and it simplified the number of cases to be handled.
11461: Square numbers - interesting thing here is that we need to remember the length of the beards as we proceed. I was thinking of comparing diff(i - 1, i) with diff(i, i + 1), but again, need to remember the history. So, I kept the implementation simple by remembering the last diff and the current.
Small problems can teach us quite a bit.
Thanks to Alluri for solving the problems ahead of me and recommending them to me.