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

HAWK,我这题没用堆栈,只用了一些变量,但我怎么算内存也没超过1000K,怎么回事啊……

Posted by rruucc at 2003-06-09 21:59:18 on Problem 1090
不用看程序,就看变量声明就够了……

#include<stdio.h> 
#define MaxL 100 
#define Max 1000 
#define MaxNum(a,b) (a>b?a:b) 

struct Number 
{ 
  int l; 
  char d[MaxL]; 
}; 

struct Number f[Max],g[Max],min[Max],put[Max]; 
int n,d[Max]; 

void copy(Number &a,Number b) 
{ int i; 
 a.l=b.l; 
 for (i=0; i<a.l; i++) a.d[i]=b.d[i]; 
} 

void add(Number &a,Number b) 
{ int i; 
  short jin=0; 
 for (i=0; i<MaxNum(a.l,b.l); i++) 
  {if (i<a.l&&i<b.l) a.d[i]=a.d[i]+b.d[i]+jin; 
   else if (i>=a.l) a.d[i]=b.d[i]+jin; 
   else if (i>=b.l) a.d[i]=a.d[i]+jin; 
   jin=a.d[i]/10; a.d[i]%=10; 
  } 
 a.l=MaxNum(a.l,b.l); 
 while (jin>0) 
  {a.d[a.l]=jin; jin=a.d[a.l]/10; a.d[a.l]%=10; a.l++;} 
} 

void s_mul(Number a,short x,Number &c) 
{ int i; 
  short jin=0; 
 c.l=a.l; 
 for (i=0; i<a.l; i++) 
  {c.d[i]=a.d[i]*x+jin; 
   jin=c.d[i]/10; c.d[i]%=10; 
  } 
 while (jin>0) 
  {c.d[c.l]=jin; jin=c.d[c.l]/10; c.d[c.l]%=10; c.l++;} 
} 

void adjust(Number &a) 
{ int i; 
  short jin; 
 jin=-1; i=0; 
 do 
  {a.d[i]+=jin; 
   if (a.d[i]<0) {a.d[i]+=10; jin=-1;} else jin=0; 
   i++; 
  } 
 while (jin<0); 
 if (a.d[a.l-1]==0) a.l--; 
} 

void print(Number a) 
{ int i; 
 for (i=a.l-1; i>=0; i--) printf("%d",a.d[i]); 
 printf("\n"); 
} 

void init() 
{ int i; 
  Number one; 
 scanf("%d",&n); 
 for (i=0; i<n; i++) scanf("%d",&d[i]); 
 one.l=1; one.d[0]=1; 
 f[0].l=1; f[0].d[0]=1; 
 for (i=1; i<n; i++) 
  {s_mul(f[i-1],2,f[i]); 
   add(f[i],one); 
  } 
 g[0].l=1; g[0].d[0]=1; 
 for (i=1; i<n; i++) 
  {g[i].l=0; 
   add(g[i],g[i-1]); add(g[i],f[i-1]); add(g[i],one); 
  } 
} 

int bigger(Number a,Number b) 
{ int i; 
 if (a.l>b.l) return 1; 
 if (a.l<b.l) return 0; 
 for (i=a.l-1; i>=0; i--) 
  if (a.d[i]>b.d[i]) return 1; 
 return 0; 
} 

void search() 
{ int i; 
  Number one,tmp; 
 one.l=1; one.d[0]=1; 
 min[0].l=1; min[0].d[0]=d[0]; 
 put[0].l=1; if (d[0]) put[0].d[0]=0; else put[0].d[0]=1; 
 for (i=1; i<n; i++) 
  if (d[i]) 
   {copy(put[i],min[i-1]); 
    if (i==1) 
     {if (d[i-1]) 
       {min[i].l=1; min[i].d[0]=2;} 
      else 
       {copy(min[i],min[i-1]); add(min[i],g[i]);} 
     } 
    if (i>1) 
     {if (d[i-1]) 
       {copy(min[i],min[i-1]); add(min[i],g[i]); 
    copy(tmp,min[i-2]); add(tmp,one); add(tmp,g[i-1]); 
    if (bigger(min[i],tmp)) copy(min[i],tmp); 
       } 
      else 
       {copy(min[i],min[i-1]); add(min[i],g[i]); 
    copy(tmp,put[i-1]); add(tmp,one); add(tmp,g[i-1]); 
    if (bigger(min[i],tmp)) copy(min[i],tmp); 
       } 
     } 
   } 
  else 
   {copy(put[i],put[i-1]); add(put[i],one); add(put[i],g[i-1]); 
    copy(min[i],min[i-1]); 
   } 
} 

int main() 
{init(); 
 search(); 
 print(min[n-1]); 
 return 0; 
} 

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