Answers to: CLRL - Editorialhttps://discuss.codechef.com/questions/116716/clrl-editorial<h1>PROBLEM LINK:</h1>
<p><a href="http://www.codechef.com/problems/CLRL">Practice</a><br>
<a href="http://www.codechef.com/NOV17/problems/CLRL">Contest</a></p>
<p><strong>Author:</strong> <a href="http://www.codechef.com/users/abizerl123">Abizer Lokhandwala</a><br>
<strong>Tester:</strong> <a href="http://www.codechef.com/users/iscsi">Istvan Nagy</a><br>
<strong>Editorialist:</strong> <a href="http://www.codechef.com/users/melfice">Oleksandr Kulkov</a></p>
<h1>DIFFICULTY:</h1>
<p>CAKEWALK</p>
<h1>PREREQUISITES:</h1>
<p>None</p>
<h1>PROBLEM:</h1>
<p>Chef searched a given number (rating) in a given <strong>sorted</strong> array (in ascending order) through a sequence of queries of the form "which number is at position $i$?". The answers to these queries are given, the indices are unknown. Determine whether some queries in the sequence are redundant (from previous queries and monotonicity of the array it is clear that given number cannot be located at the position of the query).</p>
<h1>QUICK EXPLANATION:</h1>
<p>Process the ratings one-by-one, maintain range of possible values of Reziba's rating (initially $[-\infty, +\infty]$, where). If the current rating does not belong to the range, the sequence is redundant and the answer is <code>NO</code>. Depending on whether the rating is higher or lower than Reziba's, change the corresponding bound of the range.</p>
<h1>EXPLANATION:</h1>
<p>When can we conclude that the sequence has mistakes? In problem statement, there is only one constraint:</p>
<p><code>However, Chef will never go beyond a person whose rating Chef has asked before. For example, if Chef was walking to the left and finds a person who already told him to walk to the right then he will not continue going to the person's left.</code></p>
<p>As we know the ratings of the persons and Reziba's rating, we know the direction Chef was told by each person. Therefore, the problem is to find out whether Chef, after being directed by some person, for example, to go left, will pass that person again going right; that is, there is a rating that is lower somewhere later in the sequence.</p>
<p>Formalizing what was said above, if for some $i$ we have $A_i>R$, for any $j>i$ it should be $A_j < A_i$. Similarly, if for some $i$ $A_i < R$, for any $j > i$ it should be $A_j > A_i$. It is clear that for each $i$, the constraints on $A_i$ have the form $X_i < A_i < Y_i$, where</p>
<p>$X_i = max \{A_j : j < i, A_j < R\}$</p>
<p>$X_i = min \{A_j : j < i, A_j > R\}$</p>
<p>We can process the ratings one-by-one, maintaining $X_i$, $Y_i$ (in editorialist's solution they are named $min$_$rating$ and $max$_$rating$) and checking the constraints one-by-one.</p>
<p>The solution works in $O(N)$ time, because we do a constant amount of operations for each input rating, and requires $O(1)$ additional memory (apart from $O(N)$ memory to store the input array) because we use a constant amount of variables.</p>
<h1>AUTHOR'S AND TESTER'S SOLUTIONS:</h1>
<p>Author's solution can be found <a href="http://www.codechef.com/download/Solutions/NOV17/Setter/CLRL.cpp">here</a>.<br>
Tester's solution can be found <a href="http://www.codechef.com/download/Solutions/NOV17/Tester/CLRL.cpp">here</a>.<br>
Editorialist's solution can be found <a href="http://www.codechef.com/download/Solutions/NOV17/Editorialist/CLRL.cpp">here</a>.<br></p>
<h1>RELATED PROBLEMS:</h1>enTue, 22 May 2018 04:09:01 -0000