744af0f22d 2014-07-26 kinaba: #include <iostream> 744af0f22d 2014-07-26 kinaba: #include <sstream> 744af0f22d 2014-07-26 kinaba: #include <iomanip> 744af0f22d 2014-07-26 kinaba: #include <vector> 744af0f22d 2014-07-26 kinaba: #include <string> 744af0f22d 2014-07-26 kinaba: #include <map> 744af0f22d 2014-07-26 kinaba: #include <set> 744af0f22d 2014-07-26 kinaba: #include <algorithm> 744af0f22d 2014-07-26 kinaba: #include <numeric> 744af0f22d 2014-07-26 kinaba: #include <iterator> 744af0f22d 2014-07-26 kinaba: #include <functional> 744af0f22d 2014-07-26 kinaba: #include <complex> 744af0f22d 2014-07-26 kinaba: #include <queue> 744af0f22d 2014-07-26 kinaba: #include <stack> 744af0f22d 2014-07-26 kinaba: #include <cmath> 744af0f22d 2014-07-26 kinaba: #include <cassert> 744af0f22d 2014-07-26 kinaba: #include <tuple> 744af0f22d 2014-07-26 kinaba: using namespace std; 744af0f22d 2014-07-26 kinaba: typedef long long LL; 744af0f22d 2014-07-26 kinaba: typedef complex<double> CMP; 744af0f22d 2014-07-26 kinaba: 744af0f22d 2014-07-26 kinaba: enum Type {X, SUM, MAX}; 744af0f22d 2014-07-26 kinaba: struct Tree { 744af0f22d 2014-07-26 kinaba: Type type; 744af0f22d 2014-07-26 kinaba: Tree* left; 744af0f22d 2014-07-26 kinaba: Tree* right; 744af0f22d 2014-07-26 kinaba: int size; 744af0f22d 2014-07-26 kinaba: ~Tree() { delete left; delete right; } 744af0f22d 2014-07-26 kinaba: }; 744af0f22d 2014-07-26 kinaba: 744af0f22d 2014-07-26 kinaba: class CircuitsConstruction { public: 744af0f22d 2014-07-26 kinaba: int maximizeResistance(string circuit, vector <int> conductors) 744af0f22d 2014-07-26 kinaba: { 744af0f22d 2014-07-26 kinaba: sort(conductors.rbegin(), conductors.rend()); 744af0f22d 2014-07-26 kinaba: const char* p = circuit.c_str(); 744af0f22d 2014-07-26 kinaba: Tree* t = parse(p); 744af0f22d 2014-07-26 kinaba: int ans = solve(t, conductors); 744af0f22d 2014-07-26 kinaba: delete t; 744af0f22d 2014-07-26 kinaba: return ans; 744af0f22d 2014-07-26 kinaba: } 744af0f22d 2014-07-26 kinaba: 744af0f22d 2014-07-26 kinaba: Tree* parse(const char*& p) 744af0f22d 2014-07-26 kinaba: { 744af0f22d 2014-07-26 kinaba: char tc = *p++; 744af0f22d 2014-07-26 kinaba: Tree* t = new Tree; 744af0f22d 2014-07-26 kinaba: t->type = (tc=='X' ? X : tc=='A' ? SUM : MAX); 744af0f22d 2014-07-26 kinaba: if(tc=='X') { 744af0f22d 2014-07-26 kinaba: t->left = t->right = 0; 744af0f22d 2014-07-26 kinaba: t->size = 1; 744af0f22d 2014-07-26 kinaba: } else { 744af0f22d 2014-07-26 kinaba: t->left = parse(p); 744af0f22d 2014-07-26 kinaba: t->right = parse(p); 744af0f22d 2014-07-26 kinaba: t->size = t->left->size + t->right->size; 744af0f22d 2014-07-26 kinaba: } 744af0f22d 2014-07-26 kinaba: return t; 744af0f22d 2014-07-26 kinaba: } 744af0f22d 2014-07-26 kinaba: 744af0f22d 2014-07-26 kinaba: int plusdepth(Tree* t) 744af0f22d 2014-07-26 kinaba: { 744af0f22d 2014-07-26 kinaba: switch(t->type) 744af0f22d 2014-07-26 kinaba: { 744af0f22d 2014-07-26 kinaba: case X: 744af0f22d 2014-07-26 kinaba: return 0; 744af0f22d 2014-07-26 kinaba: case SUM: 744af0f22d 2014-07-26 kinaba: return plusdepth(t->left) + plusdepth(t->right) + 1; 744af0f22d 2014-07-26 kinaba: case MAX: 744af0f22d 2014-07-26 kinaba: return max(plusdepth(t->left), plusdepth(t->right)); 744af0f22d 2014-07-26 kinaba: } 744af0f22d 2014-07-26 kinaba: } 744af0f22d 2014-07-26 kinaba: 744af0f22d 2014-07-26 kinaba: int solve(Tree* t, const vector<int>& c) 744af0f22d 2014-07-26 kinaba: { 744af0f22d 2014-07-26 kinaba: int n = plusdepth(t)+1; 744af0f22d 2014-07-26 kinaba: return accumulate(c.begin(), c.begin()+n, 0); 744af0f22d 2014-07-26 kinaba: } 744af0f22d 2014-07-26 kinaba: }; 744af0f22d 2014-07-26 kinaba: 744af0f22d 2014-07-26 kinaba: // BEGIN CUT HERE 744af0f22d 2014-07-26 kinaba: #include <ctime> 744af0f22d 2014-07-26 kinaba: double start_time; string timer() 744af0f22d 2014-07-26 kinaba: { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) << " msec)"; return os.str(); } 744af0f22d 2014-07-26 kinaba: template<typename T> ostream& operator<<(ostream& os, const vector<T>& v) 744af0f22d 2014-07-26 kinaba: { os << "{ "; 744af0f22d 2014-07-26 kinaba: for(typename vector<T>::const_iterator it=v.begin(); it!=v.end(); ++it) 744af0f22d 2014-07-26 kinaba: os << '\"' << *it << '\"' << (it+1==v.end() ? "" : ", "); os << " }"; return os; } 744af0f22d 2014-07-26 kinaba: void verify_case(const int& Expected, const int& Received) { 744af0f22d 2014-07-26 kinaba: bool ok = (Expected == Received); 744af0f22d 2014-07-26 kinaba: if(ok) cerr << "PASSED" << timer() << endl; else { cerr << "FAILED" << timer() << endl; 744af0f22d 2014-07-26 kinaba: cerr << "\to: \"" << Expected << '\"' << endl << "\tx: \"" << Received << '\"' << endl; } } 744af0f22d 2014-07-26 kinaba: #define CASE(N) {cerr << "Test Case #" << N << "..." << flush; start_time=clock(); 744af0f22d 2014-07-26 kinaba: #define END verify_case(_, CircuitsConstruction().maximizeResistance(circuit, conductors));} 744af0f22d 2014-07-26 kinaba: int main(){ 744af0f22d 2014-07-26 kinaba: 744af0f22d 2014-07-26 kinaba: CASE(0) 744af0f22d 2014-07-26 kinaba: string circuit = "BXBXX"; 744af0f22d 2014-07-26 kinaba: int conductors_[] = {8, 2, 3}; 744af0f22d 2014-07-26 kinaba: vector <int> conductors(conductors_, conductors_+sizeof(conductors_)/sizeof(*conductors_)); 744af0f22d 2014-07-26 kinaba: int _ = 8; 744af0f22d 2014-07-26 kinaba: END 744af0f22d 2014-07-26 kinaba: CASE(1) 744af0f22d 2014-07-26 kinaba: string circuit = "AAXXAXAXX"; 744af0f22d 2014-07-26 kinaba: int conductors_[] = {1, 1, 2, 8, 10}; 744af0f22d 2014-07-26 kinaba: vector <int> conductors(conductors_, conductors_+sizeof(conductors_)/sizeof(*conductors_)); 744af0f22d 2014-07-26 kinaba: int _ = 22; 744af0f22d 2014-07-26 kinaba: END 744af0f22d 2014-07-26 kinaba: CASE(2) 744af0f22d 2014-07-26 kinaba: string circuit = "AXBXX"; 744af0f22d 2014-07-26 kinaba: int conductors_[] = {8, 2, 3}; 744af0f22d 2014-07-26 kinaba: vector <int> conductors(conductors_, conductors_+sizeof(conductors_)/sizeof(*conductors_)); 744af0f22d 2014-07-26 kinaba: int _ = 11; 744af0f22d 2014-07-26 kinaba: END 744af0f22d 2014-07-26 kinaba: CASE(3) 744af0f22d 2014-07-26 kinaba: string circuit = "BAAXBXXBXAXXBBAXXBXXAAXXX"; 744af0f22d 2014-07-26 kinaba: int conductors_[] = {17, 7, 21, 102, 56, 72, 88, 15, 9, 192, 16, 8, 30}; 744af0f22d 2014-07-26 kinaba: vector <int> conductors(conductors_, conductors_+sizeof(conductors_)/sizeof(*conductors_)); 744af0f22d 2014-07-26 kinaba: int _ = 454; 744af0f22d 2014-07-26 kinaba: END 744af0f22d 2014-07-26 kinaba: /* 744af0f22d 2014-07-26 kinaba: CASE(4) 744af0f22d 2014-07-26 kinaba: string circuit = ; 744af0f22d 2014-07-26 kinaba: int conductors_[] = ; 744af0f22d 2014-07-26 kinaba: vector <int> conductors(conductors_, conductors_+sizeof(conductors_)/sizeof(*conductors_)); 744af0f22d 2014-07-26 kinaba: int _ = ; 744af0f22d 2014-07-26 kinaba: END 744af0f22d 2014-07-26 kinaba: CASE(5) 744af0f22d 2014-07-26 kinaba: string circuit = ; 744af0f22d 2014-07-26 kinaba: int conductors_[] = ; 744af0f22d 2014-07-26 kinaba: vector <int> conductors(conductors_, conductors_+sizeof(conductors_)/sizeof(*conductors_)); 744af0f22d 2014-07-26 kinaba: int _ = ; 744af0f22d 2014-07-26 kinaba: END 744af0f22d 2014-07-26 kinaba: */ 744af0f22d 2014-07-26 kinaba: } 744af0f22d 2014-07-26 kinaba: // END CUT HERE