漂亮小姐姐点击就送: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+二分

相关文章: