有没有大佬帮我看看哪里错了
查看原帖
有没有大佬帮我看看哪里错了
468275
TOLKELL楼主2024/3/27 12:12
#include <bits/stdc++.h>
using namespace std;
long long int n,m,w[25],a[25],p;
bool dfs(long long int step,long long int sum,long long int lv){
	if(sum==p) sum=0,lv++,step=1;
	if(lv>=3) return 1;
	while(step<=m&&a[step]) step++;
	if(step>m) return 0;
	bool flag=0;
	if(sum+w[step]<=p){
		a[step]=1;
		flag=flag||dfs(step+1,sum+w[step],lv);
		a[step]=0;
	}
	flag=flag||dfs(step+1,sum,lv);
	return flag;
}
void print(bool x){
	if(x) cout<<"yes";
	else cout<<"no";
	cout<<endl; 
}
int main()
{
    cin>>n;
    while(n--){
    	cin>>m;p=0;
    	for(int i=1;i<=m;i++) {cin>>w[i];p+=w[i];}
    	sort(w+1,w+1+m);
    	if(p%4!=0||w[m]>p/4) {print(0);continue;}
    	p/=4;
    	print(dfs(1,0,0));
    }
    return 0;
}  
2024/3/27 12:12
加载中...