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 |
TLE to death.... 已经+了lrj黑书上的所有剪枝却依然超时。。。大牛请教 附带程序#include<iostream> using namespace std; int n; int que[70]; bool done[70]; bool flag; int total, first; bool search(int k, int last, int start) { if ((total == n) && (k == first)) return true; else if (total == n) return false; for (int i = start; i != -1; i --) if ((! done[i]) && (que[i] <= k) && ((k != first) || (que[i] >= last))) { total ++; done[i] = true; if (k - que[i] > 0) if (k == first) if (search(k - que[i], que[i], i - 1)) return true; else ; else if (search(k - que[i], last, i - 1)) return true; if (k - que[i] == 0) if (search(first, last, n - 1)) return true; done[i] = false; total --; if (que[i] == k) return false; } } int main() { while (scanf("%d", &n) != -1) { if (n == 0) break; int sum = 0; for (int i = 0; i != n; i ++) { scanf("%d", &que[i]); sum += que[i]; } sort(que, que + n); for (int i = sum / que[n - 1]; i > 0; i --) if (sum % i == 0) { memset(done, false, sizeof(done)); total = 0; for (int j = 0; j != n; j ++) if (que[j] == sum / i) { total ++; done[j] = true; } first = sum / i; if (search(sum / i, que[n - i], n - i)) { printf("%d\n", sum / i); break; } } } return 0; } Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator