Hints for Contest 4 problems:
The idea and motivation behind these hints are that you should only open them up after spending a decent amount of trying to solve the problem yourself. Also, open the hints in sequential order.
Example: Try the problem for 40 mins, then open hint 1. Then using hint 1 only, try the problem for another 20-30 minutes. If still unable to make much progress only, then open hint 2 and so on.
The benefit of knowing a partial solution rather than the complete solution is that you can work out the later stages of the problem yourself, thus improving your problem-solving skills.
TLDR; use the hints cautiously, if you start relying on them too much, it can hamper with your learning process.
Binary search on data structure (containing all the top disk radii) to find the smallest top disk radius which the current disc can be placed on top of.
In total, you’ll have three operations you want to handle: adding a new disc, binary searching for a disc and removing a disc.
Simulate the process, using the multiset. Be careful not to use a set because of duplicates being removed.
Consider the answer for some array to be the sum of the number of violations within the left half of the array, the number of violations within the right half of the array, and the number of violations that cross the halfway point in the array.
For every element in the left half of the array, there is some number of elements in the right half of the array smaller than it. Each such pair will create one violation.
For every element in the left half of the array, binary search for the latest position in the right half which is \le it, and add the number of positions to the answer crossing the midpoint.
Combine answers to the left half, right half, and across the midpoint cases to solve the problem.
The bottom of the square is on the x-axis and the diagram is symmetrical across the y-axis.
Binary search for the x-coordinate of the bottom-right corner of the square.
Let the answer to your binary search be X.
Since it’s a square, the base is of side 2X.
The triangle base must also be on the line y=2X
Binary search for the rightmost point on the base of the triangle. Let the answer to this be R.
Similarly, binary search for the top point of the triangle. Let the answer to this be T.
You’ve got the dimensions, combine them to find the area:
2X * 2X + 2Y * (T - 2X) / 2
Consider the alternative problem, “Given an integer X, find the number of pairs (i, j) such that A[i] + B[j] ≤ X.”
How can you use this modified problem to solve the original problem?
If you binary searched over X for the minimum X such that the answer was ≥ Q, this minimum X would be the answer to a query Q.
How can you solve this modified problem?
Sort both arrays A and array B.
For each integer Y in array A, all integers ≤ X - Y in array B will create a valid pair. You can binary search for the number of valid pairs in B. This will take O(NlogN) time.
Can we solve previous hint in O(N) time using two-pointers?
Use a similar technique to the previous problem (LOWSUM).
Reach the end starting with some number of soldiers X.
Notice that if you can reach the end starting with X soldiers, you must be able to reach the end with X+1 soldiers as well.
Binary search for X!
Use that fact that is a substring is valid then all the substring formed are formed by this also valid.
For each i from 1 to N, we can find the j such that S[i, j] is valid and S[i, j+1] is not and denote this as ans[i].
For each query of type (l, r), we can find a value cnt for which ans[cnt] \le r.
Answer = sum of ans[i] from 1 to cnt - sum of ans[i] from 1 to (l-1) + (R-k)*(R+1) - (R*(R+1)/2 - L*(L-1)/2).
Use prefix sum to produce the sum of first terms of ans array. Find the cnt value using binary search.
Sort the points by x-coordinate and divide the points into two half and find the answer for each half.
The answer of the first half is ans_1 and the second half is ans_2 then answer of the combination of two half is max(ans_1, ans_2, answer produced by points such that one lie in the first half and the second in second half).
For each point p_i in the first part, we are only looking for the points p_j in the second half such that p_i.y - min(ans_1, ans_2) < p_j.y < p_i.y.
To know more about the above hint please refer to this:
Suppose ans_1 and ans_2 denote the minimum perimeter for the first and the second half and current_ans (without merge) is min(ans_1, ans_2).
Now, we need to check those that can form by merging these two half.
Tringle whose maximum length side is less then current_ans/2 can contribute to the final answer.
Let the answer be denoted by X and let the maximum transaction between any pair of friends be denoted by Y. Note that as X increases, Y either remains the same or also increases. That is, f(X) = Y is a monotonically increasing function. Therefore, we can perform a binary search on the function.
Now, we have to write a function g(x) that returns true or false depending on whether it’s possible for all friends to have >= x coins.
In order to write g(x), a key observation is that in a sorted list of coins, friend i would only take coins from a friend with index > i. Therefore, for each prefix of friends, we can check if the remaining suffix can transfer the required coins to the prefix without breaking the condition. This function should be in O(N).