标签:
HDOJ1518
Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 11375 Accepted Submission(s): 3660
#include<iostream> #include<cstdio> #include<cstring> #include<algorithm> using namespace std; int s[25]; int sign[25]; int sum; int ave; int n; int flag; void dfs(int t,int len ,int k) { int i; if(t==5) { flag=1; return; } if(len==ave) { dfs(t+1,0,0); if(flag) return; } for(i=k; i<n; i++) //从前走到后 if(sign[i]==0&&s[i]+len<=ave) //标记使用了的棍子 { sign[i]=1; dfs(t,s[i]+len,i+1); if(flag) return; sign[i]=0; //回朔,没有使用状态恢复 } else if(sign[i]==0&&s[i]+len>ave) return; } int main() { int i,t; scanf("%d",&t); while(t--) { sum=0; scanf("%d",&n); for(i=0; i<n; i++) { scanf("%d",&s[i]); sum+=s[i]; } if(sum%4!=0)//构边的优化 { cout<<"no"<<endl; continue; } ave=sum/4; for(i=0; i<n; i++) //有比边长大的边就不行 if(s[i]>ave) break; if(i<n) { cout<<"no"<<endl; continue; } memset(sign,0,sizeof(sign)); sort(s,s+n); flag=0; dfs(1,0,0); if(flag) cout<<"yes"<<endl; else cout<<"no"<<endl; } return 0;
}
标签:
原文地址:http://www.cnblogs.com/zrf9527/p/4912878.html