Page 1 of 6
10487 - Closest Sums
Posted: Mon May 05, 2003 4:17 pm
hi this is my algorithm
1.scan the input
2.store each two distinct closest sum in array
3.scan the query
4.search the answer from the array sequentially
but it's slow to get accepted.
can anyone give me a better approach in this problem???
Posted: Tue May 06, 2003 8:43 am
I got Acc in the same way, but ....
after creating all possible pairs I sort them and use bsearch to search query values ...
I don't know how we could get better time in this problem ...
Posted: Tue May 06, 2003 8:58 am
You can sort the input values and find the answer to each query (without constructing all possible sums) in linear time. Think about it, it is not too difficult.
Posted: Tue May 06, 2003 9:43 am
I do similar things.
1). Read input
2). Add distinct numbers
3). Sort the sum by shell sort
4). Read query
5). Binary search the answer
But I get TLE.
Can anyone help me?
Posted: Tue May 06, 2003 10:34 am
I think that this problem can solve with no sort.
Use hashing, instead of sort... The range of input data is only 1000...
So you can use hashing technique by array.
Posted: Tue May 06, 2003 10:46 am
but why my program got rte.
what may be array size of distinct number array?
Posted: Tue May 06, 2003 11:39 am
shell sort has time complexity O(N^2) I think, try to use qsort
this table should have N*N size ... maybe this cause error ...
Posted: Tue May 06, 2003 1:11 pm
Oh... actually I neither know how to implement quick sort in PASCAL nor hashing. I really need to learn a lot.
Btw, I think the table should have only N*(N-1)/2 size. I haven't declare so large.
Posted: Tue May 06, 2003 2:15 pm
You have right - I always try to declare only such big tables as is necessary ...
Try to search net for QSORT or QUICK SORT and you take a lot of sources I think
Posted: Wed May 07, 2003 10:10 am
Thanks, I have now re-written the code with quicksort and got accpeted in a second.
I think I will not use other sorting afterward.
Btw, is it a stable sort?
Posted: Wed May 07, 2003 12:57 pm
to anupam :
before this I always get Run time error
Posted: Wed May 07, 2003 1:02 pm
btw the algorithm that I describe above
(without any sorting and bsearch)
is accepted in 0.9 sec
but I see that in top ten rank
for this problem,
the time to get accepted is incredible
and I still couldn't figure out how
to do this in linear time
10487, 10489 and 10490
Posted: Thu May 08, 2003 7:41 pm
I have problems with this three programs. Any help is appreciated!
a: array [1..1000] of integer;
readln(input,n); c:= 0;
While (n<>0) do
Inc(c); writeln(output,'Case ',c,':');
for i:= 1 to n do
for i:= 1 to m do
readln(input,s); t:= maxint;
for j:= 1 to n do
for k:= j+1 to n do
if abs(a[j]+a[k]-s) < abs(t-s) then t:= a[j] + a[k];
writeln(output,'Closest sum to ',s,' is ',t,'.');
for i:= 1 to t do
readln(input,n,b); s:= 0;
for j:= 1 to b do
read(input,m); p:= 1;
for k:= 1 to m do
writeln(output,s mod n);
Function Primo(n: Longint): boolean;
res:= true; i:= 3;
if (n mod 2= 0) then res:= false;
if (n mod i= 0) then
if (n=2) or (n=3) then res:= true;
While (n<>0) do
r:= exp(n*ln(2)) -1;
if primo(trunc(r)) then writeln(output,'Perfect: ',r*exp((n-1)*ln(2)):0:0,'!')
else if primo(n) then writeln(output,'Given number is prime. But, NO perfect number is available.')
else writeln('Given number is NOT prime! NO perfect number is available.');
Posted: Thu May 08, 2003 10:25 pm
For 10487, probably the solution to your WA is to ignore the (a+b) where a == b ...
For 10489, I don't think your p := p*a statement can survive overflow problem eventhough you declared p as longint.