POJ#1011 Sticks

漂亮小姐姐点击就送:http://poj.org/problem?id=1011
                               Sticks
Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 151718 Accepted: 36127

Description

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个数随意组合成相等的数,组合起来的数最小是几
//emmmmm   就是,现在有一堆木棒,他们是被切割之后的,他们有长度,问他们被切割之前的可能的最小长度是多少 

//思路:求出最大值、所有数的和,然后从小到大枚举
//把他们扔进vector里,在里边二分 
//不断删数,知道vec为空 

//lower_bound查找方向要和vector的方向一样。。 
//lower_bound。。。。。。。害怕.jpg 

//我去竟然WA掉。。我以为会TLE掉的。。。。。
//气死。。写俩小时竟然不对。。。 

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<vector> 
using namespace std;

const int N=70;

int T,n,sum;
vector<int> vec,v;

inline int read()
{
    char c=getchar();int num=0;
    for(;!isdigit(c);c=getchar());
    for(;isdigit(c);c=getchar())
        num=num*10+c-'0';
    return num;
}

int main()
{
    while(n=read())
    {
        if(!n)
            break;
        sum=0;
        vec.clear();
        for(int i=1,a;i<=n;++i)
        {
            a=read();
            sum+=a;
            vec.push_back(a);
        }
        int now;
        sort(vec.begin(),vec.end(),greater<int>() );
        for(int ans=vec[0];ans<=sum;++ans)
        {
            v.clear();
            for(int i=0;i<n;++i)
            {
                if(vec[i]!=ans)
                    v.push_back(vec[i]);
            }
            now=0;
            bool flag=1;
            while(v.size())
            {
//                cout<<"Sz: "<<v.size()<<endl;
                vector<int>::iterator pos=lower_bound(v.begin(),v.end(),ans-now,greater<int>() );
                if(pos==v.end())
                {
                    break;
                }
                else if((*pos)+now==ans)
                {
                    v.erase(pos);
                    if(v.size())
                    {
                        now=v[0];
                        v.erase(v.begin());
                    }
                    else
                        now=-1;
                }
                else
                {
                    now+=(*pos);
                    v.erase(pos);
                }
            }
            if(now==-1)
            {
                printf("%d\n",ans);
                break;
            }
        }
    }
    return 0;
}
写了俩小时WA掉的vector+二分
//好吧来搞搜索
//将每根木棒按照长度降序排列,记录他们的总长度sum 
//然后从最长的那根木棒的长度枚举到他们的总长度sum 
//记枚举的长度为len,那么如果sum%len==0的时候才去dfs
//因为我们必须把木棒全部拼成长度为len的木棒,所以显然必须整除
//然后在dfs里瞎搞就行了 

#include<iostream>
#include<cmath>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;

const int N=66;

int n,sum,len,a[N];
bool flag,vis[N];
int step=0;

bool cmp(int a,int b)
{
    return a>b;
}

bool dfs(int x,int nowlen,int alllen)
{
    if(alllen==sum)//已经用完所有木棒,正解
        return true;
    if(nowlen==len)//已经拼好一根木棒,继续拼下一根,并从剩下木棒中最长的木棒开始
        nowlen=0,x=1;
    for(int i=x;i<=n;++i)
    {
        if(vis[i]||nowlen+a[i]>len)//木棒已经用过,木棒无法完成组合
            continue;
        vis[i]=true;
        if(dfs(i,nowlen+a[i],alllen+a[i]))//若此次搜索到正确结过则跳出
            return true;
        vis[i]=false;
        if(nowlen+a[i]==len||nowlen==0)//使用第i根木棒的情况下得不到正解,即第i根木棒无法组合成目标长度,跳出
            return false;
        while(a[i+1]==a[i])
            ++i;//第i根木棒无法使用,则与第i根木棒长度相等的均无法使用
    }
    return false;
}

int main()
{
    while(scanf("%d",&n)&&n)
    {
        sum=0;
        for(int i=1;i<=n;++i)
        {
            scanf("%d",&a[i]);
            sum+=a[i];
        }
        sort(a+1,a+n+1,cmp);
        for(len=a[1];len<=sum;++len)
        {
            if(sum%len==0)
            {
                memset(vis,false,sizeof(vis));
                if(dfs(1,0,0))
                {
                    printf("%d\n",len);
                    break;
                }
            }
        }
    }
    return 0;
}

 

转载于:https://www.cnblogs.com/lovewhy/p/8745851.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值