Online Judge | Problem Set | Authors | Online Contests | User | ||||||
---|---|---|---|---|---|---|---|---|---|---|
Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest |
论坛上的数据都能过,但还是WA,帮忙看看啊非常非常感谢,实在是很崩溃了 #include <cstdio> #include <cstdlib> #include <cstring> #include <functional> #include <algorithm> int piece_num; int pieces[65]; int used[65]; int stick_num; int stick_len; bool search_stick(int cur_id, int start, int cur_len) { int temp; while (start < piece_num && (used[start] || cur_len + pieces[start] > stick_len)) ++start; if (start == piece_num) return false; if (cur_len + pieces[start] < stick_len) { used[start] = 1; if (search_stick(cur_id, start + 1, cur_len + pieces[start])) return true; used[start] = 0; if (cur_len > 0) { temp = start + 1; while (temp < piece_num && (used[temp] || pieces[temp] == pieces[start])) ++temp; return search_stick(cur_id, temp, cur_len); } } else if (cur_len + pieces[start] == stick_len) { if (cur_id == stick_num - 1) return true; used[start] = 1; if (search_stick(cur_id + 1, 0, 0)) return true; used[start] = 0; temp = start + 1; while (temp < piece_num && (used[temp] || pieces[temp] == pieces[start])) ++temp; return search_stick(cur_id, temp, cur_len); } return false; } int main(void) { int it; int total; int min; freopen("input.txt", "r", stdin); while (scanf("%d", &piece_num)) { if (piece_num == 0) break; total = 0; min = 0; for (it = 0; it < piece_num; ++it) { scanf("%d", &pieces[it]); total += pieces[it]; if (pieces[it] > min) min = pieces[it]; } std::sort(pieces, pieces + piece_num, std::greater<int>() ); for (it = min; it <= total; ++it) { if (total % it != 0) continue; stick_num = total / it; stick_len = it; memset(used, 0, sizeof(used)); if (search_stick(0, 0, 0)) break; } printf("%d\n", it); } return 0; } Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator