ADDED SRM/533-U/1A.cpp Index: SRM/533-U/1A.cpp ================================================================== --- SRM/533-U/1A.cpp +++ SRM/533-U/1A.cpp @@ -0,0 +1,103 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#ifdef __GNUC__ +#include +#define unordered_map __gnu_cxx::hash_map +#else +#include +#endif +using namespace std; +typedef long long LL; +typedef complex CMP; + +class CasketOfStar { public: + int maxEnergy(vector weight) + { + return rec(weight, 0, weight.size()-1); + } + + map, int> memo; + int rec(const vector& w, int L, int R) + { + if( L+1 == R ) + return 0; + pair key(L,R); + if( memo.count(key) ) + return memo[key]; + int best = 0; + for(int C=L+1; C +double start_time; string timer() + { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) << " msec)"; return os.str(); } +template ostream& operator<<(ostream& os, const vector& v) + { os << "{ "; + for(typename vector::const_iterator it=v.begin(); it!=v.end(); ++it) + os << '\"' << *it << '\"' << (it+1==v.end() ? "" : ", "); os << " }"; return os; } +void verify_case(const int& Expected, const int& Received) { + bool ok = (Expected == Received); + if(ok) cerr << "PASSED" << timer() << endl; else { cerr << "FAILED" << timer() << endl; + cerr << "\to: \"" << Expected << '\"' << endl << "\tx: \"" << Received << '\"' << endl; } } +#define CASE(N) {cerr << "Test Case #" << N << "..." << flush; start_time=clock(); +#define END verify_case(_, CasketOfStar().maxEnergy(weight));} +int main(){ + +CASE(0) + int weight_[] = {1,2,3,4}; + vector weight(weight_, weight_+sizeof(weight_)/sizeof(*weight_)); + int _ = 12; +END +CASE(1) + int weight_[] = {100,2,1,3,100}; + vector weight(weight_, weight_+sizeof(weight_)/sizeof(*weight_)); + int _ = 10400; +END +CASE(2) + int weight_[] = {2,2,7,6,90,5,9}; + vector weight(weight_, weight_+sizeof(weight_)/sizeof(*weight_)); + int _ = 1818; +END +CASE(3) + int weight_[] = {477,744,474,777,447,747,777,474}; + vector weight(weight_, weight_+sizeof(weight_)/sizeof(*weight_)); + int _ = 2937051; +END +CASE(4) + int weight_[] = {1,1,1,1,1,1,1,1,1,1,1,1,1,1,1}; + vector weight(weight_, weight_+sizeof(weight_)/sizeof(*weight_)); + int _ = 13; +END +/* +CASE(5) + int weight_[] = ; + vector weight(weight_, weight_+sizeof(weight_)/sizeof(*weight_)); + int _ = ; +END +CASE(6) + int weight_[] = ; + vector weight(weight_, weight_+sizeof(weight_)/sizeof(*weight_)); + int _ = ; +END +*/ +} +// END CUT HERE ADDED SRM/533-U/1B.cpp Index: SRM/533-U/1B.cpp ================================================================== --- SRM/533-U/1B.cpp +++ SRM/533-U/1B.cpp @@ -0,0 +1,220 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#ifdef __GNUC__ +#include +#define unordered_map __gnu_cxx::hash_map +#else +#include +#endif +using namespace std; +typedef long long LL; +typedef complex CMP; + +struct UnionFind +{ + vector uf, sz; + int nc; + + UnionFind(int N): uf(N), sz(N,1), nc(N) + { for(int i=0; i= sz[b] ) swap(a, b); + uf[a] = b; + sz[b] += sz[a]; + --nc; + } + return (a!=b); + } +}; + +class MagicBoard { public: + string ableToUnlock(vector board) + { + const int H = board.size(); + const int W = board[0].size(); + vector deg(H+W); + UnionFind uf(H*W); + + int num = 0; + for(int y=0; y +double start_time; string timer() + { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) << " msec)"; return os.str(); } +template ostream& operator<<(ostream& os, const vector& v) + { os << "{ "; + for(typename vector::const_iterator it=v.begin(); it!=v.end(); ++it) + os << '\"' << *it << '\"' << (it+1==v.end() ? "" : ", "); os << " }"; return os; } +void verify_case(const string& Expected, const string& Received) { + bool ok = (Expected == Received); + if(ok) cerr << "PASSED" << timer() << endl; else { cerr << "FAILED" << timer() << endl; + cerr << "\to: \"" << Expected << '\"' << endl << "\tx: \"" << Received << '\"' << endl; } } +#define CASE(N) {cerr << "Test Case #" << N << "..." << flush; start_time=clock(); +#define END verify_case(_, MagicBoard().ableToUnlock(board));} +int main(){ + +CASE(0) + string board_[] = {"##", + ".#"}; + vector board(board_, board_+sizeof(board_)/sizeof(*board_)); + string _ = "YES"; +END +CASE(1) + string board_[] = {"#.", + ".#"}; + vector board(board_, board_+sizeof(board_)/sizeof(*board_)); + string _ = "NO"; +END +CASE(2) + string board_[] = {"##", + "##", + "##"}; + vector board(board_, board_+sizeof(board_)/sizeof(*board_)); + string _ = "YES"; +END +CASE(3) + string board_[] = {"###", + "###", + "###"}; + vector board(board_, board_+sizeof(board_)/sizeof(*board_)); + string _ = "NO"; +END +CASE(4) + string board_[] = {"###", + "..#", + "###", + "#..", + "###"}; + vector board(board_, board_+sizeof(board_)/sizeof(*board_)); + string _ = "NO"; +END +CASE(5) + string board_[] = {"................", + ".######..######.", + ".######..######.", + ".##......##..##.", + ".##......##..##.", + ".######..######.", + ".######..######.", + ".....##..##..##.", + ".....##..##..##.", + ".######..######.", + ".######..######.", + "................"}; + vector board(board_, board_+sizeof(board_)/sizeof(*board_)); + string _ = "YES"; +END +CASE(6) + string board_[] = {"#"}; + vector board(board_, board_+sizeof(board_)/sizeof(*board_)); + string _ = "YES"; +END +CASE(7) +string board_[] = {".#"}; + vector board(board_, board_+sizeof(board_)/sizeof(*board_)); + string _ = "YES"; +END +CASE(8) +string board_[] = { + "#################################################", + "#################################################", + "#################################################", + "#################################################", + "#################################################", + "#################################################", + "#################################################", + "#################################################", + "#################################################", + "#################################################", + "#################################################", + "#################################################", + "#################################################", + "#################################################", + "#################################################", + "#################################################", + "#################################################", + "#################################################", + "#################################################", + "#################################################", + "#################################################", + "#################################################", + "#################################################", + "#################################################", + "#################################################", + "#################################################", + "#################################################", + "#################################################", + "#################################################", + "#################################################", + "#################################################", + "#################################################", + "#################################################", + "#################################################", + "#################################################", + "#################################################", + "#################################################", + "#################################################", + "#################################################", + "#################################################", + "#################################################", + "#################################################", + "#################################################", + "#################################################", + "#################################################", + "#################################################", + "#################################################", + "#################################################", + "#################################################", + "#################################################", +}; + vector board(board_, board_+sizeof(board_)/sizeof(*board_)); + string _ = "???"; +END + +} +// END CUT HERE