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. :-)

No comments:

Post a Comment