Sunday, February 9, 2014

SPOJ 9734. Hacking the random number generator

http://www.spoj.com/problems/HACKRNDM/

Of course this type fo probelms can be used with a simple approach. We need to keep and array named B, (we will call the input array A) and B[I] will shows the number of elements from the array A which have the value of I. Then we will check and add to our answer elements B[A[i]+k] . This might work but we are not sure about the constraints. There might be negative numbers where we can't keep negative numbers as an array index. So, STL map covers this problem because with the help of STL map we can keep every type of an index we want. (If you need info about STL map go here).


       DOWNLOAD THE FULL SOURCE CODE

STL map

Map is another STL container and is aother good way of keeping data. We declare map like this.
map<type1 , type2> m;
Basically this map means that we keep data of type 2 with indexses of data type1. This sounds complicated but I will try to be more clear. For example we want to keep a list of names and surnames. We can do that with map.
If we assign map<string,string> m;
this emans that the following expression is legal m["Smith"]="John"; We can consider the first data type we assign to map as the index data type. So in our example "Smith" is the index and "John" is the value. When we are declaring an ordinary array int a[10000]. then we can use the following expression a[10],a[1] but not a[-25] because the indexing starts with 0. You can imagin map as a little more enhanced version of array. So , one more time map<type1 /*index data type*/ , type2 /*value data type*/> m;
examples
map<string,int> m;
m["John"]=0;

map<int, int > m;
m[-10000]=902;

map<unsigend int,int> m;
m[10]=34; //Note that map<unsigend int,int> m has the same effect as the regular array because the indexing starts with non negative numbers

And one more important question, the memory usage. When we first declare the map it is using almost no memory. When the first value is attached, ( for example we have map<string,string> m; and we write m["John"]="Smith") some memoryis being taken only for storing one elment and somehow keep its index. When we assign another value , it checks if the index we use has already been used he just replaces already taken memory with anotehr value if its new than he take memory again. Example
map<string,string> m;
m["John"]="Smith";
takes memory for storing the value "Smith" under the index "John";
m["Johny"]="Smith";
Takes memory because the index "Jonhy" was unused.
m["John"]="Brown";
Already has a memory for the idnex "John" so just replaces the value "Smith" with teh value "Brown";

Looping over maps is being done with iterators.
Now some important functions

m.begin()
return the value for iterator which shows the beginning of the map. IMPORTANT map always starts looping from teh lowest index to the highest.

m.clear()
clears the map completely.

b.empty()
boolean function which returns true if the map is empty and false if it is not.

m.end()
returns the end of the map for iterator.
Looping over map is accomplished like this.
map<type1,type2> m;
........................
........................
........................
map<type1,type2>::iterator it;
for(it=m.begin();it!=m.end();it++)
{
}
m.erase()
This function ca be used in more than 1 ways. If you give a single iterator to it it will remove the element which has that iterator. If you give 2 iterators it will remove all teh elements between these 2 iterators.

m.find( value)
retuns and iterator, needs a value to find it.

m.size()
returns the size of the map= the number of elements in the map.

Saturday, February 8, 2014

SPOJ 8611. Non-Decreasing Digits

http://www.spoj.com/problems/NY10E/

This task can be solved eaither with math calculaions or with DP (dynamic programming). I will suggest a solution with DP. So we will keep an 2 dimensional array. A[I][J] will show the number of non-decreasing numbers of lenght I which starts with teh digit J. Initialy a[1][0],a[1][1],a[1][2].....a[1][9] are equal to 0. Now we need to count others. For the lenght 2 if the number starts with 0 then after it we can have any number which we made i a previous step . If the number starts with 1 that means that after that we can have all 1 digit numbers which start with a digit greater or equal to 1. The genertal formula will be like this
a[i][j]=a[i-1][j]+a[i-1][j+1]+.....+a[i-1][9]. And I also suggest to keep the sum of the row in the 10the element so that you can be able to output the answer just after reading teh input. Also check teh source code for a better understanding.


       DOWNLOAD THE FULL SOURCE CODE

SPOJ 16246. Trener

http://www.spoj.com/problems/TRENER/

This task is another implementation task. I think the best way is to sort teh array then start looking from the beginning , we need to calculate the numebr of consecuetive elements which start with teh same letter, if the number of those elements is greater than 4 that means that we can add that letter to the answer. This might not be the fastest algorithm but it passes in 0.01 wiht using slow intput output.


       DOWNLOAD THE FULL SOURCE CODE

Thursday, February 6, 2014

SPOJ 13002. For Loops Challenge

The problem statement

This task is an implementation task and the biggest challange is the output format. But anyway I will explain how to solve it in case you need an explaination. I think the best way would be getting all the elements in one array and sort them . Then we need to loop and with that we will keep 2 numbers p and q, which shows the interval which is going to be printed. If the difference between teh current element and the next element equals to 1 then we increment q with 1 if not then we write the output which tells that the number between A[p] and A[q] are printed then we turn the values p and q to the next index. Check the source code for a better understanding.


       DOWNLOAD THE FULL SOURCE CODE

Wednesday, February 5, 2014

13753. Amazing Prime Sequence

The problem statement

This task is all based on precomputations. Do you remember how we were getting all the prime numbers in na given range with the sieve of Eratoshpen? We will do something like that here. Let us have an array of size 10^7 where the element A[I] shows the smallest prime factor for I. Here is what I am saying, for the number 2 the smallest prime factor is 2, and of course for all the numbers divisible by 2 the answer is the same right?. So, when in sieve of Eratosphen we were just marking the numbers as primes, in here we will give all the numbers which are factors of another prime numbers the value of that prime number if that place hasn't been marked yet. Because there is a case, for 2 in A[14] we write the number 2. Then, when we consider the number 7 the asnwer for A[14] shouldn't be changed to 7. Check teh source code for a better understanding.


       DOWNLOAD THE FULL SOURCE CODE

SPOJ 10500. Haybale Stacking

The problem statement

For getting the answer somehow we need to have all the values , and we need to figure out a way to get those values in a special way because the most obvious approach is too slow in this case. So , Instead of looping and increasing every element in the given range we can keep do it in other way. We can keep an array with the name A , every element A[I] shows the difference between elements I and I-1 or in other words A[I]-A[I-1]. When we read an interval suppose it says from a to b. We add 1 to A[a] and subtract 1 from B[b+1]  this way we can easily get the values.Now for getting the exact values of the elements we need to loop over and keep a variable num, num is 0 initially. for Calculate NUMBER[I], we add A[I] to num and NUMBER[I]=num.  Then we need to use a fast sorting to sort them and output n/2+1 th element.Check the source code for a better udnerstanding.


       DOWNLOAD THE FULL SOURCE CODE

Monday, February 3, 2014

SPOJ 10286. DOTA HEROES

problem statement

This task is a very easy one if you look carefully. I suggest looking on the explainatin of the sample test. It suggests a way of crossing a tower, He says that one guy comes and gets a shot and then the rest can pass. So, we only need to send somebody who won't die after one shot and the tower can be considered as crossed, so IN other words if tehre is a hero who can take one hit and stay alive that means that you can pass one barrier. So we ned to calculate the total numebr of shots which all the heroes can take without dying and compare it to the number of towers.


       DOWNLOAD THE FULL SOURCE CODE

SPOJ 8612. Penney Game

The probelm statement.

This task is a task of implementation. The constraints are very low so the simple brute force will pass easily. ust take every 3 letter string and start checking for that in the given string. And every time you find one  increment the asnwer with 1. Here is a checking method I suggest. ust because the strings are of length 3 we can loop over the given string and check if the current character and the next to are equal the the first , second and thirdf characters of the short string.
Check the cource code for a beter understanding.


       DOWNLOAD THE FULL SOURCE CODE

SPOJ 9032. Cube Free Numbers

The problem statement

This task is a pure precomputation task. Remember how we were finding all prime numbers in O(NlogN) time? Yes, the Sieve of Eratosphen.(If you need info about the sieve of eratosphen go here). We will do the same type of thing here but instead of marking the prime numbers we will mark all the NOT Cube free numbers. Of course the first NOT cube free number is 2*2*2=8 then all the numbers which are divisible on 8 are also NOT cube free so we mark them as well. Then it comes to 3, 3*3*3=27 we mark all the number which are divisibly by 27 , we do this to the number 100 because 100*100*100=10^6 which is the maximum value which can be in teh input. Then we need to numerate all the numbers and the solution is ready. Check the source code for more clarification.


       DOWNLOAD THE FULL SOURCE CODE