ENG  RUSTimus Online Judge
Online Judge
Problems
Authors
Online contests
About Online Judge
Frequently asked questions
Site news
Webboard
Links
Problem set
Submit solution
Judge status
Guide
Register
Update your info
Authors ranklist
Current contest
Scheduled contests
Past contests
Rules
back to board

Discussion of Problem 1005. Stone Pile

Why my algorithm is wrong?
Posted by Shogal [Kaliningrad] 10 Sep 2004 00:16
I tried to solve problem with this algorithm:

1) Read numbers
2) Sort array descent
3) Put each element into smallest of two piles (start with largest element)
4) Answer will be abs(p1-p2)

And when I try - wrong answer in 5-th test appears.

What is wrong?

=============
Sourcecode :
=============
var S : array[0..20] of longint;
____N : integer;
____T : longint;
____I, J : integer;
____P1, P2 : longint;
begin
____read(N);
____for I := 1 to N do Read(S[I]);
____for I := 1 to N-1 do
________for J := I+1 to N do
____________if S[J] >= S[I] then begin
________________T := S[I];
________________S[I] := S[J];
________________S[J] := T;
____________end;
____P1 := 0;
____P2 := 0;
____for I := 1 to N do
________if P1<P2 then P1 := P1 + S[I]
_________________else P2 := P2 + S[I];
____write(abs(P2-P1));
end.

( '_' are whitespaces, I added them for most comfortable reading )
Re: Why my algorithm is wrong?
Posted by Stefan Gheorghe 10 Sep 2004 14:48
Well, so I did it at first time and got WA. Solution is DP. Here is a test which it fails: 5 weights: 6 6 5 5 2
your algo outputs 2 which is false, correct is 6, 6 and 5, 5, 2 -> 0 difference
No subject
Posted by Thomas Tham 13 Aug 2005 08:35


Edited by author 13.08.2005 08:38