Untitled
unknown
c_cpp
17 days ago
1.3 kB
4
Indexable
Never
#include<cstdio> #include<algorithm> using namespace std; int sticks[20]; bool used[20]; int side_length; bool rec(int cur, int now, int total, int n) { if(total == side_length) { if(now++ == 3) return true; cur=0; total=0; } for(int i=cur; i<n; i++) { if(!used[i]) { if(total+sticks[i] <= side_length) { used[i] = true; if(rec(cur+1, now, total+sticks[i], n)) return true; used[i] = false; if (total + sticks[i] == side_length) return false; } } } return false; } int main() { int t; scanf("%d", &t); while(t--) { int sum = 0; int n; scanf("%d", &n); for(int i=0; i<n; i++) { scanf("%d", &sticks[i]); sum += sticks[i]; } sort(sticks, sticks + n, [](const int& s1, const int& s2)->bool{return s1 > s2; }); //reverse(sticks.begin(), sticks.end()); side_length = sum/4; fill(used, used + n, false); if(sum%4!=0 || sticks[0] >side_length) puts("no"); else puts(rec( 0, 0, 0, n) ? "yes" : "no"); } return 0; }
Leave a Comment