ADDED SRM/563/1A.cpp Index: SRM/563/1A.cpp ================================================================== --- SRM/563/1A.cpp +++ SRM/563/1A.cpp @@ -0,0 +1,111 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +using namespace std; +typedef long long LL; +typedef long double LD; +typedef complex CMP; + +class FoxAndHandle { public: + string lexSmallestName(string S) + { + string cur = ""; + int p = 0; + while(cur.size()*2cur+S[x]) + next_best=cur+S[x], next_p=x+1; + } + cur = next_best; + p = next_p; + } + return cur; + } + + bool possible(const string& used, int x, const string& S) + { + int freq[26] = {0}; + int pos[26] = {0}; + int tot[26] = {0}; + for(int i=0; itot[c]) + return false; + return true; + } +}; + +// BEGIN CUT HERE +#include +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(_, FoxAndHandle().lexSmallestName(S));} +int main(){ + +CASE(0) + string S = "foxfox"; + string _ = "fox"; +END +CASE(1) + string S = "ccieliel"; + string _ = "ceil"; +END +CASE(2) + string S = "abaabbab"; + string _ = "aabb"; +END +CASE(3) + string S = "bbbbaaaa"; + string _ = "bbaa"; +END +CASE(4) + string S = "fedcbafedcba"; + string _ = "afedcb"; +END +CASE(5) + string S = "nodevillivedon"; + string _ = "deilvon"; +END +/* +CASE(6) + string S = ; + string _ = ; +END +CASE(7) + string S = ; + string _ = ; +END +*/ +} +// END CUT HERE ADDED SRM/563/1B.cpp Index: SRM/563/1B.cpp ================================================================== --- SRM/563/1B.cpp +++ SRM/563/1B.cpp @@ -0,0 +1,159 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +using namespace std; +typedef long long LL; +typedef long double LD; +typedef complex CMP; + +template +struct DP2 +{ + const int N1, N2; + vector data; + DP2(int N1, int N2, const T& t = T()) + : N1(N1), N2(N2), data(N1*N2, t) { assert(data.size()*sizeof(T)<(1<<26)); } + T& operator()(int i1, int i2) + { return data[ (i1*N2)+i2 ]; } + void swap(DP2& rhs) + { data.swap(rhs.data); } +}; + +class SpellCards { public: + int maxDamage(vector level, vector damage) + { + static const int IMPOSSIBLE = -99999999; + + int N = level.size(); + DP2 dp(N, N+1, IMPOSSIBLE); + for(int w=1; w<=N; ++w) + for(int s=0; s ip(L+1, w, IMPOSSIBLE); + ip(1,0) = D; + for(int c=1; c<=L; ++c) + for(int k=1; k best(N+1); + for(int k=0; k +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(_, SpellCards().maxDamage(level, damage));} +int main(){ + +CASE(0) + int level_[] = {1,1,1}; + vector level(level_, level_+sizeof(level_)/sizeof(*level_)); + int damage_[] = {10,20,30}; + vector damage(damage_, damage_+sizeof(damage_)/sizeof(*damage_)); + int _ = 60; +END +CASE(1) + int level_[] = {3,3,3}; + vector level(level_, level_+sizeof(level_)/sizeof(*level_)); + int damage_[] = {10,20,30}; + vector damage(damage_, damage_+sizeof(damage_)/sizeof(*damage_)); + int _ = 30; +END +CASE(2) + int level_[] = {4,4,4}; + vector level(level_, level_+sizeof(level_)/sizeof(*level_)); + int damage_[] = {10,20,30}; + vector damage(damage_, damage_+sizeof(damage_)/sizeof(*damage_)); + int _ = 0; +END +CASE(3) + int level_[] = {50,1,50,1,50}; + vector level(level_, level_+sizeof(level_)/sizeof(*level_)); + int damage_[] = {10,20,30,40,50}; + vector damage(damage_, damage_+sizeof(damage_)/sizeof(*damage_)); + int _ = 60; +END +CASE(4) + int level_[] = {2,1,1}; + vector level(level_, level_+sizeof(level_)/sizeof(*level_)); + int damage_[] = {40,40,10}; + vector damage(damage_, damage_+sizeof(damage_)/sizeof(*damage_)); + int _ = 80; +END +CASE(5) + int level_[] = {1,2,1,1,3,2,1}; + vector level(level_, level_+sizeof(level_)/sizeof(*level_)); + int damage_[] = {10,40,10,10,90,40,10}; + vector damage(damage_, damage_+sizeof(damage_)/sizeof(*damage_)); + int _ = 170; +END +CASE(6) + int level_[] = {1,2,2,3,1,4,2}; + vector level(level_, level_+sizeof(level_)/sizeof(*level_)); + int damage_[] = {113,253,523,941,250,534,454}; + vector damage(damage_, damage_+sizeof(damage_)/sizeof(*damage_)); + int _ = 1918; +END +CASE(7) + int level_[] = {4,8,50,24,4,32,6,39,3,2,4,4,39,39,10,3,42,36,38,18,6,18,41,15,28,40,45,26,40,30,25,46,18,35,29,25,48,10,18,25,46,33,30,26,36,8,20,35,28,29}; + vector level(level_, level_+sizeof(level_)/sizeof(*level_)); + int damage_[] = {8577,2855,8317,7176,295,7028,5317,7099,4642,29,2481,7249,5387,6266,6241,4550,9178,2375,1037,4438,4189,5886,5285,3583,2874,8484,4725,2096,7560,4057,988,5914,3460,1305,6223,2305,6229,2719,2404,6454,278,5242,2948,9607,1302,4263,3366,4925,8381,3692}; + vector damage(damage_, damage_+sizeof(damage_)/sizeof(*damage_)); + int _ = -2; +END +CASE(8) + int level_[] = {25,25,25,25,25,25,25,25,25,25,25,25,25,25,25,25,25,25,25,25,25,25,25,25,25,25,25,25,25,25,25,25,25,25,25,25,25,25,25,25,25,25,25,25,25,25,25,25,25,25}; + vector level(level_, level_+sizeof(level_)/sizeof(*level_)); + int damage_[] = {8577,2855,8317,7176,295,7028,5317,7099,4642,29,2481,7249,5387,6266,6241,4550,9178,2375,1037,4438,4189,5886,5285,3583,2874,8484,4725,2096,7560,4057,988,5914,3460,1305,6223,2305,6229,2719,2404,6454,278,5242,2948,9607,1302,4263,3366,4925,8381,3692}; + vector damage(damage_, damage_+sizeof(damage_)/sizeof(*damage_)); + int _ = -2; +END +} +// END CUT HERE ADDED SRM/563/1C.cpp Index: SRM/563/1C.cpp ================================================================== --- SRM/563/1C.cpp +++ SRM/563/1C.cpp @@ -0,0 +1,346 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +using namespace std; +typedef long long LL; +typedef long double LD; +typedef complex CMP; + +static const unsigned MODVAL = 1000000009; +struct mint +{ + unsigned val; + mint():val(0){} + mint(int x):val(x%MODVAL) {} + mint(unsigned x):val(x%MODVAL) {} + mint(LL x):val(x%MODVAL) {} +}; +mint& operator+=(mint& x, mint y) { return x = x.val+y.val; } +mint& operator-=(mint& x, mint y) { return x = x.val-y.val+MODVAL; } +mint& operator*=(mint& x, mint y) { return x = LL(x.val)*y.val; } +mint operator+(mint x, mint y) { return x+=y; } +mint operator-(mint x, mint y) { return x-=y; } +mint operator*(mint x, mint y) { return x*=y; } +mint POW(mint x, LL e) { mint v=1; for(;e;x*=x,e>>=1) if(e&1) v*=x; return v; } + +class CoinsGame { public: + int ways(vector board) + { + map,int> ID; + for(int y=0; y > G(ID.size()+1); + G[0] = vector(4, 0); + + int H = board.size(); + int W = board[0].size(); + for(int y=0; y >& G) + { + vector< vector > set_buffer; + set active_sets; + set active_splitters; + + { + set_buffer.push_back( vector(1,0) ); + vector all; + for(int v=1; v& splv = set_buffer[*active_splitters.begin()]; + set spl(splv.begin(), splv.end()); + active_splitters.erase(active_splitters.begin()); + + for(int d=0; d<4; ++d) + for(set::iterator it=active_sets.begin(); it!=active_sets.end(); ) + { + int ii = *it++; + const vector& cur = set_buffer[ii]; + vector t, f; + for(int k=0; k es; + for(set::iterator it=active_sets.begin(); it!=active_sets.end(); ++it) + if(*it) + es.push_back(set_buffer[*it].size()); +/* + for(bool changed=true; changed;) + { + changed=false; + for(int i=0; i spl(eqs[i].first.begin(), eqs[i].first.end()); + eqs[i].second = true; + for(int k=0; k t, f; + for(vector::iterator it=eqs[k].first.begin(); it!=eqs[k].first.end(); ++it) + (spl.count(G[*it][d]) ? t : f).push_back(*it); + if(!t.empty() && !f.empty()) { + if(eqs[k].second) { + eqs.push_back(make_pair(t,t.size()>f.size())); + eqs.push_back(make_pair(f,t.size()<=f.size())); + dead.push_back(false); + dead.push_back(false); + changed = true; + } else { + eqs.push_back(make_pair(t,false)); + eqs.push_back(make_pair(f,false)); + dead.push_back(false); + dead.push_back(false); + changed = true; + } + dead[k] = true; + } + } + } + } + + vector es; + for(int i=1; i +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(_, CoinsGame().ways(board));} +int main(){ + +CASE(0) + string board_[] = {".."}; + vector board(board_, board_+sizeof(board_)/sizeof(*board_)); + int _ = 1; +END +CASE(1) + string board_[] = {"##.#", + ".###", + "###.", + "#.##"}; + vector board(board_, board_+sizeof(board_)/sizeof(*board_)); + int _ = 11; +END +CASE(2) + string board_[] = {"####", + "#..#", + "#..#", + "####"}; + vector board(board_, board_+sizeof(board_)/sizeof(*board_)); + int _ = 0; +END +CASE(3) + string board_[] = {"#.#.#"}; + vector board(board_, board_+sizeof(board_)/sizeof(*board_)); + int _ = 0; +END +CASE(4) + string board_[] = {"........", + "........", + "........", + "........", + "........", + "........", + "........", + "........"}; + vector board(board_, board_+sizeof(board_)/sizeof(*board_)); + int _ = 688856388; +END +CASE(5) + string board_[] = {"........................................", "........................................", "........................................", "........................................", "........................................", "........................................", "........................................", "........................................", "........................................", "........................................", "........................................", "........................................", "........................................", "........................................", "........................................", "........................................", "........................................", "........................................", "........................................", "........................................", "........................................", "........................................", "........................................", "........................................", "........................................", "........................................", "........................................", "........................................", "........................................", "........................................", "........................................", "........................................", "........................................", "........................................", "........................................", "........................................", "........................................", "........................................", "........................................", "........................................"}; + vector board(board_, board_+sizeof(board_)/sizeof(*board_)); + int _ = 150087966; +END +CASE(5) + string board_[] = {"........................................", + "........................................", + "........................................", + "........................................", + "........................................", + "........................................", + "........................................", + "........................................", + "........................................", + "........................................", + "........................................", + "........................................", + "........................................", + "........................................", + "........................................", + "........................................", + "........................................", + "........................................", + "........................................", + "........................................",}; + vector board(board_, board_+sizeof(board_)/sizeof(*board_)); + int _ = 150087966; +END +CASE(5) + string board_[] = {"........................................", + "........................................", + "........................................", + "........................................", + "........................................", + "........................................", + "........................................", + "........................................", + "........................................", + "........................................", + "........................................", + "........................................", + "........................................", + "........................................", + "........................................", + "........................................", + "........................................", + "........................................", + "........................................", + "........................................", +"........................................", + "........................................", + "........................................", + "........................................", + "........................................", + "........................................", + "........................................", + "........................................", + "........................................", + "........................................", + "........................................", + "........................................", + "........................................", + "........................................", + "........................................", + "........................................", + "........................................", + "........................................", + "........................................", + "........................................", +"........................................", + "........................................", + "........................................", + "........................................", + "........................................", + "........................................", + "........................................", + "........................................", + "........................................", + "........................................", + "........................................", + "........................................", + "........................................", + "........................................", + "........................................", + "........................................", + "........................................", + "........................................", + "........................................", + "........................................", +"........................................", + "........................................", + "........................................", + "........................................", + "........................................", + "........................................", + "........................................", + "........................................", + "........................................", + "........................................", + "........................................", + "........................................", + "........................................", + "........................................", + "........................................", + "........................................", + "........................................", + "........................................", + "........................................", + "........................................",}; + vector board(board_, board_+sizeof(board_)/sizeof(*board_)); + int _ = 150087966; +END +} +// END CUT HERE