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 |
附代码In Reply To:这里的测试数据都过了,武大acm也能通过,这里始终wa,快疯了,各位有没有其他的数据? Posted by:csucrab at 2008-08-12 10:59:26 #include <iostream> #include <vector> #include <string> #include <algorithm> using namespace std; int num, original, part; int achieved = 0; vector<int> sticks; vector<int> states; inline bool UDgreater ( int elem1, int elem2 ) { return elem1 > elem2; } inline void reset_states() { for(int i = 0; i < num; i++) states[i] = 0; } bool assign(int n, int tmp_sum) { if(part == 1) //(3) 只有一个分段不要计算 return true; while(n < num && (states[n] || tmp_sum + sticks[n] > original)) //(4) 已经使用过的和放不下的不再计算 n++; if(n == num) return false; if(tmp_sum + sticks[n] == original) { // states[n] = 1; achieved++; if(achieved >= part - 1) //(5) 最后一段不需要测试,剩下的就刚好 return true; if(assign(0, 0)) return true; else { //(6) 如果当前木棍刚好填满一段,而后续段不能满足,则不需尝试不要当前木棍的情况 states[n] = 0; achieved--; return false; } } else { //其他情况要尝试使用/不使用两种情况 states[n] = 1; if(assign(n + 1, tmp_sum + sticks[n])) return true; else if(tmp_sum > 0){ //(7) 如果某次尝试的第一根木棍不能满足则无需再试 states[n] = 0; int k = n + 1; while(k < num && sticks[k] == sticks[n]) //(8) 去除重复的,当前木棍不可用,如果后续有相同值同样不可用 k++; if(k < num) { if(assign(k, tmp_sum)) return true; } else return false; } } return false; } int main() { int sum = 0; int part_len; while(cin >> num && num) { for(int i = 0; i < num; i++) { cin >> part_len; sum += part_len; sticks.push_back(part_len); states.push_back(0); } sort(sticks.begin(), sticks.end(), UDgreater); for(original = sticks[0]; original <= sum; original++) { //(1) 每段长度在最大长度和总长度之间 if(sum % original != 0) //(2) 每段长度要能被总长度整除 continue; part = sum / original; bool ret = assign(0, 0); reset_states(); if(ret) { cout << original << endl; achieved = 0; break; } } sum = 0; sticks.clear(); states.clear(); } return 0; } Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator