Here is what simple things you can do to handle everything easily. Input the entire line as one string, obviously if one of those string is smaller than the other one it means that it should come earlier. Now, you can just sort the entire array using C++ sort which works very then easily check for the copies by comparing each one to the next and output the answer.
This is the blog of site xoptutorials.com. All the news will be posted here and on the facebook page of xoptutorials.
Showing posts with label SPOJ. Show all posts
Showing posts with label SPOJ. Show all posts
Thursday, December 4, 2014
SPOJ 2716. Maximal Quadrilateral Area (QUADAREA)
The area of the cyclic quadrilateral is sqrt ( (m-a)*(m-b)*(m-c)*(m-d)) where m is the half of the sum of all the sides. The rest is clear, input and output using the formula.
FULL SOURCE CODE
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.
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
Sunday, September 21, 2014
SPOJ 8061. Christmas Play (AMR10G)
N<=20000. We need to sort the entire array and then for every i , if i+k-1<=n choose the minimum of A[i+k-1]-A[i].
FULL SOURCE CODE
SPOJ 7169. Pizza
The first thing just comes to mind is to add all the number and round up and output the answer, but unfortunately it is not how it works. Suppose we have 3/4 3/4 and 1/2 by doing the way I described above you will calculate 2 pizza's for friends but the answer is 3, because if we by 2 pizzas for 3/4 s then with the remaining 1/4 and 1/4 we can't form one 1/2 . So, here is what I suggest doing for being correct. First of all we need to buy pizzas for all 1/2s (take 2 1/2s and add 1 pizza) until either one 1/2 is left or none. Then we need to buy 1 pizza for 1/2 and 3/4 (until we run out either of 1/4s or 3/4s). If we have 3/4s left then we need to increment the answer by their number. Then we need to add the number of 1/2s *0.5 and the number of 1/4s *0.25 and round up the answer. NOTE Don't forget to add 1 to the total answer (see the problem statement) and output the new line.
FULL SOURCE CODE
Wednesday, September 17, 2014
SPOJ 8670. THE MAX LINES
First of all, it is easy to notice that the triangle is a right angle triangle because it has a base which is the diameter of the circle. that means that BC^2=AC^2 + AB^2 we can add and subtract one AC which will not change the equation. BC^2=AC^2 + AB^2 -AC + AC . Let us write BC^2 as (2r)^2 which will be 4r^2 and let's group the right part properly. 4r^2= (AB^2+AC)+AC^2-AC . =>
AB^2+AC= 4r^2 -AC^2+AC which can be considered as a function f(AC) where AC is the unknown variable. We need to find the maximum value of function f(AC) which can be done with differentiation . f '(AC)= 0 -2*AC +1 .
-2*AC+1=0 => AC=1/2 => we can easily notice that the function increases up to f(1/2) and then it decreases so the maximum value is at AC=1/2 which gives the full answer of 4*r*r -1/4+1/2 =4*r*r+1/4.
AB^2+AC= 4r^2 -AC^2+AC which can be considered as a function f(AC) where AC is the unknown variable. We need to find the maximum value of function f(AC) which can be done with differentiation . f '(AC)= 0 -2*AC +1 .
-2*AC+1=0 => AC=1/2 => we can easily notice that the function increases up to f(1/2) and then it decreases so the maximum value is at AC=1/2 which gives the full answer of 4*r*r -1/4+1/2 =4*r*r+1/4.
FULL SOURCE CODE
Tuesday, September 16, 2014
SPOJ 6219. Edit Distance
This is a basic problem of edit distance which you can read about in here http://manyprogrammingtutorials.blogspot.com/2014/09/edit-distance-levenshtein-distance.html
.
.
FULL SOURCE CODE
Sunday, September 14, 2014
SPOJ 7757. Flowers Flourish from France
The task itself is not hard, but we need to carefully handle the implementation. Here is hat I suggest, take the first letter and keep it in a variable. Let's call it K. K is the symbol which we need to check every time. and also turn it into upper case, if it is already uppercase it will not change, we do it so that we can avoid the problem of letters being the same but the cases being different. Then loop over the given string , whenever we encountered a space that means that the next letter is a first letter of a word, we take that letter, turn it into uppercase and check with variable K. If they are the same we keep going, but now the value of K becomes the newly found letter uppercased. If at some point a letter which was after ' ' (the forst letter of a certain word) is not equal to current K than we abort and output 'N'.
FULL SOURCE CODE
Wednesday, September 10, 2014
SPOJ 7406. Beehive Numbers (BEENUMS)
The important thing here is to notice the pattern of the beehive. The first layer contains 1 cell. When building a new layer we lay 6 more. If you just take out pen and paper and start drawing you will figure out the pattern, when laying the next layer we lay 6*2 12 cells, so for laying Nth layer we will need N=6*(N-1) cells when N>2. I recommend calculating all the numbers and store them in the map before processing the input.
FULL SOURCE CODE
SPOJ 95. Street Parade (STPAR)
We need to take out elements in order with a little help of one stack. We need to solve this by eliminating every possible element starting from the left and also taking out those very same elements from the stack if it's their turn ( the phrase "their turn" means that we already eliminated all the elements up to the current one , and we can eliminate him if he is one the leftmost spot or on top of the stack). We need to loop over the given array from left to right and see, if it's his turn then eliminate it (this can be understood in many ways, just ignoring and passing to the next one or removing it from the array, it depends on your preference ). (note that we keep a variable cur (current) which shows which is the current element to be eliminated), now if we can't eliminate it we add it to our stack waiting for it's turn, (NOTE there is no other way, if our current element can;t be eliminated now because it's not his turn then we do not have any other option but to push him into stack). And in every step we also check "if it's now the turn of the top element of stack", if yes we need to keep removing them until we either reach an element which can't be eliminated now or our stack gets empty. We need to keep doing those until we have looped over all elements. If we were able to successfully remove all of them then the answer is yes.
FULL SOURCE CODE
Tuesday, September 9, 2014
SPOJ 379. Ambiguous Permutations (PERMUT2)
The constraints N<=100000 make this task an easy one. We need to build the sequence and check if every element is equal to the given sequence if it is then the answer is yes otherwise no.
FULL SOURCE CODE
Sunday, September 7, 2014
SPOJ 400. To and Fro
This is an implementation task so we need to think the easiest and the most understandable method. I suggest trying to recover the matrix then the string itself. We can do it in the following way. The first element of the given string S[0] is the first element of an array A[1][1] . Now we need to move according to the direction, and every time write the current char in the current spot, whenever we reached the end (or the beginning) of the array we need to flip the direction, get down to the next row and keep on going. Check out the source code for a better understanding.
FULL SOURCE CODE
Monday, August 11, 2014
SPOJ 16033. Tip Top Game (TIPTOP)
http://www.spoj.com/problems/TIPTOP/
(A little misunderstanding regarding the task, the thing which we need to check is whether the number has even number of divisors or no).
Judging from the input (10^5 test cases and each texst casu up to 10^18) we can assume that this task is a type where we need to input the number and ouput the answer without much operations. For solving tasks like this I recommend writing a brute force code, output the answer for some number and then spot the pattern easily. You can download the output for the first 1000 numbers below and you might figure out the pattern.
(A little misunderstanding regarding the task, the thing which we need to check is whether the number has even number of divisors or no).
Judging from the input (10^5 test cases and each texst casu up to 10^18) we can assume that this task is a type where we need to input the number and ouput the answer without much operations. For solving tasks like this I recommend writing a brute force code, output the answer for some number and then spot the pattern easily. You can download the output for the first 1000 numbers below and you might figure out the pattern.
Download output for first 1000 numbers
Basically, the answer is yes when the number of divisors is odd. So when the number of divisors of a number is odd? By observing the output you will figure out that it is true when the number is a perfect square. So, let's discuss why is it true. In case of all prime numbers which can't be perfect squares , the number of divisors is even (only 2 divisors).
Let us have a list of out divisors of a certain prime numner P.
1 and P
So, when we multiply this number with any other number not equal to himself, we will get a new list of numbers. Supose we multiply with. K
we get, 1, K, P, P*k which is again even. It will turn odd only if there is a number in that list which we wrote 2 times which is K and P so we must multiply a prime number with itself to get a number with odd number of divisors =>only perfect squares have that feature.
DOWNLOAD THE FULL SOURCE CODE
Saturday, July 12, 2014
SPOJ 3032. Adding two numbers (ADUN)
http://www.spoj.com/problems/ADUN/
You know, the first problem of almost every coder A+B. Enjoy
You know, the first problem of almost every coder A+B. Enjoy
DOWNLOAD THE FULL SOURCE CODE
Thursday, July 10, 2014
SPOJ 4033. Phone List (PHONELST)
http://www.spoj.com/problems/PHONELST/
This task is a basic task for learning how to use Tries , you can find info about Tries here.
This task is a basic task for learning how to use Tries , you can find info about Tries here.
DOWNLOAD THE FULL SOURCE CODE
Subscribe to:
Posts (Atom)