Please read the new rule regarding the restriction on the use of AI tools. ×

Riyad_Hossain's blog

By Riyad_Hossain, 3 hours ago, In English

Recently, I have tried a problem: 1893B - Neutral Tonality and solved it with O(NlogN) tc.

Problem Summary: Insert array b into array a at any position while making sure that the LIS is minized.

Approach: For an element el of the array a insert the elements of b which are greater than or equal to el.

However, the solution got TLE on the third test case: Submission 282567664

What's wrong in my approach/code? Can anyone help me to debug this problem?

Code: Used multiset to find elements and map to store the elements.


void solve() { int n, m, x; cin >> n >> m; vi a(n); multiset<int> ms; for (int i = 0; i < n; i++) cin >> a[i]; for (int i = 0; i < m; i++) cin >> x, ms.insert(x); map<int, vi> elements; forr(i, 0, n - 1) { auto it = lb(a[i], ms); while (it != ms.end()) { elements[i].pb(*it); ms.erase(it); if (!sz(ms)) break; it = lb(a[i], ms); } } forr(i, 0, n - 1) { reverse(all(elements[i])); for (int v : elements[i]) printsp(v); printsp(a[i]); } while (sz(ms)) printsp(*ms.rbegin()), ms.erase(--ms.end()); }
  • Vote: I like it
  • -2
  • Vote: I do not like it

»
3 hours ago, # |
  Vote: I like it +1 Vote: I do not like it

Try replacing

lb(a[i], ms)

With this

ms.lower_bound(a[i])

NOT THAT (That is wrong)

lower_bound(ms.begin(), ms.end(), a[i])
  • »
    »
    3 hours ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    Thanks for your suggetion.

    I tried 282571062. But Again TLE

    • »
      »
      »
      3 hours ago, # ^ |
        Vote: I like it +1 Vote: I do not like it

      Change this too

      • »
        »
        »
        »
        3 hours ago, # ^ |
          Vote: I like it 0 Vote: I do not like it

        Oops, my bad.

        It worked 282572959.

        Thanks a lot Hexagons. You're truly a generous person.

        Can you enlighten me why the previous way didn't work?

        • »
          »
          »
          »
          »
          2 hours ago, # ^ |
            Vote: I like it +1 Vote: I do not like it

          The iterator of multiset is a bidirectional iterator, and not a random-access iterator.

          Hence you should use the method for lower bound. Otherwise, there can be a $$$O(N)$$$ operation when doing lower_bound.

          • »
            »
            »
            »
            »
            »
            11 minutes ago, # ^ |
              Vote: I like it 0 Vote: I do not like it

            oh, I see. Thanks sahaun