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 <iostream> #include <vector> using namespace std; class Stick { public: vector<int> m_Length; vector<int> m_Num; vector<int> m_Temp; vector<int> m_Stack; int m_Total; int m_Result; public: Stick() { m_Length.reserve(50); m_Num.reserve(50); m_Temp.reserve(50); m_Stack.reserve(64); } bool HaveTry() { int left,bottom; for(int i=0;i<m_Temp.size();) { if(0==m_Temp[i]) { i++; continue; } bottom=m_Stack.size(); m_Stack.push_back(i); left=m_Result-m_Length[i]; m_Temp[i]--; while(left>0) { for(int j=m_Stack[m_Stack.size()-1];j<m_Temp.size();) { if(0==m_Temp[j] || left<m_Length[j]) { j++; continue; } else { m_Stack.push_back(j); left-=m_Length[j]; m_Temp[j]--; } } if(left!=0) { //find next one to replace last int last; while(1) { if(bottom+1==m_Stack.size()) { if(0==bottom) return false; m_Temp[m_Stack[m_Stack.size()-1]]++; m_Stack.pop_back(); int degree=0; for(int sum=0;sum<m_Result;degree++) { sum+=m_Length[m_Stack[m_Stack.size()-1-degree]]; } bottom-=degree; left=0; } last=m_Stack[m_Stack.size()-1]+1; for(;last<m_Temp.size();last++) { if(0!=m_Temp[last]) break; } if(last==m_Temp.size()) { m_Temp[m_Stack[m_Stack.size()-1]]++; left+=m_Length[m_Stack[m_Stack.size()-1]]; m_Stack.pop_back(); } else break; } m_Temp[m_Stack[m_Stack.size()-1]]++; m_Temp[last]--; left+=m_Length[m_Stack[m_Stack.size()-1]]; left-=m_Length[last]; m_Stack[m_Stack.size()-1]=last; } } } return true; } int Process() { if(m_Length.empty()) return 0; for(m_Result=m_Length[0];m_Result<m_Total;m_Result++) { if(m_Total%m_Result!=0) continue; m_Temp=m_Num; m_Stack.clear(); if(HaveTry()) return m_Result; } return m_Result; } }; int main() { Stick st; vector<int> temp(51,0); int n,num; while(1) { cin>>num; if(num<=0) break; st.m_Stack.clear(); st.m_Total=0; st.m_Length.clear(); st.m_Num.clear(); temp.assign(51,0); for(int i=0;i<num;i++) { cin>>n; if(n>50|| n<=0) continue; st.m_Total+=n; temp[n]++; } for(int j=50;j>0;j--) { if(0!=temp[j]) { st.m_Length.push_back(j); st.m_Num.push_back(temp[j]); } } cout<<st.Process()<<endl; } return 0; } Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator