1 条题解

  • 4
    @ 2021-8-7 21:05:12

    C++ :

    #include <iostream>
    #include <cstring>
    #include <algorithm>
    using namespace std;
    bool v[65];
    int a[65];
    int n;
    bool cmp(int x,int y)
    {
    	return x>y;
    }
    bool dfs(int cab,int cur, int last,int cnt,int len)
    {
    	if(cur > cnt)
    		return true;
    	if(cab == len)
    		return dfs(0,cur+1,1,cnt,len);
    	int fail = 0;
    	for(int i=last;i<=n;i++)
    	{
    		if(fail!=a[i]&&a[i]+cab<=len&&!v[i])
    		{
    			v[i] = 1;
    			if(dfs(cab+a[i],cur,i+1,cnt,len))
    				return true;
    			fail = a[i];
    			v[i] = 0;
    			if(cab == 0 || cab + a[i] == len)
    				return false;
    		}
    	}
    	return false;
    }
    int main()
    {
    	while(cin>>n&&n)
    	{
    		int maxx = -1;
    		int pos = 1;
    		int sum = 0;
    		for(int i=1;i<=n;i++)
    		{
    			int t;
    			cin>>t;
    			if(t<=50)
    			{
    				sum+=t;
    				a[pos] = t;
    				pos++;
    				maxx = max(maxx,t);
    			}
    
    		}
    		n = pos-1;
    		sort(a+1,a+1+n,cmp);
    		int i;
    		for(i=maxx;i<=sum;i++)
    		{
    
    			if(sum%i)
    				continue;
    			memset(v,0,sizeof(v));
    
    			if(dfs(0,1,1,sum/i,i))
    				break ;
    		}
    		cout<<i<<endl;
    
    	}
    
    	return 0;
    }
    
    • 1

    信息

    ID
    78
    时间
    1000ms
    内存
    128MiB
    难度
    5
    标签
    递交数
    135
    已通过
    51
    上传者