4fd800b3a8 2011-02-23 kinaba: #include <iostream> 4fd800b3a8 2011-02-23 kinaba: #include <sstream> 4fd800b3a8 2011-02-23 kinaba: #include <iomanip> 4fd800b3a8 2011-02-23 kinaba: #include <vector> 4fd800b3a8 2011-02-23 kinaba: #include <string> 4fd800b3a8 2011-02-23 kinaba: #include <map> 4fd800b3a8 2011-02-23 kinaba: #include <set> 4fd800b3a8 2011-02-23 kinaba: #include <algorithm> 4fd800b3a8 2011-02-23 kinaba: #include <numeric> 4fd800b3a8 2011-02-23 kinaba: #include <iterator> 4fd800b3a8 2011-02-23 kinaba: #include <complex> 4fd800b3a8 2011-02-23 kinaba: #include <queue> 4fd800b3a8 2011-02-23 kinaba: #include <stack> 4fd800b3a8 2011-02-23 kinaba: #include <cmath> 4fd800b3a8 2011-02-23 kinaba: #include <cassert> 4fd800b3a8 2011-02-23 kinaba: #include <cstring> 4fd800b3a8 2011-02-23 kinaba: using namespace std; 4fd800b3a8 2011-02-23 kinaba: typedef long long LL; 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: static const int N = 60; 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: typedef vector<int> Num; 4fd800b3a8 2011-02-23 kinaba: Num conv( const string& s ) 4fd800b3a8 2011-02-23 kinaba: { 4fd800b3a8 2011-02-23 kinaba: Num n(N); 4fd800b3a8 2011-02-23 kinaba: for(int i=0; i<s.size(); ++i) 4fd800b3a8 2011-02-23 kinaba: { 4fd800b3a8 2011-02-23 kinaba: char c = s[s.size()-1-i]; 4fd800b3a8 2011-02-23 kinaba: n[N-1-i] = ('0'<=c&&c<='9' ? c-'0' : c-'A'+10); 4fd800b3a8 2011-02-23 kinaba: } 4fd800b3a8 2011-02-23 kinaba: return n; 4fd800b3a8 2011-02-23 kinaba: } 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: Num add( const Num& a, const Num& b ) 4fd800b3a8 2011-02-23 kinaba: { 4fd800b3a8 2011-02-23 kinaba: Num n(N); 4fd800b3a8 2011-02-23 kinaba: int c = 0; 4fd800b3a8 2011-02-23 kinaba: for(int i=0; i<N; ++i) 4fd800b3a8 2011-02-23 kinaba: { 4fd800b3a8 2011-02-23 kinaba: int s = a[N-1-i] + b[N-1-i] + c; 4fd800b3a8 2011-02-23 kinaba: n[N-1-i] = s%36; 4fd800b3a8 2011-02-23 kinaba: c = s/36; 4fd800b3a8 2011-02-23 kinaba: } 4fd800b3a8 2011-02-23 kinaba: return n; 4fd800b3a8 2011-02-23 kinaba: } 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: class HexatridecimalSum { 4fd800b3a8 2011-02-23 kinaba: public: 4fd800b3a8 2011-02-23 kinaba: string maximizeSum(vector <string> numbers, int k) 4fd800b3a8 2011-02-23 kinaba: { 4fd800b3a8 2011-02-23 kinaba: vector<Num> ns; 4fd800b3a8 2011-02-23 kinaba: transform( numbers.begin(), numbers.end(), back_inserter(ns), &conv ); 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: vector<Num> gain(36); 4fd800b3a8 2011-02-23 kinaba: for(int d=0; d<36; ++d) 4fd800b3a8 2011-02-23 kinaba: { 4fd800b3a8 2011-02-23 kinaba: vector<int> g(N); 4fd800b3a8 2011-02-23 kinaba: for(int i=0; i<ns.size(); ++i) 4fd800b3a8 2011-02-23 kinaba: for(int j=0,f=0; j<N; ++j) { 4fd800b3a8 2011-02-23 kinaba: if( ns[i][j] ) f=1; 4fd800b3a8 2011-02-23 kinaba: if( f && ns[i][j] == d ) 4fd800b3a8 2011-02-23 kinaba: g[j] += (35-d); 4fd800b3a8 2011-02-23 kinaba: } 4fd800b3a8 2011-02-23 kinaba: Num gg(N); 4fd800b3a8 2011-02-23 kinaba: for(int j=0; j<N; ++j) { 4fd800b3a8 2011-02-23 kinaba: int x = g[N-1-j]; 4fd800b3a8 2011-02-23 kinaba: gg[N-1-j] = x % 36; 4fd800b3a8 2011-02-23 kinaba: if(x/36) g[N-1-j-1] += x/36; 4fd800b3a8 2011-02-23 kinaba: } 4fd800b3a8 2011-02-23 kinaba: gain[d] = gg; 4fd800b3a8 2011-02-23 kinaba: } 4fd800b3a8 2011-02-23 kinaba: sort( gain.rbegin(), gain.rend() ); 4fd800b3a8 2011-02-23 kinaba: Num zero(N); 4fd800b3a8 2011-02-23 kinaba: Num n = accumulate( gain.begin(), gain.begin()+k, zero, &add ); 4fd800b3a8 2011-02-23 kinaba: Num m = accumulate( ns.begin(), ns.end(), n, &add ); 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: string ans; 4fd800b3a8 2011-02-23 kinaba: for(int i=0,f=0; i<N; ++i) 4fd800b3a8 2011-02-23 kinaba: { 4fd800b3a8 2011-02-23 kinaba: if(m[i]) f=1; 4fd800b3a8 2011-02-23 kinaba: if(f) 4fd800b3a8 2011-02-23 kinaba: if( m[i]<=9 ) 4fd800b3a8 2011-02-23 kinaba: ans += char(m[i]+'0'); 4fd800b3a8 2011-02-23 kinaba: else 4fd800b3a8 2011-02-23 kinaba: ans += char(m[i]-10+'A'); 4fd800b3a8 2011-02-23 kinaba: } 4fd800b3a8 2011-02-23 kinaba: return ans; 4fd800b3a8 2011-02-23 kinaba: } 4fd800b3a8 2011-02-23 kinaba: }; 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: // BEGIN CUT HERE 4fd800b3a8 2011-02-23 kinaba: #include <ctime> 4fd800b3a8 2011-02-23 kinaba: double start_time;string timer() { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) << " msec)"; return os.str(); } 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: template <typename T> string print_array(const vector<T> &V) { ostringstream os; os << "{ "; for (typename vector<T>::const_iterator iter = V.begin(); iter != V.end(); ++iter) os << '\"' << *iter << "\","; os << " }"; return os.str(); } 4fd800b3a8 2011-02-23 kinaba: int verify_case(const string &Expected, const string &Received) { if (Expected == Received) cerr << "PASSED" << timer() << endl; else { cerr << "FAILED" << timer() << endl; cerr << "\tExpected: \"" << Expected << '\"' << endl; cerr << "\tReceived: \"" << Received << '\"' << endl; } return 0;} 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: template<int N> struct Case_ { Case_(){start_time=clock();} }; 4fd800b3a8 2011-02-23 kinaba: char Test_(...); 4fd800b3a8 2011-02-23 kinaba: int Test_(Case_<0>) { 4fd800b3a8 2011-02-23 kinaba: string numbers_[] = {"HELLO"}; 4fd800b3a8 2011-02-23 kinaba: vector <string> numbers(numbers_, numbers_+sizeof(numbers_)/sizeof(*numbers_)); 4fd800b3a8 2011-02-23 kinaba: int k = 2; 4fd800b3a8 2011-02-23 kinaba: string RetVal = "ZZLLO"; 4fd800b3a8 2011-02-23 kinaba: return verify_case(RetVal, HexatridecimalSum().maximizeSum(numbers, k)); } 4fd800b3a8 2011-02-23 kinaba: int Test_(Case_<1>) { 4fd800b3a8 2011-02-23 kinaba: string numbers_[] = {"500", "POINTS", "FOR", "THIS", "PROBLEM"}; 4fd800b3a8 2011-02-23 kinaba: vector <string> numbers(numbers_, numbers_+sizeof(numbers_)/sizeof(*numbers_)); 4fd800b3a8 2011-02-23 kinaba: int k = 5; 4fd800b3a8 2011-02-23 kinaba: string RetVal = "1100TC85"; 4fd800b3a8 2011-02-23 kinaba: return verify_case(RetVal, HexatridecimalSum().maximizeSum(numbers, k)); } 4fd800b3a8 2011-02-23 kinaba: int Test_(Case_<2>) { 4fd800b3a8 2011-02-23 kinaba: string numbers_[] = {"TO", "BE", "OR", "NOT", "TO", "BE"}; 4fd800b3a8 2011-02-23 kinaba: vector <string> numbers(numbers_, numbers_+sizeof(numbers_)/sizeof(*numbers_)); 4fd800b3a8 2011-02-23 kinaba: int k = 0; 4fd800b3a8 2011-02-23 kinaba: string RetVal = "QNO"; 4fd800b3a8 2011-02-23 kinaba: return verify_case(RetVal, HexatridecimalSum().maximizeSum(numbers, k)); } 4fd800b3a8 2011-02-23 kinaba: int Test_(Case_<3>) { 4fd800b3a8 2011-02-23 kinaba: string numbers_[] = {"KEQUALS36"}; 4fd800b3a8 2011-02-23 kinaba: vector <string> numbers(numbers_, numbers_+sizeof(numbers_)/sizeof(*numbers_)); 4fd800b3a8 2011-02-23 kinaba: int k = 36; 4fd800b3a8 2011-02-23 kinaba: string RetVal = "ZZZZZZZZZ"; 4fd800b3a8 2011-02-23 kinaba: return verify_case(RetVal, HexatridecimalSum().maximizeSum(numbers, k)); } 4fd800b3a8 2011-02-23 kinaba: int Test_(Case_<4>) { 4fd800b3a8 2011-02-23 kinaba: string numbers_[] = {"GOOD", "LUCK", "AND", "HAVE", "FUN"}; 4fd800b3a8 2011-02-23 kinaba: vector <string> numbers(numbers_, numbers_+sizeof(numbers_)/sizeof(*numbers_)); 4fd800b3a8 2011-02-23 kinaba: int k = 7; 4fd800b3a8 2011-02-23 kinaba: string RetVal = "31YUB"; 4fd800b3a8 2011-02-23 kinaba: return verify_case(RetVal, HexatridecimalSum().maximizeSum(numbers, k)); } 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: template<int N> void Run_() { cerr << "Test Case #" << N << "..." << flush; Test_(Case_<N>()); Run_<sizeof(Test_(Case_<N+1>()))==1 ? -1 : N+1>(); } 4fd800b3a8 2011-02-23 kinaba: template<> void Run_<-1>() {} 4fd800b3a8 2011-02-23 kinaba: int main() { Run_<0>(); } 4fd800b3a8 2011-02-23 kinaba: // END CUT HERE 4fd800b3a8 2011-02-23 kinaba: