ENG  RUSTimus Online Judge
Online Judge
Problems
Authors
Online contests
About Online Judge
Frequently asked questions
Site news
Webboard
Links
Problem set
Submit solution
Judge status
Guide
Register
Update your info
Authors ranklist
Current contest
Scheduled contests
Past contests
Rules
back to board

Discussion of Problem 1574. Mathematicians and brackets

O(n)
Posted by jagatsastry 24 Nov 2007 13:29
I used an O(n*n) method and got TLE#9. Does anyone know of any O(n) method. I actually checked every cyclic shift to see if it is a valid expression.
Re: O(n)
Posted by Victor Barinov (TNU) 24 Nov 2007 20:36
Of course there are exists O(n) algo. You just must to find 1 valid cyclic shift. And then check balance in it. Number of points with balance == 0 will be the answer to ploblem.
Re: O(n)
Posted by Denis Koshman 16 Jul 2008 21:00
There is another method - check number of points where minimal disballance is nonnegative (after checking that overall ballance is zero).
Re: O(n)
Posted by Oleg Strekalovsky [Vologda SPU] 21 Apr 2009 14:00
I think,that your algo get TLE on new tests.
Because, it's not diffucult crate test,where your program will do o(N^2).
Exampel - 49999 times '(', 50000 times ')', 1 time ')'.

My AC program find shift, not finding right bracket expression (it's O(N^2)), but by an other method ( n steps).

Good luck =)

Edited by author 21.04.2009 21:12
Re: O(n)
Posted by Zayakin Andrey[PermSU] 21 Aug 2010 01:33
There is exist  O(n*log(n)) solution.
Re: O(n)
Posted by Nic Roets 11 Nov 2011 14:41
If the sequence s is of length n, consider the sequence ss, which will have length 2n. Now define l_i as the number of '(' in the first i characters of ss and r_i as the number of ')' in the first i characters of ss. Let d_i=l_i-r_i. We want to know if there are any  numbers smaller than d_i in d_i, ... , d_{n+i}. Interval trees will give you O(n log n) algorithm, but there are even simpler algorithms that will give you an O(n) algorithm (Hint: d_i is continuous).
Re: O(n)
Posted by arrammis 28 May 2018 18:30
What do you mean when saying "point" ?? What is "point" here ? Could you please give a clear description of this method ?