Answers to: INVENTRY - EDITORIALhttps://discuss.codechef.com/questions/139516/inventry-editorial<h1>PROBLEM LINK:</h1>
<p><a href="http://www.codechef.com/problems/INVENTRY">Practice</a> <br>
<a href="https://www.codechef.com/SNCKPE19/problems/INVENTRY">Contest</a> </p>
<p><strong>Setter:</strong> <a href="https://www.codechef.com/users/kingofnumbers">Hasan Jaddouh</a> <br>
<strong>Tester:</strong> <a href="https://www.codechef.com/users/mgch">Misha Chorniy</a> <br>
<strong>Editorialist:</strong> <a href="http://www.codechef.com/users/taran_1407">Taranpreet Singh</a> </p>
<h1>DIFFICULTY:</h1>
<p>Easy</p>
<h1>PREREQUISITES:</h1>
<p>Greedy and Observations.</p>
<h1>PROBLEM:</h1>
<p>Given $N$ positions numbered from 1 to $N$, which each cell either empty or containing a box, we need to move each box such that if the total number of boxes is $K$, the boxes end at leftmost $K$ positions.</p>
<p>The chef can move boxes in two ways, as follows, each being considered as a separate move.</p>
<ul>
<li>If positions $i$ and $i+2$ are empty, and position $i+1$ contain a box, we can move box to position $i$. Call it PUSH operation OR</li>
<li>If positions $i+1$ and $i+2$ are empty, and position $i$ contains a box, we can move box to position $i+1$. Call it PULL operation.</li>
</ul>
<p>Determine the minimum number of moves to achieve the task, or determine if its impossible to achieve the task.</p>
<h1>SUPER QUICK EXPLANATION</h1>
<ul>
<li>First of all, the boxes already lying to the border do not affect answer at all, since they will never be moved. Ignore them, along with their positions.</li>
<li>Secondly, if we number the boxes from 1 to $K$ from left, we can know that we need to apply PUSH operation exactly $P_i-x$ where box numbered $x$ is present at $P_i$ position.</li>
<li>After ignoring boxes as well as their positions as mentioned in point 1, if we can, using PULL operation, can place stones at the gap of 1, without reaching the last cell, the answer is possible. Otherwise, it's impossible to move all boxes.</li>
<li>Now, After getting all stones at gaps of 1, we can move them to leftmost positions using PUSH operations. See, if P is the current position, and There are $X-1$ stones to the left of current stone, The final position of current stone will be $X$, which we'll reach by applying PUSH operation $P-X$ times.</li>
<li>The crux of the idea is, to apply the minimum number of PULL operations to ensure that we can apply PUSH operation on every stone not already at its intended position and then apply required number of PUSH operations to get the stone at its leftmost position.</li>
</ul>
<h1>EXPLANATION</h1>
<p>See, if we have some boxes already at leftmost positions, something like <code>###.#...#..</code>, we can simply ignore the first three boxes, and solve the problem for 1.#...#..1 The answer will remain the same.</p>
<p>After this, number the boxes. Considering the above example, <code>.1...2..</code> is the configuration. We also know, that box numbered will move to the first position, box numbered 2 will move to the second position, requiring minimum $(pos-number)$ operations for each box, if we apply no PULL operation.</p>
<p>Each PUSH operation moves box one position to the left, while each PULL operation moves the box one position. We know the Initial position, say $P_I$, as well as the final intended position of the box, call if $P_F$, so, if we apply $x$ PULL operations and $y$ PUSH operations, then the relation will always satisfy.</p>
<p>$P_F = P_I+x-y$.</p>
<p>Rewriting it as $P_I-P_F + x = y$.</p>
<p>We need to minimize the number of operations, so, we will try to choose the minimum value of x, that is, Minimum number of PULL operations. We can easily find the Total number of operations (x+y) if we know $x$.</p>
<p>Now, Let's see, how we can move boxes to the left.</p>
<p>We can prove, that we can apply all PULL operations first, followed by PUSH operations. The proof is left as an exercise. For proof, try to solve the case <code>.##.#.#...</code> Everything will be clear.</p>
<p>To apply PUSH operation on the box at position p, we need both positions $p-1, p+1$ to be empty. Position $p-1$ will be automatically empty if we start considering boxes from the left. Now, to empty position $p+1$ we have to apply PULL operation at the box at position p+1. To achieve that, we need to push box immediately to the right of p+1 at position p+3. This procedure repeats, each time, we need to move boxes till we have all boxes in form probably</p>
<p><code>.#.#.#.#.#....#.#.#.</code></p>
<p>Gaps may occur, do not worry about them, The important thing is, that no two stones are adjacent.</p>
<p>This form is necessary because we cannot apply PUSH operation at a box at position p unless position p+1 is empty, and that never happens, if there are two boxes adjacent to each other (unless these boxes are already at leftmost position, already ignored)</p>
<p>Hence, we can find the position where the current box shall reach, if we know the position previous box will reach. If both positions p and p+1 are not empty, the box at p+1 will reach p+2 and so on.</p>
<p>Consider example <code>.###...</code></p>
<p>We can simulate, that We will arrive at position <code>.#.#.#.</code>, moving the second box by 1 and third box by 2 pull operations.</p>
<p>Consider example <code>.##.#..</code></p>
<p>Again, we can see, that we will arrive at the same position with 2 PULL operations. Now, knowing total pull operations, we can count the total number of moves too.</p>
<p>The idea is, that if we intend to move the previous box at position pos, the current box will be moved to position $max(P, pos+2)$ because we need the box beyond pos+1. This becomes the intended position for the current box. </p>
<p>This way, we try to get the boxes at gaps of at least 1 by applying the minimum number of PULL operations.</p>
<p>After that, we can apply PUSH operation at all stones and count the number of PUSH operations required which is just the distance between leftmost position it can reach and its current position after PULL operations.</p>
<p>Now, If still doubtful, try to simulate the whole process, for example, <code>.####......</code> step by step.</p>
<p>See the secret box.</p>
<div class="hidden-block"> <div class="hide-bar view">View Content</div> <div class="hide-bar hide" style="display:none;">Hide Content</div> <div class="hidden-content" style="display:none;">
<p><code>.####....</code> <br>
<code>.###.#...</code> <br>
<code>.###..#..</code> <br>
<code>.##.#.#..</code> <br>
<code>.##.#..#.</code> <br>
<code>.##..#.#.</code> <br>
<code>.#.#.#.#.</code> <br>
<code>#..#.#.#.</code> <br>
<code>#.#..#.#.</code> <br>
<code>##...#.#.</code> <br>
<code>##..#..#.</code> <br>
<code>##.#...#.</code> <br>
<code>###....#.</code> <br>
<code>###...#..</code> <br>
<code>###..#...</code> <br>
<code>###.#....</code> <br>
<code>####.....</code> </p>
</div></div>
<p><strong>Impossible case</strong></p>
<p>Suppose, the Intended position of any box we calculate as above, is above $N$, or its the last position, then the answer is impossible.</p>
<p>Because, we cannot move any box outside the range, and secondly, if we move a box to last position (how will you??), we cannot apply either PUSH as well as PULL operation, Hence, we can never move that box to its intended position, making answer impossible.</p>
<p>That's all for this problem.</p>
<p><strong>Challenge</strong></p>
<p>Determine the minimum number of boxes for a given $N$, such that it is impossible to move them to correct positions.</p>
<p>Determine the maximum number of boxes for a given $N$, such that it is possible to move them to correct positions.</p>
<p>Enjoy :P</p>
<h1>Time Complexity</h1>
<p>Time complexity is $O(|S|)$ per test case.</p>
<h1>AUTHOR'S AND TESTER'S SOLUTIONS:</h1>
<p><a href="http://www.codechef.com/download/Solutions/SNCKPE19/setter/INVENTRY.cpp">Setter's solution</a><br>
<a href="http://www.codechef.com/download/Solutions/SNCKPE19/tester/INVENTRY.cpp">Tester's solution</a> <br>
<a href="http://www.codechef.com/download/Solutions/SNCKPE19/editorialist/INVENTRY.java">Editorialist's solution</a> </p>
<p>Feel free to Share your approach, If it differs. Suggestions are always welcomed. :)</p>enMon, 12 Nov 2018 17:05:39 +0530Answer by ajaysaundeephttps://discuss.codechef.com/questions/139516/inventry-editorial/140101<p><strong>strong text</strong></p>
<pre><code>i tried every testcases but my code is giving wrong answer.
my solution:<a href="https://www.codechef.com/viewsolution/21613166">https://www.codechef.com/viewsolution/21613166</a>
<a href="/users/211453/taran_1407"><a href="/users/211453/taran_1407">@taran_1407</a></a> pls help
</code></pre>ajaysaundeepMon, 12 Nov 2018 17:05:39 +0530https://discuss.codechef.com/questions/139516/inventry-editorial/140101Answer by nilmadhab1https://discuss.codechef.com/questions/139516/inventry-editorial/139667<p>if we apply x PULL operations and y PUSH operations, then the relation will always satisfy.</p>
<p>PF=PI−x+y.</p>
<p>I think it should be PF = PI + x - y as while pull PI will go right and after that apply y push and it goes at PF position. </p>nilmadhab1Tue, 06 Nov 2018 09:12:48 +0530https://discuss.codechef.com/questions/139516/inventry-editorial/139667Answer by robertkinghttps://discuss.codechef.com/questions/139516/inventry-editorial/139609<p>access denied for the testers solution.</p>
<p>I decided to use OO for this solution just to make the box logic easier. I worked out the position of a box based on its previous box.</p>
<pre><code> class Box:
def __init__(self, i, prev):
self.prev = prev
self.i = i
self.order = 0
self.rightPos = i
if self.prev is not None:
self.order = prev.order + 1
self.rightPos = max(self.rightPos, self.prev.rightPos + self.prev.spaceNeeded + 1)
self.spaceNeeded = 0
if self.order < self.i:
self.spaceNeeded = 1 #needs push
if self.rightPos > self.i:
self.spaceNeeded = 1 #needs pull
def canFit(self, a):
return self.rightPos + self.spaceNeeded < len(a)
def distance(self):
right = self.rightPos - self.i
left = self.rightPos - self.order
return right + left
def f(a):
ans = 0
prevBox = None
for i, v in enumerate(a):
if v == '#':
box = Box(i, prevBox)
if not box.canFit(a):
return -1
ans += box.distance()
prevBox = box
return ans
def test():
tests = """##.#.. 1
.#.#.#. 6
##.## -1
##.#.. 1
.#.#.#. 6
###.# -1
.##. -1
.##.. 4
#.##.. 4
##.##.. 4
.#.#..#.... 7
.###.. -1
.###... 9
.###..#.. 14"""
for line in tests.splitlines():
a, ans = line.split()
if f(a) != int(ans):
print(a, f(a), ans)
test()
for _ in range(int(input())):
n = int(input())
a = input().strip()
print(f(a))
</code></pre>robertkingMon, 05 Nov 2018 04:43:10 +0530https://discuss.codechef.com/questions/139516/inventry-editorial/139609