← Back to topics
Topic

Brojanje - Hint

Al3kSaNdaR
Can someone give me a hint for solving task " Brojanje " ? I have tryed using sorting -> http://www.z-trening.com/new/www/html/submit.php?subm_stat=1&submit=7100034629 and inserting http://www.z-trening.com/new/www/html/submit.php?subm_stat=1&submit=7100035184 but I get TLE. :\
demjan0001
well, insertion sort with 400 elements ...
because u will have max 400 different elements ...
Al3kSaNdaR
Ahaaa :) Now I got it. Thanks ;)
gates
Or you can try with segment or fenwick tree..there limit on number of different elements can be as big as N.
RobertGerbicz
My solution is using interval tree.
gates
interval tree = segment tree = tournament tree :)
demjan0001
you can use cumulative tables and binary search, too ... :D
Al3kSaNdaR
No trees :P I don't know them. :\ I'll solve it with improved insertion sort ASAP. ^^
gates
@demjan0001:

fenwick tree = cumulative tables;
as i said...and with cumulative tables you can do it in O( lg N ) without additional binary search and additional lg N factor.
demjan0001
i didn't know fenwick tree = cumulative tables ...
gates
now you know...
Al3kSaNdaR
I have tried but I still get TLE on 2 test cases. :( Can you tell me what's wrong ?

http://www.z-trening.com/new/www/html/submit.php?submit=7100035491&subm_code=1
picsel
I've written a similar code in Pascal that couldn't pass first test case because of TLE. Then I just wrote the same one in C++ and it passed. Reading input is faster in C++ and it helps here.
WindListener
Can anyone tell me what's wrong with my code??

http://www.z-trening.com/new/www/html/submit.php?subm_stat=1&submit=7100035544
Daniel93
Well, you are using vector::insert, and it has a time complexity of O( n ), and you call it in worst case 100000 times, so its sure you get TLE.
Al3kSaNdaR
What's wrong now ? :\

http://www.z-trening.com/new/www/html/submit.php?submit=7100035806&subm_code=1
Daniel93
Maybe there is a mistake with your sorting, try to use std::sort.
picsel
You can find tests on the top of the page here: http://www.yuoi.nis.edu.rs/takmicenja/2009.1.okr/3.brojanje/task.html

So download them and check them yourself :)
stjepang
You can also use cumulative tables / fenwick tree without binary search ;) ( O( log n ) query, of course )
( just have a look at my code )