← Back to topics
Topic

Brojanje - Hint

A
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. :\
d
demjan0001
well, insertion sort with 400 elements ...
because u will have max 400 different elements ...
A
Al3kSaNdaR
Ahaaa :) Now I got it. Thanks ;)
g
gates
Or you can try with segment or fenwick tree..there limit on number of different elements can be as big as N.
g
gates
interval tree = segment tree = tournament tree :)
d
demjan0001
you can use cumulative tables and binary search, too ... :D
A
Al3kSaNdaR
No trees :P I don't know them. :\ I'll solve it with improved insertion sort ASAP. ^^
g
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.
d
demjan0001
i didn't know fenwick tree = cumulative tables ...
g
gates
now you know...
A
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
p
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.
W
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
D
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.
A
Al3kSaNdaR
What's wrong now ? :\

http://www.z-trening.com/new/www/html/submit.php?submit=7100035806&subm_code=1
D
Daniel93
Maybe there is a mistake with your sorting, try to use std::sort.
p
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 :)
s
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 )