Блог пользователя shahincsejnu

Автор shahincsejnu, история, 4 года назад, По-английски

I was trying to solve 279C

but getting WA in test case-21, the case is huge that is why cannot figure out why getting WA.

My Approach : First i find out all ladder segment using two-pointer technique, then i used binary search to find whether the given segment is in any ladder segment. I think my logic correct but still why getting WA. My Code

  • Проголосовать: нравится
  • +9
  • Проголосовать: не нравится

»
4 года назад, # |
  Проголосовать: нравится +7 Проголосовать: не нравится

As you can see per my submission history on this task, I had the same problem :)

3 1
7 4 4
1 3

On this test your code gives a wrong answer.

»
4 года назад, # |
Rev. 5   Проголосовать: нравится +1 Проголосовать: не нравится

This can be solved in $$$O(M+N)$$$, which should be better complexity that your binary search solution.

(Pseduo-code):

vector<int> l(n, 0), r(n, n - 1), pre(n);
for (int i = 1; i <= n - 1; ++i)
	l[i] = (a[i] >= a[i - 1]) ? l[i - 1] : i;
for (int i = n - 2; i >= 0; --i)
	r[i] = (a[i] >= a[i + 1]) ? r[i + 1] : i;
for (int i = n - 1; i >= 0; i = l[i] - 1)
	for (int j = l[i]; j <= i; ++j)
		pre[j] = r[i];
while (m--) { 
	cin >> li >> ri; --li; --ri; 
	println((ri <= pre[li]) ? "Yes" : "No"); 
}

Array $$$a$$$ is taken as input (0-based index)

For $$$i = 0 .. n - 1$$$, define $$$l_i$$$ as the leftmost(smallest) index such that $$$a[l_i] \leq a[l_{i}+1] \leq ... \leq a[i] $$$ Note: $$$l_i <= i$$$

Similarly, for $$$i = 0 .. n - 1$$$, define $$$r_i$$$ as the rightmost (largest) index such that $$$a[i] \geq ... \geq a[r_{i}+1] \geq a[r_i] $$$

Then to process each query in $$$O(1)$$$, we precompute for each index $$$i \in 0 .. n - 1 : pre[i] = $$$ the rightmost point of any ladder passing through index $$$i$$$.

For DP recurrences, see code. Note the order of the for loops. For $$$r_i$$$, we need to go in reverse.

If anything is unclear, let me know.

I didn't understand your solution. What exactly are you storing in the array that you do binary search on? Its hard to debug your code when if you don't tell your idea..

  • »
    »
    4 года назад, # ^ |
      Проголосовать: нравится +3 Проголосовать: не нравится

    I understood your approach. Thanks!!

    Anyway, what i did in my code is : i iterate the array and by two pointer i took the largest valid ladder and push their starting and ending point in pair of vector. Now, i know how many ladder i have, than i did binary search on the ladders and tried to find out whether given segment lies in any of ladder.