Here's my Code.
Although I think my program is pretty good and it do get the right answer but it got the TLE....
I want to know why it have a TLE and what should I do?
Thank you.
[pascal]
{UVA 103.pas}
{Stacking Boxes}
type
TypeOfBox=record
d:array[1..11] of integer;
next:integer;
f:integer;
end;
var
box :array[1..31] of TypeOfBox;
ans :array[1..31] of integer;
i,j,n,k,p :integer;
max,s :longint;
function check(inner,outer:integer):boolean; {To Check if Box[inner] can be put into Box[outer]}
var p:boolean;
i:integer;
begin
p:=true;
for i:=1 to n do if box[inner].d>box[outer].d then p:=false;
check:=p;
end;
Procedure sort(id:integer); {To Sort D of each box}
var
i,j,t,min :integer;
begin
for i:=1 to n-1 do begin
min:=i;
for j:=i+1 to n do if box[id].d[min]>box[id].d[j] then min:=j;
t:=box[id].d;
box[id].d:=box[id].d[min];
box[id].d[min]:=t;
end;
end;
begin
while not eof do begin
fillchar(box,sizeof(box),0);
readln(k,n);
for i:=1 to k do begin
for j:=1 to n do
read(box.d[j]);
readln;
sort(i);
end;
for i:=1 to k do begin
max:=0;p:=0;
for j:=1 to k do if j<>i then begin
if check(j,i) and (max<=box[j].f) then begin
p:=j;
max:=box[j].f;
end;
end;
if p<>0 then begin
box.f:=max+1;
box.next:=p;
end;
end;
max:=0;
for i:=1 to k do if box.f>max then begin max:=box.f;p:=i;end;
writeln(max+1);
s:=1;
ans[s]:=p;
while box[p].f<>0 do begin
inc(s);
ans[s]:=box[p].next;
p:=box[p].next;
end;
for i:=s downto 2 do write(ans[i],' ');
writeln(ans[1]);
end;
end.
[/pascal]
I changed the Sort Procedure to QSort But Still TLE.............
[pascal]{UVA 103.pas}
{Stacking Boxes}
type
TypeOfBox=record
d:array[1..11] of integer;
next:integer;
f:integer;
end;
var
box :array[1..31] of TypeOfBox;
ans :array[1..31] of integer;
i,j,n,k,p :integer;
max,s :longint;
function check(inner,outer:integer):boolean;
var p:boolean;
i:integer;
begin
p:=true;
for i:=1 to n do if box[inner].d>box[outer].d then p:=false;
check:=p;
end;
procedure QSort(id: Integer);
procedure Sort(l, r: Integer);
var
i, j ,x ,y : integer;
begin
i := l; j := r; x := box[id].d[(l+r) DIV 2];
repeat
while box[id].d < x do i := i + 1;
while x < box[id].d[j] do j := j - 1;
if i <= j then
begin
y := box[id].d; box[id].d := box[id].d[j]; box[id].d[j] := y;
i := i + 1; j := j - 1;
end;
until i > j;
if l < j then Sort(l, j);
if i < r then Sort(i, r);
end;
begin {QuickSort};
Sort(1,n);
end;
begin
while not eof do begin
fillchar(box,sizeof(box),0);
readln(k,n);
for i:=1 to k do begin
for j:=1 to n do
read(box.d[j]);
readln;
Qsort(i);
end;
for i:=1 to k do begin
max:=0;p:=0;
for j:=1 to k do if j<>i then begin
if check(j,i) and (max<=box[j].f) then begin
p:=j;
max:=box[j].f;
end;
end;
if p<>0 then begin
box.f:=max+1;
box.next:=p;
end;
end;
max:=0;
for i:=1 to k do if box.f>max then begin max:=box.f;p:=i;end;
writeln(max+1);
s:=1;
ans[s]:=p;
while box[p].f<>0 do begin
inc(s);
ans[s]:=box[p].next;
p:=box[p].next;
end;
for i:=s downto 2 do write(ans[i],' ');
writeln(ans[1]);
end;
end.
this is my code using an approch of recurion and exhauction on this problem
but it got a TLE is test case 5
however,i have tried the case of 30 10 in my own computer which can run within 0.1 sec.
please help me...
[cpp]#include <iostream>
using namespace std;
/*
if there exist no box
update list
else
for remining box
get the smallest id one from supply list
delete geted id
if it can be insert
insert it in another list
recur;
delete the id box from list
else
update list;
*/
int r(int K,int N,bool E[],int T,int PreviousList[],int& Max,int* pList,int A[][10]);
int r(int K,int N,bool E[],int T,int PreviousList[],int& Max,int* pList,int A[][10])
{
bool Exist[30];
int Total=T;
int List[30];
bool tempB=true;
bool Inserted;
bool tempB1,tempB2;
int count=0;
for (int i=0;i<=K-1;i++)
{
Exist=E;
List=PreviousList;
if (E)
{
tempB=false;
count++;
}
}
if (tempB)
{
if (Total>Max)
{
Max=Total;
for (int i=0;i<=Total-1;i++)
*(pList+i)=List;
}
}
else if (Max<K && Total+count>Max)
{
for (int i=0;i<=K-1;i++)
if (Exist)
{
Inserted=false;
for (int j=0;j<=Total;j++)
{
tempB1=true;
tempB2=true;
for (int k=0;k<=N-1;k++)
{
if (j<Total)
if (A[k]>=A[List[j]][k])
tempB1=false;
if (j>0)
if (A[k]<=A[List[j-1]][k])
tempB2=false;
}
if (tempB1 && tempB2)
{
for (int k=Total-1;k>=j;k--)
List[k+1]=List[k];
List[j]=i;
Total++;
Inserted=true;
Exist=false;
r(K,N,Exist,Total,List,Max,pList,A);
for (int k=0;k<=Total-1;k++)
if (List[k]==i)
{
for (int l=k;l<=Total-1;l++)
List[l]=List[l+1];
k=Total-1;
}
Total--;
j=Total;
}
}
if (!Inserted)
{
if (Total>Max)
{
Max=Total;
for (int j=0;j<=Total-1;j++)
*(pList+j)=List[j];
}
}
}
}
return 0;
}
int main()
{
int K,N,tempI;
int A[30][10];
int Max;
int List[30];
bool Exist[30];
while (cin >> K >> N)
{
Max=0;
for (int i=0;i<=K-1;i++)
{
Exist[i]=true;
List[i]=0;
for (int j=0;j<=N-1;j++)
cin >> A[i][j];
}
for (int i=0;i<=K-1;i++)
for (int j=0;j<=N-2;j++)
for (int k=j+1;k<=N-1;k++)
if (A[i][j]>A[i][k])
{
tempI=A[i][j];
A[i][j]=A[i][k];
A[i][k]=tempI;
}
[cpp]
for (int i=0;i<=K-1;i++)
for (int j=0;j<=N-2;j++)
for (int k=j+1;k<=N-1;k++)
if (A[j]>A[k])
{
tempI=A[j];
A[j]=A[k];
A[k]=tempI;
}
[/cpp]
this is slow.. around O(n^2) bubble * n times = O(n^3), you might want to consider using a faster sorting alrgorithm (qsort in C, or sort in C++ STL) after you are done with sorting. you can apply modified LIS (Longest Increasing Subsequence), instead of comparing a number you will be comparing a whole chunk of numbers ( one row of matrix with another ).
I think you are correct.
On the other hand,I know why I can run my program in my own computer but not the judge
This is because the judge is only Pentium III - 800 MHz ...while my computer is AMD 2400+
Thank you for your idea anyway.
Got an AC after using your test cases and changed some of my code. I guess the problem with wipper and I is that we forgot to consider the case where the longest nesting string length is 1.