Skip to main content

Posts

Showing posts from March, 2018

SPOJ - HUBULLU Explained!

Problem statement. The important part is ' Both players play optimally'; meaning: for every move a player can make a move that will lead closer to the winning move. So, when player one starts the game he can make sure the last move is his. Same goes for player two. and i know it is hard to believe.

SPOJ - GSS1

Considering input series: { 4 , -10 , 3 , 100 , -20 , 1 } Query(x,y) = Max { a[i]+a[i+1]+...+a[j] ; x ≤ i ≤ j ≤ y } A node contains-  [ START & END is a node's segment limit ] Prefix is the maximum sum starting at START, end can be anywhere. There are two possibilities of the maximum. One, node's leftChild's prefix or two, adding leftChild's sum + rightChild's prefix. (which will make the prefix contiguous) Suffix is the maximum sum ending at END, start can be anywhere. There's two possibility of the maximum. One, node's rightChild's already calculated suffix or two, add rightChild's sum + leftChild's suffix   (which will make the suffix contiguous). Sum : leftChild's sum + rightChild's sum. MAX Maximum of  -  prefix (result is in the node, starts from START but doesn't end in END ) suffix  (result is in the node, doesn't start from START but surely ends in END ) leftChild's max ( result is in left

Userscript for SPOJ

Greasemonkey is an userscript manager that let's users manipulate any web page before or after it is loaded on browsers. It is available as an extension in browsers.  https://en.wikipedia.org/wiki/Greasemonkey In short, it runs custom piece of  code on any websites. In SPOJ problem page, there's problem-tag , which is really helpful but not always. The same thing with auto showed comment table . Most of the time while scrolling, I see hints that I do not want to read unless I am stuck. So, to rectify the situation, I wrote a few lines of javascript to hide  the problem-tag and  comment table. I also wrote another line to select CPP as the default language in the solution submission form. Setup: Greasemonkey for Firefox or Tampermonkey for Chrome Then go to New User Script & copy-paste the code. That's it. 

SPOJ 95 - STPAR - Street Parade

Problem Statement Heuristics:   Let, N be the number which is next in fixed order. Take an input X , if it is equal to N then next number will be n++. If not, then search for N in the side lane and after finishing the search, put X in the side lane. After all X is processed but N is not found in either X or side lane, then the order is not possible. Solution:

SPOJ 5 - PALIN

Problem Statement Explanation is here Sample Solution : #include<bits/stdc++.h> using namespace std; char k[1000005]; int l,i,j,mid; bool chkNine() {     cout<<""; //for SPOJ's internal problem, this line is needed     for(i=0;i<l;i++)     {         if( k[i]!='9' ) return false;     } } void nextPalin() {     int f=0;     l=strlen(k);     if(chkNine()) {         memset(k,'0',l);         k[0] ='1';         k[l]='1';         k[l+1]='\0';         return;     }     // calc mid,i,j     if(l%2==0)     {         i= ( l/2 )-1;         j= l/2;         mid= (l/2) -1;     }     else     {         i= ( l/2 )-1;         j= (l/2 )+1;         mid=l/2;     }     int a = i, b =j;     //iteration of k[i] & k[j]     for(; i>=0; i--,j++)     {         if(k[i] > k[j])         {             f=1;             break;         }         else if ( k[i] < k[j] )         {             f=2;             break;        

Next Smallest Palindrome

Given a number, we have to find the next smallest palindrome which is larger than this number.  The input can be large. We'll store the input in an array. e.g. if input is  1259, array will be, int k[]= {1,2,5,9} To find the next smallest palindrome, we have to generalize the input. There can be three types of input.    All digits are 9. e.g. 999 Input is not a palindrome. e.g 12942 The input itself a palindrome number. e.g. 1221 Algorithm for the first type is easy. Expand the array length by one and put boundary digits as 1 & all other digits will be 0.  Input :    999 Output : 1001   Left half & Right half : Using example, left half of 522665 is, 522 & left half of 52123 is 52 . Right half of 522665 is 665 & right half of 52123 is 23 .   Iteration: We'll compare between left half and right half, starting from middle. For example, two variable i and j will indicate to 2 & 4 respectively, if the input is 1 2 2 4 2 and i

SPOJ 394- ACODE

Problem Statement As from the problem-tag, you can see this is a DP( Dynamic Programming ) problem. Don't know Dynamic Programming? Here are some articles: Codechef , TopCoder . Try to solve a sample problem, for example, Fibonacci Numbers. Let's start cracking the problem. To solve a DP problem, the hardest and important part is to find the relation between previous or next steps and current step . We need to find out how the solution of the current step depends on its previous steps or next steps. Looking at:      2 5 1 1 4        from left to right Step 1: You can only interpret this as 2(B). Decoding- B EAAD  So, DP[1] = 1 Step 2: This can be 5(E) or collaborating with 2, it can be 25(Y). Result is BE AAD + Y AAD . So, DP[2] = 2 Step 3: Can only be interpreted as 1(A). 51 is not valid, as stated in problem. BEA AD + YA AD . DP[3] = 2.  Step 4: Will be interpreted as 1(A). BEAA D + YAA D+...   Look at the decodings, if we interpret as 1(A) the decodings

Trailing Zeroes of n!

Find the number of trailing zeroes in 122! ?  That's a big number right? The solution is pretty simple. Ask yourself when does any multiplication results in a trailing zero? The answer is (2x5) = 10, leaves one zero behind. The factorial of 122 is a multiplication which you already know. The expansion is, 98750442008336013624115798714482080125644041369 78359605958470050267671457205014364903379642774 50422940710230505796264047365129395968426948958 2137821062001338805474721479524352 0000000000000 000000000000000 So, we have to find the number of multiples of 5 from the expansion and they are 5 10 15 20 25 30 35 40 45 50 55 60 65 70 75 80 85 90 95 100 105 110 115 120. So there are 24 multiples of 5. then again, 25= 5 x 5 , which has another extra factor of  5 to count. So how many multiples of 25 from 1 to 122? (122 / 25) = 4 (taking the only integer) So, add that 4 to the earlier count. 24 + 4 = 28 , so we have 28 trailing zero in 122! Let's take a numb

SPOJ 4300 - Rectangles

Problem Statement Hint: If the input is 15 squares rectangles are-                 1x1 1x2 1x3 ...................................1x15                        2x2 2x3 ............................2x7                                   3x3   ....................3x5                          can't make 3x2 cause 3x2 and 2x3 are same . can't make 2x8 cause that requires 16 squares. can't make 4x4 cause that also requires 16 square.  Solution: #include<stdio.h> int main() {     int n,c=0,i,j;     scanf("%d",&n);     for(i=1;i*i<= n;i++)     {         c+= (n/i)-i+1;     }     printf("%d\n",c); } * If there's any query or mistake, let me know.