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

help! I got TLE
Posted by SunMoonStar 20 Sep 2003 09:20
Here is my program:

program p1115;
const maxn=100; maxm=10;
var
  ship:array [1..maxn] of longint;
  long:array [1..maxm] of longint;
  r:array [1..maxm,0..maxn] of longint;
  top:array [1..maxm] of longint;
  m,n:longint;

procedure init;
var i:longint;
begin
  fillchar(ship,sizeof(ship),0);
  fillchar(long,sizeof(long),0);
  fillchar(r,sizeof(r),0);
  fillchar(top,sizeof(top),0);
  readln(n,m);
  for i:=1 to n do
    readln(ship[i]);
  for i:=1 to m do
    readln(long[i]);
end;

procedure sout;
var i,j,k:longint;
begin
  for i:=1 to m do
    begin
      j:=1; while (r[i,j]<>0) do inc(j); dec(j);
      writeln(j); for k:=1 to j do write(r[i,k],' '); writeln;
    end;
end;

procedure sort;
var i,j,t:longint;
begin
  for i:=1 to (n-1) do
    for j:=(i+1) to n do
      if (ship[i]<ship[j]) then
        begin
          t:=ship[i]; ship[i]:=ship[j]; ship[j]:=t;
        end;
end;

procedure solve(dep:longint);
var i:longint;
begin
  for i:=1 to m do
    if (r[i,0]+ship[dep]<=long[i]) then
      begin
        inc(top[i]); r[i,top[i]]:=ship[dep]; inc(r[i,0],ship[dep]);
        if (dep=n) then begin sout; halt; end else solve(dep+1);
        r[i,top[i]]:=0; dec(r[i,0],ship[dep]); dec(top[i]);
      end;
end;

begin
  init;
  sort;
  solve(1);
end.