Sunday, March 25, 2012

Binary Indexed Trees and Star Wars. .!!!

It all started with the problem of counting inversions. . First implemented a merge-sort algorithm (quite alot of messy code) but finally managed to get an ACC for SPOJ problem INVCNT.
https://www.spoj.pl/submit/INVCNT/

Then saw a similar problem on SPOJ titled YODANESS. The problem goes like this.

Yoda is the wisest, and perhaps the most powerful Jedi of his time. Yoda is a mysterious figure and he has many oddities. One of them is that Yoda often changes the order of words in the sentence. For example, one of such phrases is "Or I will help you not." Let's call the yodaness level of any statement the number of pairs of words that changed their order, as to the order in which they were supposed to go in a normal statement. Write a program that determines the yodaness level of different statements of Yoda.

Being a Star Wars fan, i decided i would definitely solve this one. Again started with the merge-sort algorithm. Tried a lot to modify the algo to suit this problem but no matter what, succeed i could not. Then i started googling for other ways to solve this problem. Finally came across this problem listed under BIT on a Codeforces blog. Searching for it again, i find the link to an amazing tutorial on TopCoder for the same.
http://community.topcoder.com/tc?module=Static&d1=tutorials&d2=binaryIndexedTrees#prob
Went ahead and implemented it, BINGO soln ACC. .!!!
And solve the problem i did. :-)

SPOJ: Deposit

A very nice problem involving more of pen and paper work than coding. Plus a constant time algorithm. One of the few problems to get ACC at the first attempt. :-)
Personal note: DO NOT GET CONFUSED BETWEEN fibonacci(n) and factorial(n)
U can find this problem here. .
http://www.spoj.pl/problems/DEPOSIT/

SPOJ: Aggressive Cows

Its been quite a long time since i got inspiration to solve SPOJ problems. Then i hear about this problem and how binary search could create problems in it. So deciding to give it a go (c’mon how tough can binary search be??) i start coding it up. About 20 wrong attempts on my PC and TLE exceeded 5 times on SPOJ, finally realised i was doing binary search on the quantity with a smaller bounds and normal iteration through the one with larger bounds. .!!! One wrong submission later (submitted it in ADA instead of c++ :P ) finally got ACC. .

The problem can be found here. .
http://www.spoj.pl/problems/AGGRCOW/
Cheers

Hello World. .!!!

It may be cliche, but I felt that it was appropriate to start my first blog post with the introductory phrase “Hello World”. Welcome and Cheers!!!

Image