|
|
back to boardmore exactly O(N*log(K)) My algo is exactly O(n). Constant is 1. Just reading input, sometimes even not the whole input. What is K in your formula? I have O(N) solution, AC (0.156). I use "bucketing". But there is solution that works two times faster :( I think for this problem O(n) algorithms as many as stars in the sky. For example 1) qsort; 2) moving along and counting copies of equal values. I have O(N) solution, AC (0.156). I use "bucketing". But there is solution that works two times faster :( In this problem I/O hacks gave me much more speedup than algorithmic issues. My 0.078s solution uses the same linear time, constant memory algorithm as the 0.546s one. 0.062 :P just use your own procedure for reading numbers instead of fscanf. It must read numbers char-by-char Edited by author 31.10.2006 02:17 My solution uses random function and got AC! lol Hehe. I think there are no tests like this: 11 1 2 1 2 1 2 1 2 1 2 1 or you are very lucky =) 0.046 =P My program have AC(0.046) too. But I use only 96kB memory:-) Edited by author 08.03.2007 17:51 Bucketing assumes that we keep an array as big as the value range, which is one billion in this problem. So I suppose you talk about per-digit bucketing, but it is still O(N*log(K)) if we treat input numbers regardless of their base-10 representation. Did you get that constant because of limited length of particular number, given limit on its value? Just telling that it is O(N) just becase representation is fixed to base-10 is not quite far from telling that it is O(1) becase N itself is limited by 500'000 - a constant :) Edited by author 13.07.2008 20:45 Edited by author 13.07.2008 20:45 The O(n) algorithm is Boyer-Moore Majority Vote Algorithm, which time is linear, and memory constant... in fact you don't need more than 3 integer variables to run this algorithm... As some people have mentioned, writing your own int input procedure may improve your performance a lot. First time with no optimization i got 1.06s, then i used a getc() based int input procedure and did some little changes in the code and got 0.046s :) One more O(N) algo is K-th ordered statistics, which is explained in Kormen et. al. So, will program, which reads number and increases the variable, which counts how many times did we meet that number get AC, but not TLE? Thank you for this great suggestion! How did you guys optimize I/O ? Can send me your I/O procedure, my email : tungnk1993@gmail.com i tryed many different ways but always got tle on 19 or 20 tests .Than i saw Boyer-Moore Majority Vote Algorithm.it's really good algo for this problem .Here is algo http://www.cs.utexas.edu/~moore/best-ideas/mjrty/index.html. here is'n written than number of majority element must be more than half so i thought that it would write right answer in any case for example in that case B B B B A A A A A C C C but no .in that algo it's very important the number of majority element to be more than (n/2) thanks . Sorry for bad English. Edited by author 17.01.2012 14:52Boyer-Moore is one of those "brilliant in its simplicity" types of algorithms. Beautiful! |
|
|