POJ--1011 和 UVA--307 Sticks

George took sticks of the same length and cut them randomly until all parts became at most 50 units long. Now he wants to return sticks to the original state, but he forgot how many sticks he had originally and how long they were originally. Please help him and design a program which computes the smallest possible original length of those sticks. All lengths expressed in units are integers greater than zero.

Input

The input contains blocks of 2 lines. The first line contains the number of sticks parts after cutting, there are at most 64 sticks. The second line contains the lengths of those parts separated by the space. The last line of the file contains zero.

Output

The output should contains the smallest possible length of original sticks, one per line.

Sample Input

9
5 2 1 5 2 1 5 2 1
4
1 2 3 4
0

Sample Output

6
5

题解:把人逼疯的小木块.....给你n个小木块,这些是乔治用若干长度相同木条切割的,让求木条的最小长度。。。。。。。。(剪枝让我飞天(;´༎ຶД༎ຶ`) )。

贴poj的代码:

#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
int n,ll,book[100],mp[100];

bool cmp(int p,int q)
{
    return p>q;
}
int dfs(int num,int longg)
{
    if(num==0&&longg==0)
        return 1;
    if(longg==0)
        longg=ll;//一根木棍已经拼完,拼另一根。
    for(int i=0; i<n; i++)
    {
        if(book[i]==0&&mp[i]<=longg)
        {
            if(i>0)
                if(mp[i]==mp[i-1]&&book[i-1]==0)//上一个没用,这一个和上一个有一样,也不用。
                    continue;
            book[i]=1;
            if(dfs(num-1,longg-mp[i]))
                return 1;
            else//说明不能用i作为第一条,那么要拆以前的木棒,i还可能用在以前的木棒中,所以回溯
            {
                book[i]=0;
                if(longg==ll||longg==mp[i])//剪枝
                    return 0;
                //前者(如果前面已有若干组合组成木棒,但是这个不行,这个长度就没必要再搜索(前面的组合不用拆散再搜))
                //后者(如果以此为长度不能组成木棒,不用再用后面的长度搜索)
            }
        }
    }
    return 0;
}
int main()
{
    while(~scanf("%d",&n)&&n)
    {
        int i,j,sum=0;
        for(i=0; i<n; i++)
        {
            scanf("%d",&mp[i]);
            sum+=mp[i];
        }
        sort(mp,mp+n,cmp);
        for(ll=mp[0]; ll<=sum/2; ll++)
        {
            if(sum%ll)
                continue;
            memset(book,0,sizeof(book));
            if(dfs(n,ll))
            {
                printf("%d\n",ll);
                break;
            }
        }
        if(ll>sum/2)
            printf("%d\n",sum);
    }
    return 0;
}
/*<1>首先,拼成木条的长度必须是所有木棒长度总和的约数,并且大于等于木条中的最大值。

  <2>将木棒从打到小排列,可以减少递归的层数。

  <3>当第i个木棒未选取时,如果i+ 1木棒与i木棒相同长度。跳过, 没有必要重复考虑。

  <4>当遍历到i个木棒时,找不到小木棒和它组成木条时, 可以终止当前判断的木条长度。
*/

UVA的代码:

#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
int n,l,t,a[1010],book[1010];
bool cmp(int x,int y)
{
    return x>y;
}
int dfs(int lon,int num,int k)
{
    ///拼木块渐渐形成的长度,已完成要拼成的长度的木条个数,
    if(lon==l)
    {
        lon=0;
        num++;
        k=0;
    }
    if(num==t)
        return 1;
    for(int i=k+1; i<n; i++)
    {
        if(book[i]==0&&lon+a[i]<=l)
        {
            if(i>0&&a[i]==a[i-1]&&book[i-1]==0)
                continue;
            book[i]=1;
            if(dfs(lon+a[i],num,i))
                return 1;
            book[i]=0;
            if(lon==0)///(如果前面已有若干组合组成木棒,但是这个不行,这个长度就没必要再搜索(前面的组合不用拆散再搜))
                return 0;
        }
    }
    return 0;
}
int main()
{
    while(~scanf("%d",&n)&&n)
    {
        int i,j,sum=0;
        memset(book,0,sizeof(book));
        for(i=0; i<n; i++)
        {
            scanf("%d",&a[i]);
            sum+=a[i];
        }
        sort(a,a+n,cmp);
        for(l=a[0]; l<=sum/2; l++)
        {
            if(sum%l)
                continue;
            else
            {
                t=sum/l;
                book[0]=1;///忘了这个就超时了唉。。
                if(dfs(a[0],0,0))
                {
                    printf("%d\n",l);
                    break;
                }
            }
        }
        if(l>sum/2)
            printf("%d\n",sum);
    }
    return 0;
}
///21 14 13 11 9 6 4 3 2 1 l=21时,拼14的时候,先找到6,
///这时k=5,下一次直接从6后面的数开始找节省时间。

猜你喜欢

转载自blog.csdn.net/gjlfly/article/details/81128003