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 1115. Ships

Why Crash(access violation) or can you give me some tests?
Posted by Koala 29 Apr 2003 19:12
I use Greedy+DynamicProgramming. Maybe it's not right, but i want to
know why i have got crash!
-----------------------My Code-------------------------
program Ships;

const
  maxm=10;
  maxn=100;
  maxrange=10000;

var
  show:array [1..maxm,0..maxn] of longint;
  c,f:array [0..maxrange] of longint;
  a:array [1..maxn] of longint;
  visit:array [1..maxn] of boolean;
  b,code,pos:array [1..maxm] of longint;
  n,m,i,j,max,mi,temp,k:longint;

  procedure work(p,t:longint);
  var
    i,max,j,k:longint;
  begin
    for i:=1 to t do c[i]:=maxlongint; c[0]:=0;
    fillchar(f,sizeof(f),0);
    max:=0;
    for i:=1 to n do
      if not visit[i] then
        for j:=max downto 0 do
          if c[j]<maxlongint then
          begin
            k:=j+a[i];
            if (k<=t) and (c[k]>c[j]+1) then
            begin
              c[k]:=c[j]+1;
              f[k]:=i;
              if k>max then max:=k;
            end;
          end;
    show[p,0]:=0;
    j:=t;
    while j>0 do
    begin
      inc(show[p,0]);
      show[p,show[p,0]]:=a[f[j]];
      visit[f[j]]:=true;
      j:=j-a[f[j]];
    end;
  end;

begin
  read(n,m);
  for i:=1 to n do read(a[i]);
  for i:=1 to n-1 do
  begin
    max:=a[i]; mi:=i;
    for j:=i+1 to n do
      if a[j]>max then
      begin
        max:=a[j];
        mi:=j;
      end;
    temp:=a[i]; a[i]:=a[mi]; a[mi]:=temp;
  end;

  for i:=1 to m do
  begin
    read(b[i]);
    code[i]:=i;
  end;
  for i:=1 to m-1 do
  begin
    max:=b[i]; mi:=i;
    for j:=i+1 to m do
      if b[j]>max then
      begin
        max:=b[j];
        mi:=j;
      end;
    temp:=b[i]; b[i]:=b[mi]; b[mi]:=temp;
    temp:=code[i]; code[i]:=code[mi]; code[mi]:=temp;
  end;
  for i:=1 to m do pos[code[i]]:=i;

  fillchar(visit,sizeof(visit),0);
  fillchar(show,sizeof(show),0);
  for i:=1 to m do work(i,b[i]);

  for j:=1 to m do
  begin
    i:=pos[j];
    writeln(show[i,0]);
    write(show[i,1]);
    for k:=2 to show[i,0] do write(' ',show[i,k]);
    writeln;
  end;
end.