Tuesday, December 30, 2014

ACM 1219. Symbolic Sequence

  Here is a stupid but acceptable solution for you.

  Output a random letter 1000000 times and stop the program. It has a high success probability. If you failed from the first try ( which you probably won't) , try one more time.


       FULL SOURCE CODE

ACM 1731. Dill / Укроп

  If all of the pairs from the second pile have a difference which can't be covered by any difference between any pair from the first pile, then we found a solution. If we take the first pile consisting of all elements from 1 to n and the second pile: a set of elements where each differs from the previous one by (n+1) starting from a number bigger than n, all the requirements will be met.


       FULL SOURCE CODE

Thursday, December 18, 2014

ACM 1510. Order / Порядок

 This task is very easy to solve with the help of map data structure. Make a map of 2 integer on integer and after reading number increase the corresponding map[X] by 1. Then loop over all the elements and check for the index which has the maximum value.


       FULL SOURCE CODE

ACM 1296. Hyperjump / Гиперпереход

  The negative numbers make us think "what should I do if on the way of collecting the sum I saw a negative number, should I collect it and continue or should I stop collecting right there?". Here is the answer, if after collecting the negative number your current sum is more than 0 you should keep collecting because you might get a better result (don't forget to update the maximum value of the final answer every time).



       FULL SOURCE CODE

ACM 1180. Stone Game / Игра с камушками

  Here is the one crucial thing we need to notice and understand,  that if the starting position is divisible by 3 then the position is a losing position for the starter, in all other cases the starter can win. Here is how it happens. All the powers of two have remainders 1 and 2 when divided on 3 obviously. The optimal strategy would be the following : "At every step leave your opponent in a divisible by 3 position". The smallest non negative number divisible by 3 is 0 and if the starter get's to 0 that would only mean that the second player picked the last stones left. The steps are as follow. If you are in a divisible by 3 position after, you make a move for sure your opponent will not be in a position divisible by 3 (it will have a remainder of 1 or 2 when divided on 3) and after that he can take some number of stones which will leave you in a divisible by 3 position again, this process will continue until he picks the last 1 or last 2 stones. When we begin on a non divisble by 3 position we just need to make the move of our opponent and leave him in a divisible by 3 position which will get him a defeat.


       FULL SOURCE CODE

ACM 1404. Easy to Hack! / Легко взломать!

 We need to implement the reverse process in order to solve this task, Suppose we have the array of new characters where each character is written as a number. For every sum[i] the real char in that spot will be sum[i]-sum[i-1] .We just need to loop over and recover the initial string, every s[i]=a[i]-a[i-1], and in the end s[0]=s[0]-5. Also, during this process we need to check for the subtraction not get lower than 0, in that case just add 26 to the negative result to turn it into positive.

  


        FULL SOURCE CODE

 


ACM 1161. Stripies

We need to consider a little thing here, that the lost will be greater if we square root the biggest ones. We just need to get the biggest 2 numbers, calculate the new number and get the new 2 biggest from the new pile until there is only one left. 


       FULL SOURCE CODE

ACM 1786. Sandro's Biography / Биография Сандро

Here is a simple solution to this task. There is an interval with the length  6 (the length of the word "Sandro" which we need to turn into "Sandro" and it is easy to calculate the amount we need to pay, if the corresponding chars are equal than no money, if they are equal have different cases then 5 , if they have the same case but are not equal than it is still 5 and when they have different cases and are not equal then the money is 10 for that single char. We can consider all the intervals, calculate the amount for each interval and take the minimum. 


       FULL SOURCE CODE

ACM 1796. Amusement Park / Парк аттракционов

   First of all, if buying X tickets we do not have enough money then for buying more than X tickets we won't have enough money too. Secondly, If after buying X tickets , we can buy some more tickets with the remaining money, then after buying some number of tickets less than X we will still have enough money to buy more tickets. All of these means that the answer is an interval of numbers. Obviously the upper bound is the total number of money divided on the cost of the ticket. If we had one note missing than it wouldn't be enough so if we consider the possibility of all the possible number of tickets after considering each of the 6 notes lost and take the maximum of those numbers we will the lower bound of our interval.


       FULL SOURCE CODE

Wednesday, December 17, 2014

ACM 1048. Superlong Sums / Сверхдлинные суммы

 For handling large numbers (so-called bigInt s ) we need to operate with them digit by digit like we used to do from our early grades. Sum up the last digits, if the result is greater than 9 then keep the "1 in mind" and write down 9 and after processing the next pair of digits add the 1 (if kept in mind from the previous step) and do the same for all the digits and save the result up in an array. Also you should use fast input/ output methods to makre sure that your code gets accepted.


       FULL SOURCE CODE

Sunday, December 14, 2014

ACM 1021. Sacrament of the Sum / Таинство суммы

  We will use binary search for this one. Suppose our first array (called A) is sorted in ascending order. For a number x, if x+A[i] is greater than 10000 we can say for sure that for all I>i, x+A[I] will > 10000 which means for every B[i] we need to take initial segment 1..n check for the middle element and see if the sum of the element from B and the middle element of A is greater than 10000 then we should check for the segment 1...mid else mid...n. Check the source code for a better understanding.


       FULL SOURCE CODE

Saturday, December 13, 2014

Friday, December 12, 2014

ACM 1194. Handshakes / Рукопожатия

  This task is actually very funny. If we have initial number of N and everyone is going to separate until there is one left or a couple left which means that all the possible handshakes are going to take place, which means if there are N people there will be ((N-1)*N)/2 handshakes (sum of numbers form 1 to N-1) but considering that there are couples which don't give handshakes to one another which means we should subtract the number of couples from our answer.


       FULL SOURCE CODE

ACM 1935. Tears of Drowned / Слёзы утопленников

  Here is what we need to understand, If there are 2 neighboring skins, then between there should be max(a[i],a[i+1]). Now, considering A[0] and A[N+1] equal to 0 it is the sum of maximums of all pairs I and I+1. Also for obtaining the smallest number we need to sort them out.


       FULL SOURCE CODE

ACM 1224. Spiral / Спираль

 After examining the task on some samples we can understand the following, if initially we are facing right, then until the next time we will face right is the time when we will be considering a board of Size N-2,M-2 and on this process we did 4 turns. So, the answer would be , generally speaking (N/2)*4. But here is one more thing, when we are down to 1 then we are not making any turns if N gets 1, but if N gets 1 we are making only one more turn , which means that if (N<=M) answer is 2*N-2 otherwise it is 2*M-2+1 which is 2*M-1.


       FULL SOURCE CODE 

ACM 1131. Copying / Копирование

  First of all the rate of copying is 1, then after the next move the rate will be 2*1, and then 2*2*1, until the rate reaches K. If the rate is constant then the answer is N/K and +1 if N is not fully divisible on K. You need to implement the process of the rate growth and decrease the number of remaining computers, and then add N/K to the final answer ( +1 if N is not fully divisible on K).


       FULL SOURCE CODE

ACM 2005. Taxi for Programmers / Такси для программистов

  The first point is always 1 and the last point is always 5. Then we need to take all the permutations of numbers (2,3,4) where the 3rd number is not on the last place and check for the best answer.


       FULL SOURCE CODE

Thursday, December 11, 2014

ACM 1146. Maximum Sum

  Here is how we will solve it, first of all we need a helping array of already calculated sums, let's call it sum where sum[i][j] shows the sum of elements of the i-th row from 1 to j. Now we will fix some interval  from 1 to N for the columns, and another interval from 1 to N to all the rows. In other words, if we have an interval of [C1,C2] for columns and [R1,R2] for rows then we need to calculate the sum of a square which has top left coordinates of [R1,C1] and bottom right coordinates [R2,C2]. In the process update the value of the final answer if necessary.


       FULL SOURCE CODE

ACM 1712. Cipher Grille / Шифровальная решётка

Obviously we need to output all the characters where 'X' lies in the corresponding cell, and rotate the field. We need to those steps 4 times, but the hardest part here is rotation.
Have a look at the following 4x4 table.
1234
5678
90ab
cdef
If we rotate it we get
c951
d062
ea73
fb84

As you can see, every Ith row became N-i+1 the column ( if numerate from 1). or A[i][j]=A*[j][N-i+1];



       FULL SOURCE CODE

ACM 2011. Long Statement / Длинное условие

First of all sort all the numbers, then our goal is to check the number of different permutations , if that numbers exceeds 5 then the answer is Yes. If you are using C++ you can easily handle everything with the next_permutation function in the library "algorithm" , if not then read about getting the next permutation of a sequence here.



       FULL SOURCE CODE 

ACM 1502. Domino Dots / Точки домино

  If we sort the domino figures by one side we will get the following set, 0/0 0/1 0/2...... 0/N, 1/1 ,1/2,....., N-1/N-1,  N-1/N,  N/N
so if the first number is M then the other number can be from M to N. The sum of numbers from 1 to K equals K*(K+1)/2. For calculating sum of numbers from M to N we need to take N*(N+1)/2 - (M-1)*M/2.  So for all the dominoes which have the first number of M we will have answer of N*(N+1)/2 - (M-1)*M/2 + (N-M+1)*M.


       FULL SOURCE CODE

Wednesday, December 10, 2014

ACM 1073. Square Country / Квадратная страна

 We will keep an array of answers DP[N] where DP[i] shows the answer for i. We also would like to keep a list of squares up to 60000. The algorithm pretty much looks like the algorithm of the knapsack problem except here we keep the number of elements. Initially all the numbers equal to infinity (to a very big number) now starting form 0, if we are on a current element I then we need to check for all the I+ squares and check if we can update the answers, in other words DP[I+squares]=min(DP[i+square], DP[i]+1);


       FULL SOURCE CODE 

ACM 2002. Test Task / Тестовое задание

   All you need to do, is to implement the registration process carefully, if you are using C++ I recommend using C++ STL map which will help you a lot in this case. check the source code for a good understanding.


       FULL SOURCE CODE

ACM 1792. Hamming Code / Код Хэмминга

 The problem just needs basic implementation, write a function that checks if the current sequence is correct, then switch every single element and call the function each time to see is the new sequence correct or not. When found a correct sequence, output and exit.


       FULL SOURCE CODE

ACM 1457. Heating Main / Теплотрасса

 The task wants us to calculate the mean of all the numbers SUM/N.


       FULL SOURCE CODE

Thursday, December 4, 2014

SPOJ 27. Sorting Bank Accounts (SBANK)

  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.


       FULL SOURCE CODE




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.


       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