#1356: TLE ... 效能瓶頸在哪?

Unknown User

d133. 00357 - Let Me Count The Ways -- UVa357 | From: [218.165.192.34] | 發表日期 : 2009-02-09 23:57

這題應該是用DP解   我的演算法是O(n)但還是TLE.....

 #include <stdio.h>

int a[6]={0,1,5,10,25,50};
int n,r,t;

int main()
{
    while ( EOF != scanf("%d",&n) )
    {
        long long int combine[30001]={1,1};
        for ( r = 1 ; r <= 5 ; r++)
        {
            for (t = 2 ; t <= n ; t++  )
            {
                if ( t - a[r] >=0 )
                {

                    combine[t]=combine[t - a[r] ]+combine[t];
                }
            }

        }

        if (combine[n] == 1)
            printf("There is only 1 way to produce %d cents change.\n",n);
        else if (combine[n] > 1)
            printf("There are %lld ways to produce %d cents change.\n",combine[n],n);
    }
    return 0;
}

 
#1403: Re:TLE ... 效能瓶頸在哪?


taichunmin (和風信使)

學校 : 國立彰化高級中學
編號 : 1100
來源 : [36.232.190.238]
最後登入時間 :
2021-03-29 01:45:39
d133. 00357 - Let Me Count The Ways -- UVa357 | From: [220.131.137.168] | 發表日期 : 2009-02-19 07:11

這題應該是用DP解   我的演算法是O(n)但還是TLE.....

 #include

int a[6]={0,1,5,10,25,50};
int n,r,t;

int main()
{
    while ( EOF != scanf("%d",&n) )
    {
        long long int combine[30001]={1,1};
        for ( r = 1 ; r <= 5 ; r++)
        {
            for (t = 2 ; t <= n ; t++  )
            {
                if ( t - a[r] >=0 )
                {

                    combine[t]=combine[t - a[r] ]+combine[t];
                }
            }

        }

        if (combine[n] == 1)
            printf("There is only 1 way to produce %d cents change.\n",n);
        else if (combine[n] > 1)
            printf("There are %lld ways to produce %d cents change.\n",combine[n],n);
    }
    return 0;
}


用DP沒錯,可是你建的表下一次迴圈就不見了,你為什麼要把表洗掉勒?
直接全部做出來不舊好了??

 
ZeroJudge Forum