Page 2 of 4
10474: Marbels (TLE Help Plesae)
Posted: Wed Mar 10, 2004 8:08 am
I tried to solve the problem but got TLE.
Here is my search algorithm
---- CUT AFTER AC -----
(I HAVE USE COUNTING SORT)
Help me Please to improve my time. :
Posted: Wed Mar 10, 2004 10:05 am
I also got TLE using searching.Then i use memorization and got AC.
Count the occurence of each number using array(size of 10000 and initially all 0).Then try to figure out your desire answer from this array.
And remember its a problem of TOMAL bhai.So what looks easy actually
it is not.He really makes us to think deeper and also optimal.
Posted: Wed Mar 10, 2004 10:23 am
Using linear search , O(N) will certainly get TLE.
In the real time contest ( At Daffadil ) we tried to solve it using binary search but still could not pass the time limit ( of 1 second ) .
You have to use counting sort to solve this problem.
If you are not familiar with counting sort then:
maintain a array which counts the frequecny of each number.
use another array that maintains the cumulative frequency....
then do whatever is required.
Hope it helps.
Posted: Wed Mar 10, 2004 11:36 am
I have now got WA
Please Give me some Input and Output. So that i can find my code's bug.
Posted: Wed Mar 10, 2004 8:03 pm
1 found at 1
3 found at 5
120 found at 10
7 found at 8
1234 not found
1111 found at 8
5 found at 1
10 found at 4
11 found at 6
12 found at 7
1111 found at 8
134 not found
567 not found
246 not found
24 not found
12 found at 7
435 not found
//mind it no value will be greater than 10000
Posted: Sat Mar 13, 2004 5:30 pm
Thank you Shohel bhai & Aftab Bhai for your help. I have now got AC.
Thank u very very very much.
Posted: Thu Mar 18, 2004 11:53 am
I know there's a more effective solution and sorting's not necessary, but anyway...
Using quicksort and binary search my program received a WA in
about 0,1 seconds (forgot the exact timing, less than 0,2 s in any case).
My question is: When does the OJ return a WA? Immediately after getting a wrong result or only after evaluating all sample input?
Oh, by the way, some test data would be welcome. There's no difference between my output and alu_mathics' output!
Posted: Thu Mar 18, 2004 3:19 pm
If your program not exceed time limit t finish first, and after that comparing your output witth judge output is done. Depends of result of comparing apropriate message is given to you
So if you got WA in less than 0.2 sec, than you'va been sure, that your program run trough all cases, but got wrong results to output ...
PS. When I try to solve this problem (on contest time) I have the same problem - I got WA or TLE,l depending on algorithm I use. But at end I manage wirth described below algorithm (with counting) and got Acc
Posted: Thu Mar 18, 2004 4:01 pm
I wanted to know that for quite some time, but always forgot
As to the problem, I'll try with counting sort.
Accepted using counting sort!
Posted: Mon Apr 05, 2004 9:53 pm
I have WA, but I don't understand why ...
I used quicksort & binary search. I knew there exists a quicker algorithm using counting sort , but I would like to know why this gives me WA:
type mia = array [1..10000] of longint;
i, j, k, l:longint;
le, e, lz, z, kejs:longint;
poz, lewy, srodek, prawy:longint;
while not eof do
readln (le, lz);
if (le = 0) and (lz = 0) then exit;
writeln ('CASE# ', kejs, ':');
for i:=1 to le do
readln (t );
for z:=1 to lz do
srodek:=(lewy + prawy) div 2;
if t [srodek] <> l then
if t [srodek] < l then
until (t [srodek] = l) or (lewy >= prawy) or ((lewy = prawy - 1) and (lewy = srodek));
if (lewy = srodek) and (lewy = prawy - 1) then inc (srodek);
if t [srodek] <> l then
writeln (l, ' not found') else
while (t [srodek - 1] = l) and (srodek > 1) do
writeln (l, ' found at ', srodek);
I deleted procedure quicksort, which must be right as I cut if from Free Pascal's sample qsort.pp
Posted: Wed Jun 23, 2004 11:05 am
Sth is wrong with you BS
( try to rewrite it )
BTW i think that it is why u get WA...
Posted: Sun Jun 12, 2005 8:27 am
got TLE on 10474
How can I improve????
code removed after AC
Posted: Sun Jun 12, 2005 3:08 pm
change the whole approach. the solution can be retrived in constant time.
Posted: Tue Jun 14, 2005 10:50 pm
to Salman -
You can get it within time limit using binary search, too. But you must use a faster (n log n) sorting algorithm.
to Sidky -
Will you explain what is the O(1) method? I can see a O(n) solution using counting sort & AFAIK the judge solution for this problem is based on this method, too. But O(1) seems to be too much for me.
Posted: Wed Jun 15, 2005 12:43 pm
Mohammad Mahmudur Rahman,
I suggest you read the previous posts about this problem.
What is meant by O(1) is that you can give the answer
for each Query Number in a given TEST in a O(1) time.
What I call a TEST is (A) plus (B), where:
(A) is the SET of the N numbers which are given
(B) is the SET of the Q numbers ( the SET of the query numbers )
So how it goes ?
(1) you read the N numbers which are given
(2) you do some preprocessing
(3) you start reading the Q query numbers and each time a query
number comes from the input you can give the answer for that
query number in O(1) time
The key phrase in this problem is that the numbers given are
in the interval [1,10000]. Which allows a memoization approach
to be used ( that memoization is done in the preprocessing
phase - see (2) above ).
No binary search is needed, no sorting, no complex data
structures. Two arrays of 10000 elements each will be enough.