EQIDLIS: Getting wa

Once more, I’m convinced that there is some really small bug that’s giving me wrong answer. Could you give me some text example that fails when running program.

program eqidlis;
var
t,n,i,j,k,middle,ceil,temp,res:integer;
equal:boolean;
idli:array[1..3000] of integer;
begin
readln(t);
for k:=1 to t do
  begin
  readln(n);
  for i:=1 to n do read(idli[i]);
  for i:=2 to n do
    begin
    j:=i;
    while (j>1) and (idli[j-1]>idli[j]) do
      begin
      temp:=idli[j-1];
      idli[j-1]:=idli[j];
      idli[j]:=temp;
      Dec(j);
      end;
    end;
  middle:= n div 2; j:=n; res:=0;
  for i:=1 to middle do
    begin
    ceil:=idli[j]-idli[i];
    if ceil=0 then continue
      else
      begin
      Inc(res);
      if ceil mod 2=0 then ceil:=ceil div 2
        else ceil:=(ceil div 2)+1;
      idli[i]:=idli[i]+ceil;
      idli[j]:=idli[j]-ceil;
      end;
    Dec(j);
    end;
  temp:=idli[1]; equal:=true;
  for i:=2 to n do
    if idli[i]<>temp then begin equal:=false; break; end;
  if equal then writeln(res)
    else writeln('-1');
  end;
end.

Thanks :slight_smile:

I really don’t understand much of pascal hence can’t figure out what exactly is happening here. You can view the editorial for this problem at http://www.codechef.com/wiki/may-cookoff contest-problem-editorials and compare your solution with the expected solution.

Well, this is pretty much what I did… Still, coud you provide some test cases, cause I still don’t see what’s wrong :slight_smile:

please describe precisely the approach you used.

First, I sort out the array. Then starting from counter1=1 to middle (length of the array div 2) and back from counter2=length of the array to the middle I calculate the ciel difference of each element ciel(array[counter1],array[counter2]) the way it is described. Then, I check whether all the members of array are the same. If not print -1 else print res. That’s it.

1
5
4 4 5 1 1

you should try to generate input test files yourself. :slight_smile:

1 Like

Well I did for half an hour and each time I got right result :slight_smile:

It gives me -1 … and it should give -1 :smiley:

Well I did for half an hour and each time I got right result :slight_smile:

no. the correct answer is 3.

4 4 5 1 1 --> 4 4 3 3 1 --> 3 4 3 3 2 --> 3 3 3 3 3