87PTS 求大佬优化 呜呜呜!最后一个点!277MS
  • 板块P1120 小木棍
  • 楼主ZZK423
  • 当前回复5
  • 已保存回复5
  • 发布时间2023/6/8 17:22
  • 上次更新2023/10/23 13:39:19
查看原帖
87PTS 求大佬优化 呜呜呜!最后一个点!277MS
836262
ZZK423楼主2023/6/8 17:22
#include<bits/stdc++.h>
using namespace std;


int n,sum,lenth;
int len[101];
bool vis[101];
bool dfs(int now,int part,int start){
	
    if(now*lenth == sum)return true;
    
    if(part == lenth) return dfs(now+1,0,0);
    
    for(int i=start;i<=n;i++){

        if(vis[i])continue;

        if(len[i]+part > lenth)continue;
        vis[i] = true; 
        if(dfs(now,len[i]+part,i+1)){
            return true;
        } 
        vis[i] = false;

        if(!part|| len[i]+part==lenth) {
        	return false;
        }

        int j = i;
        while(j<=n&&len[j]==len[i]){
            j++;
        }
        i=j-1;
    }
    
    return false;
}
int main()
{   
   	cin>>n;
    for(int i=1;i<=n;i++){
        cin>>len[i];
        sum+=len[i];
    }
    //Sort
    sort(len+1,len+n+1);
    reverse(len+1,len+n+1);

    for(lenth=1;lenth<=sum;lenth++){
        if(sum%lenth==0 && dfs(0,0,0)){
            cout<<lenth<<endl;
            break;
        }
    }
    return 0;
}
2023/6/8 17:22
加载中...