BESTBATS: Getting wa

Believe it or not, Wrong answer :smiley:
I was solving Top Batsmen using only mathematical methods (using combination equations that we all know) but I still get wrong answer… Please provide me with some test examples so I can debug my app :smiley:

Here is my code :

program bestbats;
var
counter,t,l,k,i,j,temp,found1,found2:integer;
fact1,fact2,res:longint;
team,sorted:array[1..11] of integer;
begin
readln(t);
for l:=1 to t do
  begin
  for i:=1 to 11 do read(team[i]);
  for i:=2 to 11 do
    begin
    j:=i;
    while (j>1) and (team[j]>team[j-1]) do
      begin
      temp:=team[j];
      team[j]:=team[j-1];
      team[j-1]:=temp;
      Dec(j);
      end;
    end;
  readln(k); res:=0;
  for i:=1 to k do
    sorted[i]:=team[i];
  counter:=1;
  while counter<=k do
    begin
    temp:=sorted[counter]; found1:=0;
    while sorted[counter]=temp do
      begin Inc(counter); Inc(found1); end;
    found2:=0;
    for i:=1 to 11 do
      if team[i]=temp then Inc(found2);
    fact1:=1;
    fact2:=1;
    if found2<>found1 then
      begin
      for i:=1 to found1 do
        begin
        fact1:=fact1*i;
        fact2:=fact2*found2;
        Dec(found2);
        end;
      Res:=Res+(fact2 div fact1)-1;
      end;
    end;
  Res:=Res+1;
  WRITELN(res);
  end;
end.

1
1 1 1 1 1 1 1 1 1 1 1
11

border cases. :smiley:

Prints out 1

and that’s the one I tried when I manually wrote test examples :smiley:

FPC 2.4.4
I see what might be the problem…
while sorted[counter]=temp do
begin Inc(counter); Inc(found1); end;
Here it will increase the counter to 12… Which will lead to this error maybe… But I don’t get that error :smiley:
Can’t see what’s wrong :smiley:

Just like I told you, 12 is not a problem… It still gives me 1 with this test example…
Anyway, I made correction regarding that part of code

program bestbats;
var
counter,t,l,k,i,j,temp,found1,found2:integer;
fact1,fact2,res:longint;

team,sorted:array[1…12] of integer;

begin
readln(t);
for l:=1 to t do
  begin
  for i:=1 to 11 do read(team[i]);

team[12]:=-1; sorted[12]:=-1;

  for i:=2 to 11 do
    begin
    j:=i;
    while (j>1) and (team[j]>team[j-1]) do
      begin
      temp:=team[j];
      team[j]:=team[j-1];
      team[j-1]:=temp;
      Dec(j);
      end;
    end;
  readln(k); res:=0;
  if k>11 then begin writeln(res); continue; end;
  for i:=1 to k do
    sorted[i]:=team[i];
  counter:=1;
  while counter<=k do
    begin
    temp:=sorted[counter]; found1:=0;
    while sorted[counter]=temp do
      begin Inc(counter); Inc(found1); end;
    found2:=0;
    for i:=1 to 11 do
      if team[i]=temp then Inc(found2);
    fact1:=1;
    fact2:=1;
    if found2<>found1 then
      begin
      for i:=1 to found1 do
        begin
        fact1:=fact1*i;
        fact2:=fact2*found2;
        Dec(found2);
        end;
      Res:=Res+(fact2 div fact1)-1;
      end;
    end;
  Res:=Res+1;
  WRITELN(res);
  end;
end.

This code MUST pass even the array boundaries problem :slight_smile:
Thanks for your help, I really appreciate what you do and I must admit CodeChef is one the best sites I’ve ever used :smiley:

2
84 82 84 81 59 62 69 44 2 6 23
3
75 78 42 29 76 88 32 53 82 13 27
2

have a look at that one. :slight_smile: good luck.

1 Like

please describe precisely the approach you used.

Well, here it goes. I sorted out the array, then I made new array containing only the best k players. And I checked how many times each member appears in original array and how many times in sorted array. If the number is the same there is only one way to put them in team, else I use combination equation. Equation should be familiar. It’s the usual mathematical way of calculation combinations (How many ways there is to choose 2 balls out of 6 --> 65/21 = 15). Then I take 1 out and later on add that one because that is the number of “original” combination. Hope this is precise enough :smiley:

yep, that’s the basic approach. i’ll have a look at your code.
the first thing i can tell you, is that i get a value out of range error, running your code on a sample test file.

$ gpc pera93.pas -o pera93.exe ; echo -e “1\n1 1 1 1 1 1 1 1 1 1 1\n11\n” | ./pera93.exe

./pera93: value out of range (error #300 at 4016dd)

$

what compiler do you use ?

with the compiler i use: if that counter reaches 12 as a value, the created binary raises an error.

with the compiler you use: if that counter reaches 12 as a value, the created binary does not raise an error, but the following instructions will probably lead to a wrong result (be it on this test case or another one).

Can’t believe what’s going on :smiley:
When I put only the second test case it prints out 1, when I put both for first one and second one it prints out the first correctly, but for the second one it makes a mistake printing out 0… Can’t believe it :smiley:

that’s because you forgot to clear a variable you used. same mistake that in your previous topic. :slight_smile: