Thursday, December 4, 2014

SPOJ 297. Aggressive cows (AGGRCOW)

  First of all we need to sort all the given stall positions. Then we can do a very good trick, we can try finding the solution from the solution set. Take the maximum and minimum number which can be the answer. We can write a function which checks if the current number satisfies the task requirements in O(n) time. Then we can take the maximum and minimum possible answer, and with binary search get to the final answer. Check for the middle, if the middle answer is OK then we can check the middle of that middle and the maximum value. And so on, until we are down to 2 or 1 answers which we can check manually.


       FULL SOURCE CODE

SPOJ 302. Count on Cantor (CANTON)

 First we need to figure out the pattern by which the numbers are numerated. This is the pattern but it is kind of reversed, have a look.
http://en.wikipedia.org/wiki/File:Diagonal_argument.svg

The first turn is right then diagonally down, and then when reached the edge it goes down by one and then diagonally up, and the same thing again. Now we need to correctly implement it, you can just make a function which goes from one number to another by a specified direction and changes it whenever necessary, check the source code for a good understanding.


       FULL SOURCE CODE

Monday, December 1, 2014

SPOJ 328. Bishops (BISHOPS)

  If you use some paper and pen you will easily find out that the answer is 2*N-2 but another harder question still remains regarding the implementation. N<=10^100 which calls the big int implementation. If you are not using Java or Python or some other language where bigInt is ready for use here are some tips for using it and the source code provided here has it's own implementation of it. So we need to keep an array of digits and recall the memories of multiplication from our early grades where we were writing one under another and multiplying digit by digit. Check the source code for a good understanding. Also you should handle the cases 0 and 1 separately.


       FULL SOURCE CODE

Sunday, November 23, 2014

SPOJ 5132. Hello Kitty (HELLOKIT)

 The constraints are very low which give us a good opportunity to get the task accepted with a simple brute force. Simple build the first string and every time remove the first letter and add it from the back and you will get the next string. Repeat the process until the new string gets equal to the initial string.


       FULL SOURCE CODE

SPOJ 5676. STONE GAME (RESN04)

Thing we have to notice here is that the game can't have different outcomes if played different way. Consider the pile i , which has A[i] elements, how many times a move can took place for pile i, as long as there are full i-s in A[i] which means A[i]/i times, we can calculate the total number of moves possible for all the piles and in the end if it is an odd number then Alice will win.


       FULL SOURCE CODE

Saturday, November 22, 2014

SPOJ 4301. Tables

It is obvious that the bigger the capacity of the boxes the less amount of boxes we will need to get enough nails. So we need to sort them out and start taking from the biggest until we get enough.


       FULL SOURCE CODE

Wednesday, November 12, 2014

ACM 2023. Donald is a postman / Дональд-почтальон

Basic implementation. We need to somehow keep the corresponding number of shelf of every name so that we can access it with the given names, the C++ map can help a lot here. Also one thing, for making a little easier you can easily notice that there are no 2 names that start with the same letter but belong to different shelfs which means that we can only consider the first letter of the names.


       FULL SOURCE CODE

Monday, November 10, 2014

ACM 1563. Bayan / Баяны

Yet another task which is being handled easily with C++ map because with map we can easily keep the names which we have already encountered before, and if we did increment the answer by 1.


       FULL SOURCE CODE

ACM 2012. About Grisha N./ Про Гришу Н.

 The simple calculation shows that in 4 hours he can manage to solve 240/45 = 5 full tasks which means that if he solves more than 6 before this he will make it.


        FULL SOURCE CODE

Tuesday, November 4, 2014

SPOJ 206. Bitmap

 A simple BFS but we need to be a little clever. Remember how we were doing it? we were starting form a certain node and our wave was passing over all the nodes and the node was the closest, and there is no other path which is shorter than the one we found on the current way, In our case instead of starting from each node all over again we can start from all the '1' nodes which will generate us the same answer. for more details look at the source code/


       FULL SOURCE CODE