WHY TLE
Posted by
PSV 26 Oct 2006 03:41
.const
. max = 1010;
. oo = 3000;
.var
. n, k, i, j, maxn, maxk, top : word;
. a : array [0..max, 0..max] of word;
. sk, sn : array [1..max] of word;
.procedure f(n, k : word);
.var
. i : word;
.begin
. if a[n, k] <> 0 then exit;
. a[n, k] := oo;
. for i := 1 to n do begin
. if a[i - 1, k - 1] = 0 then f(i - 1, k - 1);
. if a[n - i, k] = 0 then f(n - i, k);
. if a[i - 1, k - 1] > a[n - i, k] then begin
. if a[n, k] > a[i - 1, k - 1] then a[n, k] := a[i - 1, k - 1];
. end else begin
. if a[n, k] > a[n - i, k] then a[n, k] := a[n - i, k];
. end;
. end;
. inc(a[n, k]);
.end;
.begin
. read(k);
. if k <> 0 then readln(n);
. top := 0;
. while k <> 0 do begin
. inc(top);
. sk[top] := k;
. sn[top] := n;
. if k > maxk then maxk := k;
. if n > maxn then maxn := n;
. read(k);
. if k <> 0 then readln(n);
. end;
. for i := 1 to maxn do a[i, 1] := i;
. for i := 1 to maxk do a[1, i] := 1;
. for i := 1 to top do begin
. f(sn[i], sk[i]);
. writeln(a[sn[i], sk[i]]);
. end;
.end.
I use recursive DP. And I think it's right. Why fuckin TLE?
I also tried not recursive... Both have TLE
Edited by author 26.10.2006 03:43