Hello party people! 🙂
Today I was wondering whether I have forgotten to code in Python after 4 weeks of coding marathon in VBA and I have decided to try a small portion of the weekly challenge from CodeForces!
So, the second part of the challenge looked really easy, even for me, thus I have decided to give it a try. The original challenge is here. This is how it looks like:
B. Bear and Blocks
Limak is a little bear who loves to play. Today he is playing by destroying block towers. He built n towers in a row. The i-th tower is made ofhi identical blocks. For clarification see picture for the first sample. Limak will repeat the following operation till everything is destroyed. Block is called internal if it has all four neighbors, i.e. it has each side (top, left, down and right) adjacent to other block or to the floor. Otherwise, block is boundary. In one operation Limak destroys all boundary blocks. His paws are very fast and he destroys all those blocks at the same time. Limak is ready to start. You task is to count how many operations will it take him to destroy all towers.
1 2 |
6 2 1 4 6 2 2 |
Output: Print the number of operations needed to destroy all towers.
1 |
3 |
The picture below shows all three operations for the first sample test. Each time boundary blocks are marked with red color.
So, before going to the code, which was even not re-edited once it was working, what did I do? First, I have decided that my data structure would be a list of list and the way to resolve the puzzle would be to make a function, removing the blocks, returning a new structure. The function was going to be ran until the structure existed (e.g. the list of list was with a length that would return a positive boolean 😀 ). True story. The rest was just some time to code it and some time to remember how to get element from a list in Python and plenty of similar searches. Once the structure achieved what I wanted, I have decided to publish it and to watch TV series 🙂 So, here comes the code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
def remove_blocks(lst_towers): for i in range(1, len(lst_towers)-1): for k in range(0, len(lst_towers[i])): b_remove = False #check height if k == len(lst_towers[i])-1: b_remove = True #check left and right if k > len(lst_towers[i-1])-1 or k> len(lst_towers[i+1])-1: b_remove = True if b_remove: lst_towers[i][k] = 0 lst_towers[0] = [0] lst_towers[-1] = [0] lst_new = [] for k in range(0,len(lst_towers)-1): temp = [] temp = [x for x in lst_towers[k] if x != 0] if len(temp): lst_new.append(temp) return lst_new lines = int(input()) lst_first_line = list(map(int, input().split())) lst_towers = [] for i in range(0,lines): lst_temp = list() for z in range(0, lst_first_line[i]): lst_temp.append(1) lst_towers.append(lst_temp) counter = 0 while len(lst_towers): lst_towers = remove_blocks(lst_towers) counter += 1 print(counter) |
A way faster linear solution:
This is the explanation, provided by the author of the solution, a pal I met at a programming competition:
The basic idea is, that you think about the lowest blocks, because they will be removed last. Lets call the turn in which the lowest block in column i will be removed d(i). It can only be removed when an adjacent block either top, left, or right to it was removed in the previous step. The above block gets removed in at most h_i – 1 steps, so the last block is gone for sure in h_i steps at most. Assume that you already know how many steps the block to the left needs. Then the current block needs at most one more (i.e. d(i) = min(d(i), d(i-1) + 1). Since you know that the leftmost block will be removed in the first step d(0) = 1, you just need to go from left to right and update d(i). Do the same from right to left and you are done.
This is his code:
1 2 3 4 5 6 7 8 9 10 11 |
n = int(input()) a = [int(x) for x in input().split()] a[0] = 1 a[-1] = 1 for i in range(1, n): a[i] = min(a[i], a[i-1]+1) a[-i] = min(a[-i], a[-(i-1)]+1) print(max(a)) |
Pretty much, the “trick” is in a[-i] = min(a[-i], a[-(i-1)]+1) which does the exact same as the line above it, but from the end of the array to the beginning. Negative indexing of a python array is something quite useful, take a look at the following example to get its idea completely:
Cheers!