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());
}
Try replacing
With this
NOT THAT (That is wrong)
Thanks for your suggetion.
I tried 282571062. But Again TLE
Change this too
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?
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
.oh, I see. Thanks sahaun