Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

code

Posted by My_loves at 2005-11-12 20:09:34 on Problem 1011
In Reply To:TLE 到死。。 Posted by:My_loves at 2005-11-12 20:08:31
program p1011;
 var num:array[1..64]of byte;
     used:array[1..64]of boolean;
     total:integer;
     n,max,temp:integer;
     ok:boolean;

 procedure init;
  var i:integer;
  begin
   max:=0;
   total:=0;
   readln(n);
   if n=0 then halt;
   for i:=1 to n do
    begin
     read(num[i]);
     if num[i]>max then max:=num[i];
     total:=total+num[i];
    end;
  end;

 procedure qsort(l,r:integer);
  var i,j,mid,k:integer;
  begin
   i:=l; j:=r; mid:=num[(i+j) div 2];
   repeat
    while num[i]>mid do inc(i);
    while num[j]<mid do dec(j);
    if i<=j then begin
                  k:=num[i]; num[i]:=num[j]; num[j]:=k;
                  inc(i); dec(j);
                 end;
   until i>j;
   if l<j then qsort(l,j);
   if i<r then qsort(i,r);
  end;

 procedure main(sum,now,k:integer);
  var i:integer;
  begin
   if ok then exit;
   if sum=total div temp then
    begin
     writeln(temp);
     ok:=true;
     exit;
    end;

   for i:=n downto 1 do
    if not used[i] then
     begin
      if now+num[i]>temp then break;
      if ok then exit;
      if now+num[i]<temp then begin
                               used[i]:=true;
                               main(sum,now+num[i],i+1);
                               if ok then exit;
                               used[i]:=false;
                              end;
      if now+num[i]=temp then begin
                               used[i]:=true;
                               main(sum+1,0,2);
                               if ok then exit;
                               used[i]:=false;
                              end;
     end;
   if ok then exit;
  end;

 begin
  repeat
   ok:=false;
   init;
   qsort(1,n);
   for temp:=max to total do
    begin
     fillchar(used,sizeof(used),false);
     if total mod temp=0 then main(1,num[1],2);
     if ok then break;
    end;
  until false;
 end.

Followed by:

Post your reply here:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator