ADDED CheckList.pptx Index: CheckList.pptx ================================================================== --- CheckList.pptx +++ CheckList.pptx cannot compute difference between binary files ADDED SRM/144/1A.cpp Index: SRM/144/1A.cpp ================================================================== --- SRM/144/1A.cpp +++ SRM/144/1A.cpp @@ -0,0 +1,26 @@ +#include +#include +using namespace std; + +struct BinaryCode +{ + vector decode( string message ) + { + vector ans; + ans.push_back( decode(0, message) ); + ans.push_back( decode(1, message) ); + return ans; + } + + string decode(int c, string Q) + { + string P; P += char('0'+c); + for(int i=0; i +#include +#include +#include +#include +using namespace std; + +struct Lottery +{ + vector sortByOdds(vector rules) + { + vector< pair > lots; + for(int i=0; i> ch >> bl >> so >> un; + + lots.push_back( make_pair(numChoice(ch,bl,so=='T',un=='T'), name) ); + } + sort( lots.begin(), lots.end() ); + + vector ans; + for(int i=0; i +#include +#include +#include +#include +using namespace std; + +struct PenLift +{ + typedef pair point; + typedef map< point, set > graph; + + int numTimes( vector segments, int n ) + { + // Determine All Significant Coordinates + set xs, ys; + for(int i=0; i> x1 >> y1 >> x2 >> y2; + xs.insert(x1); + xs.insert(x2); + ys.insert(y1); + ys.insert(y2); + } + + // Construct the graph, separated by all significant coordinates + graph G; + for(int i=0; i> x1 >> y1 >> x2 >> y2; + if( x1 == x2 ) + { + set::iterator it = ys.lower_bound(min(y1,y2)); + set::iterator ed = ys.upper_bound(max(y1,y2)); + for(set::iterator pv=it++; it!=ed; ++it,++pv) + G[point(x1,*pv)].insert(point(x1,*it)), + G[point(x1,*it)].insert(point(x1,*pv)); + } + else + { + set::iterator it = xs.lower_bound(min(x1,x2)); + set::iterator ed = xs.upper_bound(max(x1,x2)); + for(set::iterator pv=it++; it!=ed; ++it,++pv) + G[point(*pv,y1)].insert(point(*it,y1)), + G[point(*it,y1)].insert(point(*pv,y1)); + } + } + + // count the number of odd vertices (/2) per connected components + set vis; + + int cnt = 0; + for(graph::iterator s=G.begin(); s!=G.end(); ++s) + if( !vis.count(s->first) ) + cnt += max(1, numOdd(s->first, G, vis, n)/2); + return cnt - 1; // first touch do not require a pen-lift + } + + // simple dfs + int numOdd( point p, graph& G, set& vis, int n ) + { + if( vis.count(p) ) + return 0; + vis.insert(p); + + int cnt = G[p].size()*n % 2; // if odd degree then 1 else 0 + for(set::iterator it=G[p].begin(); it!=G[p].end(); ++it) + cnt += numOdd(*it, G, vis, n); + return cnt; + } +}; ADDED SRM/145/1A.cpp Index: SRM/145/1A.cpp ================================================================== --- SRM/145/1A.cpp +++ SRM/145/1A.cpp @@ -0,0 +1,27 @@ +#include +#include +#include +#include +using namespace std; + +struct Bonuses +{ + vector getDivision(vector points) + { + int sum = accumulate(points.begin(), points.end(), 0); + + int left = 100; + vector ans(points.size()); + for(int i=0; i > poid(points.size()); + for(int i=0; i +#include +#include +#include +using namespace std; + +struct VendingMachine +{ + int motorUse(vector prices, vector purchases) + { + vector sum; + vector< vector > pri(prices.size()); + for(int i=0; i>n; ++j) + {if(j>=sum.size())sum.push_back(0); sum[j]+=n; pri[i].push_back(n);} + } + int M = pri[0].size(); + + int mot = 0; + int pc=0, pt=-5; + for(int i=0; i= 5 ) + { + // rotate + int hc = max_element(sum.begin(), sum.end()) - sum.begin(); + mot += min( abs(hc-pc), M-abs(hc-pc) ); + pc = hc; + } + + // buy + sum[cl] -= pri[sh][cl]; + pri[sh][cl] = 0; + mot += min( abs(cl-pc), M-abs(cl-pc) ); + pc = cl; + pt = t; + } + + // rotate + int hc = max_element(sum.begin(), sum.end()) - sum.begin(); + mot += min( abs(hc-pc), M-abs(hc-pc) ); + pc = hc; + + return mot; + } +}; ADDED SRM/145/1C.cpp Index: SRM/145/1C.cpp ================================================================== --- SRM/145/1C.cpp +++ SRM/145/1C.cpp @@ -0,0 +1,46 @@ +#include +using namespace std; + +struct HillHike +{ + long long numPaths(int distance, int maxHeight, vector landmarks) + { + // reached-maxHeight?, #reached-landmarks, current-height + long long buf1[2][51][51]={0}, buf2[2][51][51], (*dp)[51][51]=buf1; + dp[0][0][0] = 1; + + for(int d=1; d<=distance; ++d) + { + long long (*dp2)[51][51] = (dp==buf1 ? buf2 : buf1); + memset(dp2, 0, sizeof(long long)*2*51*51); + + for(int rm=0; rm<=1; ++rm) + for(int rl=0; rl<=landmarks.size(); ++rl) + for(int ch=0; ch<=maxHeight; ++ch) + { + if(ch>1 || d==distance && ch>0) + { + dp2[rm] + [rl0 || d==distance) + { + dp2[rm] + [rl +#include +#include +#include +#include +using namespace std; + +struct Masterbrain +{ + int possibleSecrets(vector guesses, vector results) + { + int N = guesses.size(); + int cnt = 0; + for(int n=0; n<7*7*7*7; ++n) + { + int x[] = {n/7/7/7%7+1,n/7/7%7+1,n/7%7+1,n%7+1}; + + bool possible = false; + for(int lie=0; lie xs, gs; + for(int i=0; i<4; ++i) + if( g[i] == x[i] ) + true_b++; + else + xs.insert(x[i]), gs.insert(g[i]); + vector p; + set_intersection(xs.begin(), xs.end(), gs.begin(), gs.end(), back_inserter(p)); + return true_b==b && w==p.size(); + } +}; ADDED SRM/146/1C.cpp Index: SRM/146/1C.cpp ================================================================== --- SRM/146/1C.cpp +++ SRM/146/1C.cpp @@ -0,0 +1,64 @@ +#include +#include +#include +using namespace std; + +struct Roundabout +{ + int clearUpTime(string north, string east, string south, string west) + { + char M[4] = {'N', 'E', 'S', 'W'}; + string C[4] = {north, east, south, west}; + char R[4] = {'-', '-', '-', '-'}; + queue Q[4]; + + int N = 0; + for(int i=0; i<4; ++i) + for(int j=0; j +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +using namespace std; +typedef long long LL; + +double f(int a, int n) +{ + double x = 1; + while( n --> 0 ) + x *= a--; + return x; +} +double f(int a) +{ + return f(a, a); +} +double C(int n, int k) +{ + return f(n,k) / f(k,k); +} + +class Flush +{ +public: + double size(vector suits, int number) + { + double all = f(accumulate(suits.begin(), suits.end(), 0), number); + + double ans = 0; + for(int i1=0; i1<=number && i1<=suits[0]; ++i1) + for(int i2=0; i1+i2<=number && i2<=suits[1]; ++i2) + for(int i3=0; i1+i2+i3<=number && i3<=suits[2]; ++i3) + { + int i4 = number - i1 - i2 - i3; + if( i4 <= suits[3] ) { + double nf = max(max(i1,i2),max(i3,i4)); + double n = C(number, i1) * C(number-i1, i2) * C(number-i1-i2, i3) + * f(suits[0],i1) * f(suits[1],i2) * f(suits[2],i3) * f(suits[3],i4); + ans += nf * n/all; + } + } + return ans; + } +}; + +// 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 string print_array(const vector &V) { ostringstream os; os << "{ "; for (typename vector::const_iterator iter = V.begin(); iter != V.end(); ++iter) os << '\"' << *iter << "\","; os << " }"; return os.str(); } +int verify_case(const double &Expected, const double &Received) { double diff = Expected - Received; if (diff < 0) diff = -diff; if (diff < 1e-9) cerr << "PASSED" << timer() << endl; else { cerr << "FAILED" << timer() << endl; cerr << "\tExpected: \"" << Expected << '\"' << endl; cerr << "\tReceived: \"" << Received << '\"' << endl; } return 0;} + +template struct Case_ { Case_(){start_time=clock();} }; +char Test_(...); +int Test_(Case_<0>) { + int suits_[] = {2,2,2,2}; + vector suits(suits_, suits_+sizeof(suits_)/sizeof(*suits_)); + int number = 2; + double RetVal = 1.1428571428571428; + return verify_case(RetVal, Flush().size(suits, number)); } +int Test_(Case_<1>) { + int suits_[] = {1,4,7,10}; + vector suits(suits_, suits_+sizeof(suits_)/sizeof(*suits_)); + int number = 22; + double RetVal = 10.0; + return verify_case(RetVal, Flush().size(suits, number)); } +int Test_(Case_<2>) { + int suits_[] = {13, 13, 13, 13}; + vector suits(suits_, suits_+sizeof(suits_)/sizeof(*suits_)); + int number = 49; + double RetVal = 13.0; + return verify_case(RetVal, Flush().size(suits, number)); } +int Test_(Case_<3>) { + int suits_[] = {13, 13, 13, 13}; + vector suits(suits_, suits_+sizeof(suits_)/sizeof(*suits_)); + int number = 26; + double RetVal = 8.351195960938014; + return verify_case(RetVal, Flush().size(suits, number)); } +int Test_(Case_<4>) { + int suits_[] = {13,13,13,13}; + vector suits(suits_, suits_+sizeof(suits_)/sizeof(*suits_)); + int number = 0; + double RetVal = 0.0; + return verify_case(RetVal, Flush().size(suits, number)); } + +template void Run_() { cerr << "Test Case #" << N << "..." << flush; Test_(Case_()); Run_()))==1 ? -1 : N+1>(); } +template<> void Run_<-1>() {} +int main() { Run_<0>(); } +// END CUT HERE + ADDED SRM/245/1B.cpp Index: SRM/245/1B.cpp ================================================================== --- SRM/245/1B.cpp +++ SRM/245/1B.cpp @@ -0,0 +1,93 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +using namespace std; +typedef long long LL; + +class BikeLock +{ +public: + map,int> memo; + int minTurns(string current, string desired) + { + current += "__"; + return rec(0, current.size()-2, current, desired); + } + + int rec(int i, int N, string& s, const string& d) + { + if( i == N ) + return 0; + if( s[i] == d[i] ) + return rec(i+1, N, s, d); + pair key( i, s.substr(i,3) ); + if( memo.count(key) ) + return memo[key]; + memo[key] = 9999; + + int ans = 9999; + for(int w=1; w<=3; ++w) + for(int r=-3; r<=3; ++r) if(r) + { + for(int j=0; j +double start_time;string timer() { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) << " msec)"; return os.str(); } + +template string print_array(const vector &V) { ostringstream os; os << "{ "; for (typename vector::const_iterator iter = V.begin(); iter != V.end(); ++iter) os << '\"' << *iter << "\","; os << " }"; return os.str(); } +int verify_case(const int &Expected, const int &Received) { if (Expected == Received) cerr << "PASSED" << timer() << endl; else { cerr << "FAILED" << timer() << endl; cerr << "\tExpected: \"" << Expected << '\"' << endl; cerr << "\tReceived: \"" << Received << '\"' << endl; } return 0;} + +template struct Case_ { Case_(){start_time=clock();} }; +char Test_(...); +int Test_(Case_<0>) { + string current = "555"; + string desired = "464"; + int RetVal = 2; + return verify_case(RetVal, BikeLock().minTurns(current, desired)); } +int Test_(Case_<1>) { + string current = "1234"; + string desired = "3456"; + int RetVal = 2; + return verify_case(RetVal, BikeLock().minTurns(current, desired)); } +int Test_(Case_<2>) { + string current = "06012005"; + string desired = "06012005"; + int RetVal = 0; + return verify_case(RetVal, BikeLock().minTurns(current, desired)); } +int Test_(Case_<3>) { + string current = "123456789"; + string desired = "567412490"; + int RetVal = 5; + return verify_case(RetVal, BikeLock().minTurns(current, desired)); } +int Test_(Case_<4>) { + string current = "23887547676234543215443276347856987698648735634265"; + string desired = "14327805497625497814327632146531429785698765309822"; + int RetVal = 34; + return verify_case(RetVal, BikeLock().minTurns(current, desired)); } + +template void Run_() { cerr << "Test Case #" << N << "..." << flush; Test_(Case_()); Run_()))==1 ? -1 : N+1>(); } +template<> void Run_<-1>() {} +int main() { Run_<0>(); } +// END CUT HERE + ADDED SRM/245/1C.cpp Index: SRM/245/1C.cpp ================================================================== --- SRM/245/1C.cpp +++ SRM/245/1C.cpp @@ -0,0 +1,160 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +using namespace std; +typedef long long LL; + +int N; +vector A; + +typedef int vert; +typedef int cost; +typedef pair edge; +typedef vector edges; + +vector dijkstra_like( vert S, int T, int I ) +{ + set ans; + for(int i=1; i<=I; ++i) ans.insert(i); + + priority_queue< edge, edges, greater > Q; + Q.push( edge(0, S) ); + +// set vis; + vector vis(1<<24); + + while( !Q.empty() ) + { + edge ce=Q.top(); Q.pop(); + + int t = ce.first; + vert v = ce.second; + if( vis[v] ) + continue; + vis[v]=true; + + int sw = v&511; + int a[] = {(v>>9)&31, (v>>14)&31, (v>>19)&31}; + + ans.erase(sw); + for(int i=0; i<(1< T ) + continue; + for(int j=0; j(ans.begin(), ans.end()); +} + +class SandTimers +{ +public: + vector badIntervals(vector timers, int maxInterval, int maxTime) + { + N = timers.size(); + A = timers; + return dijkstra_like( vert(), maxTime, maxInterval ); + } +}; + +// 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 string print_array(const vector &V) { ostringstream os; os << "{ "; for (typename vector::const_iterator iter = V.begin(); iter != V.end(); ++iter) os << '\"' << *iter << "\","; os << " }"; return os.str(); } +int verify_case(const vector &Expected, const vector &Received) { if (Expected == Received) cerr << "PASSED" << timer() << endl; else { cerr << "FAILED" << timer() << endl; cerr << "\tExpected: " << print_array(Expected) << endl; cerr << "\tReceived: " << print_array(Received) << endl; } return 0;} + +template struct Case_ { Case_(){start_time=clock();} }; +char Test_(...); +int Test_(Case_<0>) { + int timers_[] = { 5, 7 }; + vector timers(timers_, timers_+sizeof(timers_)/sizeof(*timers_)); + int maxInterval = 10; + int maxTime = 10; + int RetVal_[] = {1, 6, 8 }; + vector RetVal(RetVal_, RetVal_+sizeof(RetVal_)/sizeof(*RetVal_)); + return verify_case(RetVal, SandTimers().badIntervals(timers, maxInterval, maxTime)); } +int Test_(Case_<1>) { + int timers_[] = { 2, 10, 20 }; + vector timers(timers_, timers_+sizeof(timers_)/sizeof(*timers_)); + int maxInterval = 30; + int maxTime = 40; + int RetVal_[] = {1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21, 23, 25, 27, 29 }; + vector RetVal(RetVal_, RetVal_+sizeof(RetVal_)/sizeof(*RetVal_)); + return verify_case(RetVal, SandTimers().badIntervals(timers, maxInterval, maxTime)); } +int Test_(Case_<2>) { + int timers_[] = { 2, 5, 9 }; + vector timers(timers_, timers_+sizeof(timers_)/sizeof(*timers_)); + int maxInterval = 360; + int maxTime = 360; + vector RetVal; + return verify_case(RetVal, SandTimers().badIntervals(timers, maxInterval, maxTime)); } +int Test_(Case_<3>) { + int timers_[] = { 4 }; + vector timers(timers_, timers_+sizeof(timers_)/sizeof(*timers_)); + int maxInterval = 23; + int maxTime = 47; + int RetVal_[] = {1, 2, 3, 5, 6, 7, 9, 10, 11, 13, 14, 15, 17, 18, 19, 21, 22, 23 }; + vector RetVal(RetVal_, RetVal_+sizeof(RetVal_)/sizeof(*RetVal_)); + return verify_case(RetVal, SandTimers().badIntervals(timers, maxInterval, maxTime)); } +int Test_(Case_<4>) { + int timers_[] = { 20, 13 }; + vector timers(timers_, timers_+sizeof(timers_)/sizeof(*timers_)); + int maxInterval = 30; + int maxTime = 30; + int RetVal_[] = {1, 2, 3, 4, 5, 8, 9, 10, 11, 12, 15, 16, 17, 18, 19, 21, 22, 23, 24, 25, 28, 29, 30 }; + vector RetVal(RetVal_, RetVal_+sizeof(RetVal_)/sizeof(*RetVal_)); + return verify_case(RetVal, SandTimers().badIntervals(timers, maxInterval, maxTime)); } +int Test_(Case_<5>) { + int timers_[] = { 20, 17, 13 }; + vector timers(timers_, timers_+sizeof(timers_)/sizeof(*timers_)); + int maxInterval = 25; + int maxTime = 30; + int RetVal_[] = {18, 19 }; + vector RetVal(RetVal_, RetVal_+sizeof(RetVal_)/sizeof(*RetVal_)); + return verify_case(RetVal, SandTimers().badIntervals(timers, maxInterval, maxTime)); } +int Test_(Case_<6>) { + int timers_[] = { 20, 20, 19 }; + vector timers(timers_, timers_+sizeof(timers_)/sizeof(*timers_)); + int maxInterval = 360; + int maxTime = 360; + vector RetVal; + return verify_case(RetVal, SandTimers().badIntervals(timers, maxInterval, maxTime)); } + +template void Run_() { cerr << "Test Case #" << N << "..." << flush; Test_(Case_()); Run_()))==1 ? -1 : N+1>(); } +template<> void Run_<-1>() {} +int main() { Run_<0>(); } +// END CUT HERE + ADDED SRM/264/1A.cpp Index: SRM/264/1A.cpp ================================================================== --- SRM/264/1A.cpp +++ SRM/264/1A.cpp @@ -0,0 +1,57 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +using namespace std; +typedef long long LL; + +class DivisibilityRules +{ +public: + int similar(int numerationBase, int divisor) + { + vector< vector > mb; + for(int k=2; k mbase(int n, int k) + { + vector r; + int b = 1; + for(int i=0; i string print_array(const vector &V) { ostringstream os; os << "{ "; for (typename vector::const_iterator iter = V.begin(); iter != V.end(); ++iter) os << '\"' << *iter << "\","; os << " }"; return os.str(); } + void verify_case(int Case, const int &Expected, const int &Received) { cerr << "Test Case #" << Case << "..."; if (Expected == Received) cerr << "PASSED" << endl; else { cerr << "FAILED" << endl; cerr << "\tExpected: \"" << Expected << '\"' << endl; cerr << "\tReceived: \"" << Received << '\"' << endl; } } + void test_case_0() { int Arg0 = 10; int Arg1 = 3; int Arg2 = 2; verify_case(0, Arg2, similar(Arg0, Arg1)); } + void test_case_1() { int Arg0 = 10; int Arg1 = 5; int Arg2 = 2; verify_case(1, Arg2, similar(Arg0, Arg1)); } + void test_case_2() { int Arg0 = 511; int Arg1 = 32; int Arg2 = 10; verify_case(2, Arg2, similar(Arg0, Arg1)); } + void test_case_3() { int Arg0 = 3; int Arg1 = 2; int Arg2 = 1; verify_case(3, Arg2, similar(Arg0, Arg1)); } + void test_case_4() { int Arg0 = 1000; int Arg1 = 999; int Arg2 = 7; verify_case(4, Arg2, similar(Arg0, Arg1)); } + void test_case_5() { int Arg0 = 655; int Arg1 = 532; int Arg2 = 1; verify_case(5, Arg2, similar(Arg0, Arg1)); } + +// END CUT HERE +}; +// BEGIN CUT HERE +int main() { DivisibilityRules().run_test(-1); } +// END CUT HERE ADDED SRM/264/1B.cpp Index: SRM/264/1B.cpp ================================================================== --- SRM/264/1B.cpp +++ SRM/264/1B.cpp @@ -0,0 +1,140 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +using namespace std; +typedef long long LL; + +int gcd(int x, int y) +{ + while(y) { + int t = x; + x = y; + y = t%y; + } + return x; +} + +class GradingGridIns +{ +public: + vector score(vector answer, vector lower, vector upper) + { + int lg = gcd(lower[0], lower[1]); + double la=lower[0]/lg, lb=lower[1]/lg; + int ug = gcd(upper[0], upper[1]); + double ua=upper[0]/ug, ub=upper[1]/ug; + + vector world; + + char c[] = " ./0123456789"; + for(int i=0; i::iterator lt = lower_bound( world.begin(), world.end(), la/lb ); + vector::iterator ut = upper_bound( world.begin(), world.end(), ua/ub ); + if( lt!=world.begin() ) --lt; + + vector result(answer.size()); + for(int i=0; i0 && ans[ii-1]==' ') --ii; + ans = ans.substr(0, ii); + + + int dot=-1, sla=-1, numdig=0; + int value = 0, value_pre = 0; + for(int i=0; i=0 || sla>=0) + throw "MALFORMED"; + dot = i; + value_pre = value; + value = 0; + } + else if( ans[i]=='/' ) + { + if(dot>=0 || sla>=0 || numdig==0 ) + throw "MALFORMED"; + sla = i; + value_pre = value; + value = 0; + } + else if( '0'<=ans[i] && ans[i]<='9' ) + { + ++numdig; + value = value*10 + ans[i]-'0'; + } + else + throw "MALFORMED"; + if(numdig==0) + throw "MALFORMED"; + + int L = value_pre, R = value; + if( sla >= 0 ) { + if( R == 0 ) + throw "MALFORMED"; + int g = gcd(L,R); L/=g, R/=g; + return L/double(R); + } + else if( dot >= 0 ) { + return L + R / pow(10.0, double(ans.size()-dot-1)); + } + else { + return R; + } + } + + +// BEGIN CUT HERE + public: + void run_test(int Case) { if ((Case == -1) || (Case == 0)) test_case_0(); if ((Case == -1) || (Case == 1)) test_case_1(); if ((Case == -1) || (Case == 2)) test_case_2(); if ((Case == -1) || (Case == 3)) test_case_3(); if ((Case == -1) || (Case == 4)) test_case_4(); } + private: + template string print_array(const vector &V) { ostringstream os; os << "{ "; for (typename vector::const_iterator iter = V.begin(); iter != V.end(); ++iter) os << '\"' << *iter << "\","; os << " }"; return os.str(); } + void verify_case(int Case, const vector &Expected, const vector &Received) { cerr << "Test Case #" << Case << "..."; if (Expected == Received) cerr << "PASSED" << endl; else { cerr << "FAILED" << endl; cerr << "\tExpected: " << print_array(Expected) << endl; cerr << "\tReceived: " << print_array(Received) << endl; } } + void test_case_0() { string Arr0[] = {"4/7 "," 4/7","4/07","8/14",".571",".572"}; vector Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); int Arr1[] = {4,7}; vector Arg1(Arr1, Arr1 + (sizeof(Arr1) / sizeof(Arr1[0]))); int Arr2[] = {4,7}; vector Arg2(Arr2, Arr2 + (sizeof(Arr2) / sizeof(Arr2[0]))); string Arr3[] = {"CORRECT", "CORRECT", "CORRECT", "CORRECT", "CORRECT", "CORRECT" }; vector Arg3(Arr3, Arr3 + (sizeof(Arr3) / sizeof(Arr3[0]))); verify_case(0, Arg3, score(Arg0, Arg1, Arg2)); } + void test_case_1() { string Arr0[] = {" 4/7","1.01","1.02"," 000"}; vector Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); int Arr1[] = {0,1}; vector Arg1(Arr1, Arr1 + (sizeof(Arr1) / sizeof(Arr1[0]))); int Arr2[] = {1,1}; vector Arg2(Arr2, Arr2 + (sizeof(Arr2) / sizeof(Arr2[0]))); string Arr3[] = {"CORRECT", "CORRECT", "INCORRECT", "CORRECT" }; vector Arg3(Arr3, Arr3 + (sizeof(Arr3) / sizeof(Arr3[0]))); verify_case(1, Arg3, score(Arg0, Arg1, Arg2)); } + void test_case_2() { string Arr0[] = {"1.15","1 14","1.14"," 8/7"}; vector Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); int Arr1[] = {1142,1000}; vector Arg1(Arr1, Arr1 + (sizeof(Arr1) / sizeof(Arr1[0]))); int Arr2[] = {1142,1000}; vector Arg2(Arr2, Arr2 + (sizeof(Arr2) / sizeof(Arr2[0]))); string Arr3[] = {"INCORRECT", "MALFORMED", "CORRECT", "CORRECT" }; vector Arg3(Arr3, Arr3 + (sizeof(Arr3) / sizeof(Arr3[0]))); verify_case(2, Arg3, score(Arg0, Arg1, Arg2)); } + void test_case_3() { string Arr0[] = {" ","...."," . ","1 23","8//5","9.4.","85/ ","/123","123/"}; vector Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); int Arr1[] = {1,1}; vector Arg1(Arr1, Arr1 + (sizeof(Arr1) / sizeof(Arr1[0]))); int Arr2[] = {1,1}; vector Arg2(Arr2, Arr2 + (sizeof(Arr2) / sizeof(Arr2[0]))); string Arr3[] = {"MALFORMED", "MALFORMED", "MALFORMED", "MALFORMED", "MALFORMED", "MALFORMED", "MALFORMED", "MALFORMED", "MALFORMED" }; vector Arg3(Arr3, Arr3 + (sizeof(Arr3) / sizeof(Arr3[0]))); verify_case(3, Arg3, score(Arg0, Arg1, Arg2)); } + void test_case_4() { string Arr0[] = {"1/0 "}; vector Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); int Arr1[] = {0,1}; vector Arg1(Arr1, Arr1 + (sizeof(Arr1) / sizeof(Arr1[0]))); int Arr2[] = {9999,1}; vector Arg2(Arr2, Arr2 + (sizeof(Arr2) / sizeof(Arr2[0]))); string Arr3[] = {"MALFORMED" }; vector Arg3(Arr3, Arr3 + (sizeof(Arr3) / sizeof(Arr3[0]))); verify_case(4, Arg3, score(Arg0, Arg1, Arg2)); } + +// END CUT HERE +}; +// BEGIN CUT HERE +int main() { GradingGridIns().run_test(-1); } +// END CUT HERE ADDED SRM/264/1C.cpp Index: SRM/264/1C.cpp ================================================================== --- SRM/264/1C.cpp +++ SRM/264/1C.cpp @@ -0,0 +1,65 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +using namespace std; +typedef long long LL; + +class PolygonDecomposition +{ +public: + int howMany(int n, int k) + { + if( n-2 < k ) + return -1; + + LL dp[101][101] = {}; + dp[2][0] = 1; + for(int N=3; N<=n; ++N) + dp[N][1] = 1; + + for(int N=4; N<=n; ++N) + for(int K=2; K<=k; ++K) + { + LL sum = 0; + for(LL v=3; v<=N-1; ++v) // use 1--v and don't use 1--3, ..., 1--(v-1) + for(int x=1; x<=K-1; ++x) // left: x pieces, right: K-x pieces + sum += (dp[v-1][x-1]+dp[v-1][x]) * dp[N-v+2][K-x] % 1000000000; + + // don't use 1--*; if use 2--N, we'll have dp[N-1][K-1] cuts + // otherwise, will have dp[N-1][K] because 2--1--N + // can be regarded as a single edge + sum += dp[N-1][K-1] + dp[N-1][K]; + dp[N][K] = sum % 1000000000; + } + return (int) dp[n][k]; + } + + +// BEGIN CUT HERE + public: + void run_test(int Case) { if ((Case == -1) || (Case == 0)) test_case_0(); if ((Case == -1) || (Case == 1)) test_case_1(); if ((Case == -1) || (Case == 2)) test_case_2(); if ((Case == -1) || (Case == 3)) test_case_3(); if ((Case == -1) || (Case == 4)) test_case_4(); } + private: + template string print_array(const vector &V) { ostringstream os; os << "{ "; for (typename vector::const_iterator iter = V.begin(); iter != V.end(); ++iter) os << '\"' << *iter << "\","; os << " }"; return os.str(); } + void verify_case(int Case, const int &Expected, const int &Received) { cerr << "Test Case #" << Case << "..."; if (Expected == Received) cerr << "PASSED" << endl; else { cerr << "FAILED" << endl; cerr << "\tExpected: \"" << Expected << '\"' << endl; cerr << "\tReceived: \"" << Received << '\"' << endl; } } + void test_case_0() { int Arg0 = 4; int Arg1 = 2; int Arg2 = 2; verify_case(0, Arg2, howMany(Arg0, Arg1)); } + void test_case_1() { int Arg0 = 100; int Arg1 = 1; int Arg2 = 1; verify_case(1, Arg2, howMany(Arg0, Arg1)); } + void test_case_2() { int Arg0 = 6; int Arg1 = 4; int Arg2 = 14; verify_case(2, Arg2, howMany(Arg0, Arg1)); } + void test_case_3() { int Arg0 = 31; int Arg1 = 20; int Arg2 = 956146480; verify_case(3, Arg2, howMany(Arg0, Arg1)); } + void test_case_4() { int Arg0 = 3; int Arg1 = 4; int Arg2 = -1; verify_case(4, Arg2, howMany(Arg0, Arg1)); } + +// END CUT HERE +}; +// BEGIN CUT HERE +int main() { PolygonDecomposition().run_test(-1); } +// END CUT HERE ADDED SRM/282/1A.cpp Index: SRM/282/1A.cpp ================================================================== --- SRM/282/1A.cpp +++ SRM/282/1A.cpp @@ -0,0 +1,237 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +using namespace std; +typedef long long LL; +typedef complex CMP; + +class TileCutting { +public: + int cuts(vector layout) + { + int san=0, ni=0, ic=0; + for(int y=0; y=san) + mincut = min(mincut, calc(san,ni,ic,nk)); + return mincut; + } + + int calc(int san, int ni, int ic, int nk) { + // create nk pieces of L-shaped block + // reuse nk pieces of single-block to form the rest of neede L-blocks + int cut = nk*2; + int piece = nk-(san-nk)*3; // single-block piecese left + + // form single-blocks + int pi = min(ic, piece); + ic -= pi; + piece -= pi; + + // if still left, use single-blocks to form I-shaped blocks + if( piece ) + { + ni -= piece/2; + piece -= piece/2*2; + } + + // create I-shaped blocks + int xpiece = 0; + if(ni) { + cut += (ni+1)/2*2; + xpiece = ni%2 ? 1 : 0; + } + + // create single-blocks + pi = min(ic, piece); + ic -= pi; + piece -= pi; + if( ic ) { + if( xpiece ) ic-=2, cut+=1; + if(ic>0) + cut += ic/4*4 + ic%4 + (ic%4==0 ? 0 : 1); + } + return cut; + } +}; + +// 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 string print_array(const vector &V) { ostringstream os; os << "{ "; for (typename vector::const_iterator iter = V.begin(); iter != V.end(); ++iter) os << '\"' << *iter << "\","; os << " }"; return os.str(); } +int verify_case(const int &Expected, const int &Received) { if (Expected == Received) cerr << "PASSED" << timer() << endl; else { cerr << "FAILED" << timer() << endl; cerr << "\tExpected: \"" << Expected << '\"' << endl; cerr << "\tReceived: \"" << Received << '\"' << endl; } return 0;} + +template struct Case_ { Case_(){start_time=clock();} }; +char Test_(...); +int Test_(Case_<0>) { + string layout_[] = { "..", + ".." }; + vector layout(layout_, layout_+sizeof(layout_)/sizeof(*layout_)); + int RetVal = 0; + return verify_case(RetVal, TileCutting().cuts(layout)); } +int Test_(Case_<1>) { + string layout_[] = { "x.", + ".." }; + vector layout(layout_, layout_+sizeof(layout_)/sizeof(*layout_)); + int RetVal = 2; + return verify_case(RetVal, TileCutting().cuts(layout)); } +int Test_(Case_<2>) { + string layout_[] = { ".x", + "xx" }; + vector layout(layout_, layout_+sizeof(layout_)/sizeof(*layout_)); + int RetVal = 2; + return verify_case(RetVal, TileCutting().cuts(layout)); } +int Test_(Case_<3>) { + string layout_[] = { "xxxx..xxxx", + "x..x..xx..", + "x..xxxxx..", + "xxxxxxxxxx" }; + vector layout(layout_, layout_+sizeof(layout_)/sizeof(*layout_)); + int RetVal = 6; + return verify_case(RetVal, TileCutting().cuts(layout)); } +int Test_(Case_<4>) { + string layout_[] = { "xxxxxx", + "x....x" }; + vector layout(layout_, layout_+sizeof(layout_)/sizeof(*layout_)); + int RetVal = 3; + return verify_case(RetVal, TileCutting().cuts(layout)); } +int Test_(Case_<5>) { + string layout_[] = { "x..x....", + "x..xx...", + "..xx....", + "...x....", + "......xx", + "......xx" }; + vector layout(layout_, layout_+sizeof(layout_)/sizeof(*layout_)); + int RetVal = 4; + return verify_case(RetVal, TileCutting().cuts(layout)); } +int Test_(Case_<6>) { + string layout_[] = { "x..xx.x..x.xx..x.xx.", + "..x..x..x.x..xx...x.", + ".xx...x...x...x..x..", + ".xx...x.x.x...x..xx.", + ".x..x...x.....x...x.", + ".x.x.x..x..x..x..x.x", + "...x.x.x.x.x.x.x...x", + ".x..x..x...x..x...x.", + "...x.x.x..x.x.x.....", + "...x.x.x..x.x.xxx.x.", + "xx.xx.xx.x.x.x.x..x.", + ".x..xxx...x.xx...x.x", + "xx..x.x...x.x.x.x..x", + ".xx..x.xx.xxxxx...xx", + "x....x.x...x...x.x..", + ".x.xx.x..x.x.xxx.x..", + "...xx.xxx.....xx.xxx", + ".xx..x..xx.x...x.xx.", + "x.x...x.x.xx.x..x.xx", + ".....xx.x.......xx.x", + "x...x.xx.x..x....x..", + ".x..xxx.....x.x.x.xx" } +; + vector layout(layout_, layout_+sizeof(layout_)/sizeof(*layout_)); + int RetVal = 121; + return verify_case(RetVal, TileCutting().cuts(layout)); } +int Test_(Case_<7>) { + string layout_[] = { "..................................................", + "..................................................", + "..................................................", + "..................................................", + "..................................................", + "..................................................", + "..................................................", + "..................................................", + "..................................................", + "..................................................", + "..................................................", + "..................................................", + "..................................................", + "..................................................", + "..................................................", + "..................................................", + "..................................................", + "..................................................", + "..................................................", + "..................................................", + "..................................................", + "..................................................", + "..................................................", + "..................................................", + "..................................................", + "..................................................", + "..................................................", + "..................................................", + "..................................................", + "..................................................", + "..................................................", + "..................................................", + "..................................................", + "..................................................", + "..................................................", + "..................................................", + "..................................................", + "..................................................", + "..................................................", + "..................................................", + "..................................................", + "..................................................", + "..................................................", + "..................................................", + "..................................................", + "..................................................", + "..................................................", + "..................................................", + "..................................................", + ".................................................." } +; + vector layout(layout_, layout_+sizeof(layout_)/sizeof(*layout_)); + int RetVal = 0; + return verify_case(RetVal, TileCutting().cuts(layout)); } +int Test_(Case_<8>) { + string layout_[] = +{"x..x...x...x..x....x", + ".x..x.xxx....xx..x..", + "..x...x..x...xxx....", + "x....x.....xx..xx...", + "..x.x...x..x........", + ".x...xx.xx...xxx...x", + "x..x.x......xx.x....", + "...xxx.....x.x..xx.."} +; + vector layout(layout_, layout_+sizeof(layout_)/sizeof(*layout_)); + int RetVal = 44; + return verify_case(RetVal, TileCutting().cuts(layout)); } + +template void Run_() { cerr << "Test Case #" << N << "..." << flush; Test_(Case_()); Run_()))==1 ? -1 : N+1>(); } +template<> void Run_<-1>() {} +int main() { Run_<0>(); } +// END CUT HERE + ADDED SRM/282/1B.cpp Index: SRM/282/1B.cpp ================================================================== --- SRM/282/1B.cpp +++ SRM/282/1B.cpp @@ -0,0 +1,146 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +using namespace std; +typedef long long LL; +typedef complex CMP; + +int gcd(int a, int b) { + while(a) swap(a,b%=a); return b; +} + +class Fractionalization { +public: + vector partial(vector definitions, int m, int n, string danceCall) + { + // input + map< string, vector > D; + for(int i=0; i>name; name=name.substr(0, name.size()-1); + vector ds; + for(string d; sin>>d;) + ds.push_back(d); + D[name] = ds; + } + + // solve + vector result; + while(m) { + if( !D.count(danceCall) ) + return vector(1, "ILLEGAL"); + vector& d = D[danceCall]; + int q = d.size(); + int z = m*q/n; + for(int i=0; i +double start_time;string timer() { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) << " msec)"; return os.str(); } + +template string print_array(const vector &V) { ostringstream os; os << "{ "; for (typename vector::const_iterator iter = V.begin(); iter != V.end(); ++iter) os << '\"' << *iter << "\","; os << " }"; return os.str(); } +int verify_case(const vector &Expected, const vector &Received) { if (Expected == Received) cerr << "PASSED" << timer() << endl; else { cerr << "FAILED" << timer() << endl; cerr << "\tExpected: " << print_array(Expected) << endl; cerr << "\tReceived: " << print_array(Received) << endl; } return 0;} + +template struct Case_ { Case_(){start_time=clock();} }; +char Test_(...); +int Test_(Case_<0>) { + string definitions_[] = { "FiveParts: One Two Three Four Five" }; + vector definitions(definitions_, definitions_+sizeof(definitions_)/sizeof(*definitions_)); + int m = 2; + int n = 5; + string danceCall = "FiveParts"; + string RetVal_[] = {"One", "Two" }; + vector RetVal(RetVal_, RetVal_+sizeof(RetVal_)/sizeof(*RetVal_)); + return verify_case(RetVal, Fractionalization().partial(definitions, m, n, danceCall)); } +int Test_(Case_<1>) { + string definitions_[] = { "Trade: Hinge Hinge", + "TradeThreeTimes: Trade Trade Trade" }; + vector definitions(definitions_, definitions_+sizeof(definitions_)/sizeof(*definitions_)); + int m = 5; + int n = 6; + string danceCall = "TradeThreeTimes"; + string RetVal_[] = {"Trade", "Trade", "Hinge" }; + vector RetVal(RetVal_, RetVal_+sizeof(RetVal_)/sizeof(*RetVal_)); + return verify_case(RetVal, Fractionalization().partial(definitions, m, n, danceCall)); } +int Test_(Case_<2>) { + string definitions_[] = { "Trade: Hinge Hinge", + "CenterTwoTrade: CenterTwoHinge CenterTwoHinge", + "SpinTheTop: Trade FanTheTop", + "HotFootSpin: FanTheTop CenterTwoTrade SpinTheTop" }; + vector definitions(definitions_, definitions_+sizeof(definitions_)/sizeof(*definitions_)); + int m = 1; + int n = 2; + string danceCall = "HotFootSpin"; + string RetVal_[] = {"FanTheTop", "CenterTwoHinge" }; + vector RetVal(RetVal_, RetVal_+sizeof(RetVal_)/sizeof(*RetVal_)); + return verify_case(RetVal, Fractionalization().partial(definitions, m, n, danceCall)); } +int Test_(Case_<3>) { + string definitions_[] = { "Trade: Hinge Hinge", + "CenterTwoTrade: CenterTwoHinge CenterTwoHinge", + "SpinTheTop: Trade FanTheTop", + "HotFootSpin: FanTheTop CenterTwoTrade SpinTheTop" }; + vector definitions(definitions_, definitions_+sizeof(definitions_)/sizeof(*definitions_)); + int m = 3; + int n = 4; + string danceCall = "HotFootSpin"; + string RetVal_[] = {"FanTheTop", "CenterTwoTrade", "Hinge" }; + vector RetVal(RetVal_, RetVal_+sizeof(RetVal_)/sizeof(*RetVal_)); + return verify_case(RetVal, Fractionalization().partial(definitions, m, n, danceCall)); } +int Test_(Case_<4>) { + string definitions_[] = { "A: One Two", + "B: One Two Three", + "C: One Two Three Four" }; + vector definitions(definitions_, definitions_+sizeof(definitions_)/sizeof(*definitions_)); + int m = 3; + int n = 4; + string danceCall = "C"; + string RetVal_[] = {"One", "Two", "Three" }; + vector RetVal(RetVal_, RetVal_+sizeof(RetVal_)/sizeof(*RetVal_)); + return verify_case(RetVal, Fractionalization().partial(definitions, m, n, danceCall)); } +int Test_(Case_<5>) { + string definitions_[] = { "A: X Y Z", + "B: A A A" }; + vector definitions(definitions_, definitions_+sizeof(definitions_)/sizeof(*definitions_)); + int m = 7; + int n = 23; + string danceCall = "B"; + string RetVal_[] = {"ILLEGAL" }; + vector RetVal(RetVal_, RetVal_+sizeof(RetVal_)/sizeof(*RetVal_)); + return verify_case(RetVal, Fractionalization().partial(definitions, m, n, danceCall)); } +int Test_(Case_<6>) { + string definitions_[] = { "A: B C", + "D: E F" }; + vector definitions(definitions_, definitions_+sizeof(definitions_)/sizeof(*definitions_)); + int m = 1; + int n = 2; + string danceCall = "G"; + string RetVal_[] = {"ILLEGAL" }; + vector RetVal(RetVal_, RetVal_+sizeof(RetVal_)/sizeof(*RetVal_)); + return verify_case(RetVal, Fractionalization().partial(definitions, m, n, danceCall)); } + +template void Run_() { cerr << "Test Case #" << N << "..." << flush; Test_(Case_()); Run_()))==1 ? -1 : N+1>(); } +template<> void Run_<-1>() {} +int main() { Run_<0>(); } +// END CUT HERE + ADDED SRM/282/1C.cpp Index: SRM/282/1C.cpp ================================================================== --- SRM/282/1C.cpp +++ SRM/282/1C.cpp @@ -0,0 +1,138 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +using namespace std; +typedef long long LL; +typedef complex CMP; + +class GreatExpectations { +public: + double expectedTime(vector doors) + { + const int Y = doors.size()/2 +1; + const int X = doors[0].size() +1; + + vector< vector > e(Y, vector(X, -1.0)); + e[Y-1][X-1] = 0.0; + + for(int k=0; k > ep; + if( x+1=0 ) + ep.push_back( make_pair(e[y][x+1], (doors[y*2][x]-'0')/8.0) ); + if( 0=0 ) + ep.push_back( make_pair(e[y][x-1], (doors[y*2][x-1]-'0')/8.0) ); + if( y+1=0 ) + ep.push_back( make_pair(e[y+1][x], (doors[y*2+1][x]-'0')/8.0) ); + if( 0=0 ) + ep.push_back( make_pair(e[y-1][x], (doors[y*2-1][x]-'0')/8.0) ); + + if( ep.empty() ) continue; + sort( ep.begin(), ep.end() ); // better first + + // choose k that minimizes the following E: + // E = (1+e[0])*p[0] + // + (1+e[1])*(1-p[0])*p[1] + // + (1+e[2])*(1-p[0])*(1-p[1])*p[2] + // + ... + // + (1+E) *(1-p[0])*(1-p[1])*...*(1-p[k]) + double r=1.0, s=0.0, E=1e+50; + for(int i=0; i E ) break; + r=r2, s=s2, E=E2; + } + if( e[y][x]<0 || e[y][x]>E ) + e[y][x]=E; + } + + return e[0][0]; + } +}; + +// 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 string print_array(const vector &V) { ostringstream os; os << "{ "; for (typename vector::const_iterator iter = V.begin(); iter != V.end(); ++iter) os << '\"' << *iter << "\","; os << " }"; return os.str(); } +int verify_case(const double &Expected, const double &Received) { double diff = Expected - Received; if (diff < 0) diff = -diff; if (diff < 1e-9) cerr << "PASSED" << timer() << endl; else { cerr << "FAILED" << timer() << endl; cerr << "\tExpected: \"" << Expected << '\"' << endl; cerr << "\tReceived: \"" << Received << '\"' << endl; } return 0;} + +template struct Case_ { Case_(){start_time=clock();} }; +char Test_(...); +int Test_(Case_<0>) { + string doors_[] = { "4", + "44", + "4" }; + vector doors(doors_, doors_+sizeof(doors_)/sizeof(*doors_)); + double RetVal = 3.3333333333333335; + return verify_case(RetVal, GreatExpectations().expectedTime(doors)); } +int Test_(Case_<1>) { + string doors_[] = { "48", + "440", + "42", + "862", + "06" }; + vector doors(doors_, doors_+sizeof(doors_)/sizeof(*doors_)); + double RetVal = 6.0; + return verify_case(RetVal, GreatExpectations().expectedTime(doors)); } +int Test_(Case_<2>) { + string doors_[] = { "0808", + "88888", + "0000", + "88888", + "8080" } +; + vector doors(doors_, doors_+sizeof(doors_)/sizeof(*doors_)); + double RetVal = 14.0; + return verify_case(RetVal, GreatExpectations().expectedTime(doors)); } +int Test_(Case_<3>) { + string doors_[] = { "2815", + "67686", + "1324", + "75767", + "6051" } +; + vector doors(doors_, doors_+sizeof(doors_)/sizeof(*doors_)); + double RetVal = 11.852782338798935; + return verify_case(RetVal, GreatExpectations().expectedTime(doors)); } +int Test_(Case_<4>) { + string doors_[] = { "823", + "2630", + "130" } +; + vector doors(doors_, doors_+sizeof(doors_)/sizeof(*doors_)); + double RetVal = -1.0; + return verify_case(RetVal, GreatExpectations().expectedTime(doors)); } +int Test_(Case_<5>) { + string doors_[] = { "0", + "44", + "4" } +; + vector doors(doors_, doors_+sizeof(doors_)/sizeof(*doors_)); + double RetVal = 4.0; + return verify_case(RetVal, GreatExpectations().expectedTime(doors)); } + +template void Run_() { cerr << "Test Case #" << N << "..." << flush; Test_(Case_()); Run_()))==1 ? -1 : N+1>(); } +template<> void Run_<-1>() {} +int main() { Run_<0>(); } +// END CUT HERE + ADDED SRM/330/1A.cpp Index: SRM/330/1A.cpp ================================================================== --- SRM/330/1A.cpp +++ SRM/330/1A.cpp @@ -0,0 +1,77 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +using namespace std; +typedef long long LL; +typedef complex CMP; + +class Arrows { public: + int longestArrow(string s) + { + string rs = s; + reverse(rs.begin(), rs.end()); + return max(max(la(s,'<','-'), la(s,'<','=')), max(la(rs,'>','-'), la(rs,'>','='))); + } + int la(string s, char h, char t) + { + int m = -1; + for(int i=0; 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(_, Arrows().longestArrow(s));} +int main(){ + +CASE(0) + string s = "<--->--==>"; + int _ = 4; +END +CASE(1) + string s = "<<<<<<<<<<"; + int _ = 1; +END +CASE(2) + string s = "----==-"; + int _ = -1; +END +CASE(3) + string s = "<----=====>"; + int _ = 6; +END + +} +// END CUT HERE ADDED SRM/330/1B.cpp Index: SRM/330/1B.cpp ================================================================== --- SRM/330/1B.cpp +++ SRM/330/1B.cpp @@ -0,0 +1,105 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +using namespace std; +typedef long long LL; +typedef complex CMP; + +struct byLength { + bool operator()(const string& lhs, const string& rhs) const { + if(lhs.size() != rhs.size()) return lhs.size() < rhs.size(); + return lhs < rhs; + } +}; +class PrefixFreeSubsets { public: + long long cantPrefFreeSubsets(vector words) + { + int N = words.size(); + sort(words.begin(), words.end(), byLength()); + + vector< vector > dc; + for(int i=0; i >& dc ) + { + LL p = 1; + for(int i=0; i direct_children(const string& s, vector& w, int i) { + vector ans; + for(; 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 long long& Expected, const long long& 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(_, PrefixFreeSubsets().cantPrefFreeSubsets(words));} +int main(){ + +CASE(0) + string words_[] = {"hello","hell","hi"}; + vector words(words_, words_+sizeof(words_)/sizeof(*words_)); + long long _ = 6LL; +END +CASE(1) + string words_[] = {"a","b","c","d"}; + vector words(words_, words_+sizeof(words_)/sizeof(*words_)); + long long _ = 16LL; +END +CASE(2) + string words_[] = {"a","ab","abc","abcd","abcde","abcdef"}; + vector words(words_, words_+sizeof(words_)/sizeof(*words_)); + long long _ = 7LL; +END +CASE(3) + string words_[] = {"a","b","aa","ab","ba","bb"}; + vector words(words_, words_+sizeof(words_)/sizeof(*words_)); + long long _ = 25LL; +END + +} +// END CUT HERE ADDED SRM/330/1C.cpp Index: SRM/330/1C.cpp ================================================================== --- SRM/330/1C.cpp +++ SRM/330/1C.cpp @@ -0,0 +1,121 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +using namespace std; +typedef long long LL; +typedef complex CMP; + +class LongLongNim { public: + int numberOfWins(int maxN, vector moves) + { + int FULL = (1 << *max_element(moves.begin(), moves.end())) - 1; + int MASK = 0; + for(int i=0; i ss; + vector visited(FULL+1, -1); + for(int i=1,s=1; i<=maxN; ++i) + { + s = (s<<1 | ((~s&MASK)==MASK)) & FULL; + if( visited[s] >= 0 ) + return countBits(ss, visited[s], ss.size(), maxN); + else + { + visited[s] = ss.size(); + ss.push_back(s&1); + } + } + return countBits(ss, 0, maxN, maxN); + } + + int countBits(const vector& b, int lb, int le, int N) + { + // Given an infinite seq b[0] ... b[lb-1] (b[lb] ... b[le-1])*, + // compute \Sigma b[0..N) + int sum = 0; + for(int i=0; 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(_, LongLongNim().numberOfWins(maxN, moves));} +int main(){ + +CASE(0) + int maxN = 20; + int moves_[] = {1,2,3}; + vector moves(moves_, moves_+sizeof(moves_)/sizeof(*moves_)); + int _ = 5; +END +CASE(1) + int maxN = 999; + int moves_[] = {1}; + vector moves(moves_, moves_+sizeof(moves_)/sizeof(*moves_)); + int _ = 499; +END +CASE(3) + int maxN = 6543; + int moves_[] = {2,4,7,11,20}; + vector moves(moves_, moves_+sizeof(moves_)/sizeof(*moves_)); + int _ = 1637; +END +CASE(2) + int maxN = 1000000000; + int moves_[] = {1,2}; + vector moves(moves_, moves_+sizeof(moves_)/sizeof(*moves_)); + int _ = 333333333; +END +/* +CASE(4) + int maxN = ; + int moves_[] = ; + vector moves(moves_, moves_+sizeof(moves_)/sizeof(*moves_)); + int _ = ; +END +CASE(5) + int maxN = ; + int moves_[] = ; + vector moves(moves_, moves_+sizeof(moves_)/sizeof(*moves_)); + int _ = ; +END +*/ +} +// END CUT HERE ADDED SRM/330/2C.cpp Index: SRM/330/2C.cpp ================================================================== --- SRM/330/2C.cpp +++ SRM/330/2C.cpp @@ -0,0 +1,96 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +using namespace std; +typedef long long LL; +typedef complex CMP; + +class NextPalindromicNumber { public: + string getNext(string n) + { + string s = mirror(n); + if( n < s ) + return s; + return mirror(plus1(s, (s.size()-1)/2)); + } + + string mirror(const string& s) + { + return string(s.begin(), s.begin()+s.size()/2) + string(s.rbegin()+s.size()/2, s.rend()); + } + + string plus1(string s, int i) + { + while( s[i] == '9' ) + { + s[i--] = '0'; + if( i < 0 ) + return '1'+s; + } + s[i]++; + return s; + } +}; + +// 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(_, NextPalindromicNumber().getNext(n));} +int main(){ + +CASE(0) + string n = "12345"; + string _ = "12421"; +END +CASE(1) + string n = "858"; + string _ = "868"; +END +CASE(2) + string n = "1999"; + string _ = "2002"; +END +CASE(3) + string n = "1"; + string _ = "2"; +END +CASE(4) + string n = "9999"; + string _ = "10001"; +END +/* +CASE(5) + string n = ; + string _ = ; +END +CASE(6) + string n = ; + string _ = ; +END +*/ +} +// END CUT HERE ADDED SRM/331/1A.cpp Index: SRM/331/1A.cpp ================================================================== --- SRM/331/1A.cpp +++ SRM/331/1A.cpp @@ -0,0 +1,83 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +using namespace std; +typedef long long LL; +typedef complex CMP; + +class CarolsSinging { +public: + int choose(vector lyrics) + { + int nP = lyrics.size(); + int nS = lyrics[0].size(); + vector people_who_know_the_song(nS); + for(int s=0; s +double start_time;string timer() { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) << " msec)"; return os.str(); } + +template string print_array(const vector &V) { ostringstream os; os << "{ "; for (typename vector::const_iterator iter = V.begin(); iter != V.end(); ++iter) os << '\"' << *iter << "\","; os << " }"; return os.str(); } +int verify_case(const int &Expected, const int &Received) { if (Expected == Received) cerr << "PASSED" << timer() << endl; else { cerr << "FAILED" << timer() << endl; cerr << "\tExpected: \"" << Expected << '\"' << endl; cerr << "\tReceived: \"" << Received << '\"' << endl; } return 0;} + +template struct Case_ { Case_(){start_time=clock();} }; +char Test_(...); +int Test_(Case_<0>) { + string lyrics_[] = {"YN","NY"}; + vector lyrics(lyrics_, lyrics_+sizeof(lyrics_)/sizeof(*lyrics_)); + int RetVal = 2; + return verify_case(RetVal, CarolsSinging().choose(lyrics)); } +int Test_(Case_<1>) { + string lyrics_[] = {"YN","YY","YN"}; + vector lyrics(lyrics_, lyrics_+sizeof(lyrics_)/sizeof(*lyrics_)); + int RetVal = 1; + return verify_case(RetVal, CarolsSinging().choose(lyrics)); } +int Test_(Case_<2>) { + string lyrics_[] = {"YNN","YNY","YNY","NYY","NYY","NYN"}; + vector lyrics(lyrics_, lyrics_+sizeof(lyrics_)/sizeof(*lyrics_)); + int RetVal = 2; + return verify_case(RetVal, CarolsSinging().choose(lyrics)); } +int Test_(Case_<3>) { + string lyrics_[] = {"YNNYYY","YYNYYY","YNNYYN","NYYNNN","YYYNNN","YYYNNY","NYYYYY","NYNYYY","NNNNYY", + "YYYYYY","YNNNNN","YYYYNY","YYNNNN","NNYYYN","NNNNYY","YYYNNN","NYNNYN","YNNYYN", + "YYNNNY","NYYNNY","NNYYYN","YNYYYN","NNNYNY","YYYYNN","YYNYNN","NYYNYY","YYNYYN"}; + vector lyrics(lyrics_, lyrics_+sizeof(lyrics_)/sizeof(*lyrics_)); + int RetVal = 4; + return verify_case(RetVal, CarolsSinging().choose(lyrics)); } + +template void Run_() { cerr << "Test Case #" << N << "..." << flush; Test_(Case_()); Run_()))==1 ? -1 : N+1>(); } +template<> void Run_<-1>() {} +int main() { Run_<0>(); } +// END CUT HERE + ADDED SRM/331/1B.cpp Index: SRM/331/1B.cpp ================================================================== --- SRM/331/1B.cpp +++ SRM/331/1B.cpp @@ -0,0 +1,80 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +using namespace std; +typedef long long LL; +typedef complex CMP; + +class Shopping { +public: + int minNumber(int X, vector values) + { + sort( values.begin(), values.end() ); + + int next=1, cnt=0; + while( next <= X ) + { + // maximum <= next; + vector::iterator it = upper_bound(values.begin(), values.end(), next); + if( it == values.begin() ) + return -1; + next += *(it-1); + ++cnt; + } + return cnt; + } +}; + +// 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 string print_array(const vector &V) { ostringstream os; os << "{ "; for (typename vector::const_iterator iter = V.begin(); iter != V.end(); ++iter) os << '\"' << *iter << "\","; os << " }"; return os.str(); } +int verify_case(const int &Expected, const int &Received) { if (Expected == Received) cerr << "PASSED" << timer() << endl; else { cerr << "FAILED" << timer() << endl; cerr << "\tExpected: \"" << Expected << '\"' << endl; cerr << "\tReceived: \"" << Received << '\"' << endl; } return 0;} + +template struct Case_ { Case_(){start_time=clock();} }; +char Test_(...); +int Test_(Case_<0>) { + int X = 20; + int values_[] = {1, 2, 5, 10}; + vector values(values_, values_+sizeof(values_)/sizeof(*values_)); + int RetVal = 5; + return verify_case(RetVal, Shopping().minNumber(X, values)); } +int Test_(Case_<1>) { + int X = 7; + int values_[] = {2, 4, 1, 7}; + vector values(values_, values_+sizeof(values_)/sizeof(*values_)); + int RetVal = 3; + return verify_case(RetVal, Shopping().minNumber(X, values)); } +int Test_(Case_<2>) { + int X = 20; + int values_[] = {2,4,6,8}; + vector values(values_, values_+sizeof(values_)/sizeof(*values_)); + int RetVal = -1; + return verify_case(RetVal, Shopping().minNumber(X, values)); } +int Test_(Case_<3>) { + int X = 600; + int values_[] = {1,2,3,10,11,30}; + vector values(values_, values_+sizeof(values_)/sizeof(*values_)); + int RetVal = 25; + return verify_case(RetVal, Shopping().minNumber(X, values)); } + +template void Run_() { cerr << "Test Case #" << N << "..." << flush; Test_(Case_()); Run_()))==1 ? -1 : N+1>(); } +template<> void Run_<-1>() {} +int main() { Run_<0>(); } +// END CUT HERE + ADDED SRM/331/1C.cpp Index: SRM/331/1C.cpp ================================================================== --- SRM/331/1C.cpp +++ SRM/331/1C.cpp @@ -0,0 +1,231 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +using namespace std; +typedef long long LL; +typedef complex CMP; + +typedef pair pt; +typedef pair range; + +class HiddenSquares { +public: + int count(vector x1, vector y1, vector x2, vector y2) + { + vector< pair > xseg, yseg; + for(int i=0; i > xray; + for(int i=0; i p; + + p.insert(xseg[i].second.first); + p.insert(xseg[i].second.second); + for(int j=0; j::iterator y=p.begin(); ++y!=p.end(); py=*y) + xray[xseg[i].first].push_back( range(py,*y) ); + } + + // paset! + map > yray; + for(int i=0; i p; + + p.insert(yseg[i].second.first); + p.insert(yseg[i].second.second); + for(int j=0; j::iterator x=p.begin(); ++x!=p.end(); px=*x) + yray[yseg[i].first].push_back( range(px,*x) ); + } + + // copy and + map< pt, set > xconn; + for(map >::iterator it=xray.begin(); it!=xray.end(); ++it) + { + int x = it->first; + vector& r = it->second; + + vector< pair > es; + for(int i=0; i!=r.size(); ++i) + es.push_back( make_pair(r[i].first,false) ), + es.push_back( make_pair(r[i].second,true) ); + sort(es.begin(), es.end()); + vector cur; + int nest = 0; + for(int i=0; i!=es.size(); ++i) + { + cur.push_back( es[i].first ); + es[i].second ? --nest : ++nest; + if( nest == 0 ) + { + for(int j=0; j > yconn; + for(map >::iterator it=yray.begin(); it!=yray.end(); ++it) + { + int y = it->first; + vector& r = it->second; + + vector< pair > es; + for(int i=0; i!=r.size(); ++i) + es.push_back( make_pair(r[i].first,false) ), + es.push_back( make_pair(r[i].second,true) ); + sort(es.begin(), es.end()); + + vector cur; + int nest = 0; + for(int i=0; i!=es.size(); ++i) + { + cur.push_back( es[i].first ); + es[i].second ? --nest : ++nest; + if( nest == 0 ) + { + for(int j=0; j >::iterator it=xconn.begin(); it!=xconn.end(); ++it) + { + pt p = it->first; + set& c = it->second; + for(set::iterator jt=c.begin(); jt!=c.end(); ++jt) + { + pt q = *jt; + int w = q.second-p.second; + pt p1(p.first+w,p.second); + pt q1(q.first+w,q.second); + if( yconn[p].count(p1) && yconn[q].count(q1) && xconn[p1].count(q1) ) + ++cnt; + } + } + return cnt; + } +}; + +// 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 string print_array(const vector &V) { ostringstream os; os << "{ "; for (typename vector::const_iterator iter = V.begin(); iter != V.end(); ++iter) os << '\"' << *iter << "\","; os << " }"; return os.str(); } +int verify_case(const int &Expected, const int &Received) { if (Expected == Received) cerr << "PASSED" << timer() << endl; else { cerr << "FAILED" << timer() << endl; cerr << "\tExpected: \"" << Expected << '\"' << endl; cerr << "\tReceived: \"" << Received << '\"' << endl; } return 0;} + +template struct Case_ { Case_(){start_time=clock();} }; +char Test_(...); +int Test_(Case_<0>) { + int x1_[] = {0,1,0}; + vector x1(x1_, x1_+sizeof(x1_)/sizeof(*x1_)); + int y1_[] = {0,0,1}; + vector y1(y1_, y1_+sizeof(y1_)/sizeof(*y1_)); + int x2_[] = {3,2,3}; + vector x2(x2_, x2_+sizeof(x2_)/sizeof(*x2_)); + int y2_[] = {3,3,2}; + vector y2(y2_, y2_+sizeof(y2_)/sizeof(*y2_)); + int RetVal = 14; + return verify_case(RetVal, HiddenSquares().count(x1, y1, x2, y2)); } +int Test_(Case_<1>) { + int x1_[] = {0,2}; + vector x1(x1_, x1_+sizeof(x1_)/sizeof(*x1_)); + int y1_[] = {0,0}; + vector y1(y1_, y1_+sizeof(y1_)/sizeof(*y1_)); + int x2_[] = {2,4}; + vector x2(x2_, x2_+sizeof(x2_)/sizeof(*x2_)); + int y2_[] = {4,4}; + vector y2(y2_, y2_+sizeof(y2_)/sizeof(*y2_)); + int RetVal = 1; + return verify_case(RetVal, HiddenSquares().count(x1, y1, x2, y2)); } +int Test_(Case_<2>) { + int x1_[] = {0,0,3}; + vector x1(x1_, x1_+sizeof(x1_)/sizeof(*x1_)); + int y1_[] = {0,3,0}; + vector y1(y1_, y1_+sizeof(y1_)/sizeof(*y1_)); + int x2_[] = {1,4,4}; + vector x2(x2_, x2_+sizeof(x2_)/sizeof(*x2_)); + int y2_[] = {3,4,3}; + vector y2(y2_, y2_+sizeof(y2_)/sizeof(*y2_)); + int RetVal = 0; + return verify_case(RetVal, HiddenSquares().count(x1, y1, x2, y2)); } +int Test_(Case_<3>) { + int x1_[] = {453453463,453453500,453453495,453453512,453453478,453453489, + 453453466,453453500,453453498,453453510,453453472,453453512, + 453453519,453453514,453453521,453453518,453453523,453453517, + 453453466,453453525,453453496,453453495,453453463,453453461, + 453453460,453453522,453453471,453453468,453453479,453453517, + 453453485,453453518,453453499,453453464,453453494}; + vector x1(x1_, x1_+sizeof(x1_)/sizeof(*x1_)); + int y1_[] = {364646438,364646402,364646449,364646438,364646415,364646401, + 364646446,364646416,364646456,364646414,364646463,364646407, + 364646436,364646450,364646421,364646411,364646414,364646419, + 364646445,364646427,364646405,364646442,364646418,364646464, + 364646457,364646463,364646432,364646426,364646444,364646431, + 364646450,364646418,364646434,364646458,364646402}; + vector y1(y1_, y1_+sizeof(y1_)/sizeof(*y1_)); + int x2_[] = {453453488,453453510,453453525,453453523,453453493,453453500, + 453453470,453453511,453453499,453453521,453453518,453453524, + 453453523,453453523,453453524,453453523,453453525,453453519, + 453453473,453453526,453453511,453453517,453453470,453453464, + 453453511,453453524,453453516,453453516,453453492,453453524, +453453513,453453522,453453520,453453505,453453512}; + vector x2(x2_, x2_+sizeof(x2_)/sizeof(*x2_)); + int y2_[] = {364646460,364646454,364646462,364646466,364646464,364646455, + 364646457,364646461,364646457,364646450,364646466,364646453, + 364646441,364646451,364646460,364646461,364646446,364646464, + 364646447,364646460,364646454,364646450,364646444,364646466, + 364646458,364646466,364646455,364646442,364646462,364646435, + 364646464,364646444,364646455,364646459,364646430}; + vector y2(y2_, y2_+sizeof(y2_)/sizeof(*y2_)); + int RetVal = 124; + return verify_case(RetVal, HiddenSquares().count(x1, y1, x2, y2)); } + +template void Run_() { cerr << "Test Case #" << N << "..." << flush; Test_(Case_()); Run_()))==1 ? -1 : N+1>(); } +template<> void Run_<-1>() {} +int main() { Run_<0>(); } +// END CUT HERE + ADDED SRM/332/1A.cpp Index: SRM/332/1A.cpp ================================================================== --- SRM/332/1A.cpp +++ SRM/332/1A.cpp @@ -0,0 +1,85 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +using namespace std; +typedef long long LL; + +class CreatePairs { +public: + int maximalSum(vector data) + { + sort(data.begin(), data.end()); + vector::iterator neg = upper_bound(data.begin(), data.end(), 0); + + LL sum = 0; + for(vector::iterator it=data.begin(); it!=neg; ++it) + if( it+1 == neg ) + sum += *it; + else + sum += *it * *(it+1), ++it; + + vector::iterator pos = lower_bound(data.begin(), data.end(), 2); + for(vector::iterator it=data.end()-1; it>=pos; --it) + if( it == pos ) + sum += *it; + else + sum += *it * *(it-1), --it; + for(vector::iterator it=neg; it!=pos; ++it) + sum += *it; + return sum; + } +}; + +// 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 string print_array(const vector &V) { ostringstream os; os << "{ "; for (typename vector::const_iterator iter = V.begin(); iter != V.end(); ++iter) os << '\"' << *iter << "\","; os << " }"; return os.str(); } +int verify_case(const int &Expected, const int &Received) { if (Expected == Received) cerr << "PASSED" << timer() << endl; else { cerr << "FAILED" << timer() << endl; cerr << "\tExpected: \"" << Expected << '\"' << endl; cerr << "\tReceived: \"" << Received << '\"' << endl; } return 0;} + +template struct Case_ { Case_(){start_time=clock();} }; +char Test_(...); +int Test_(Case_<0>) { + int data_[] = {0, 1, 2, 4, 3, 5}; + vector data(data_, data_+sizeof(data_)/sizeof(*data_)); + int RetVal = 27; + return verify_case(RetVal, CreatePairs().maximalSum(data)); } +int Test_(Case_<1>) { + int data_[] = {-1, 1, 2, 3}; + vector data(data_, data_+sizeof(data_)/sizeof(*data_)); + int RetVal = 6; + return verify_case(RetVal, CreatePairs().maximalSum(data)); } +int Test_(Case_<2>) { + int data_[] = {-1}; + vector data(data_, data_+sizeof(data_)/sizeof(*data_)); + int RetVal = -1; + return verify_case(RetVal, CreatePairs().maximalSum(data)); } +int Test_(Case_<3>) { + int data_[] = {-1, 0, 1}; + vector data(data_, data_+sizeof(data_)/sizeof(*data_)); + int RetVal = 1; + return verify_case(RetVal, CreatePairs().maximalSum(data)); } +int Test_(Case_<4>) { + int data_[] = {1, 1}; + vector data(data_, data_+sizeof(data_)/sizeof(*data_)); + int RetVal = 2; + return verify_case(RetVal, CreatePairs().maximalSum(data)); } + +template void Run_() { cerr << "Test Case #" << N << "..." << flush; Test_(Case_()); Run_()))==1 ? -1 : N+1>(); } +template<> void Run_<-1>() {} +int main() { Run_<0>(); } +// END CUT HERE + ADDED SRM/332/1B.cpp Index: SRM/332/1B.cpp ================================================================== --- SRM/332/1B.cpp +++ SRM/332/1B.cpp @@ -0,0 +1,184 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +using namespace std; +typedef long long LL; + +struct edge { + int y, x1, x2; + edge(){} + edge(int y, int x1, int x2) :y(y),x1(min(x1,x2)),x2(max(x1,x2)) {} + bool operator<( const edge& rhs ) const { + if( y != rhs.y ) return y x1, vector x2, vector y) + { + int N = x1.size(); + + vector e; + for(int i=0; i!=N; ++i) + e.push_back( edge(y[i], x1[i], x2[i]) ); + sort(e.begin(), e.end()); + + int ans = 0; + for(int m=1; m<(1<& e_, int m) + { + vector e; + for(int i=0; (1< point; + typedef map< point, vector > graph; + graph G; + for(int i=0; i=0; --j) + if( e[j].x1 == x ) { + point p1(x, y); + point p2(e[j].x1, e[j].y); + if( G[p2].size() < 2 ) { + G[p1].push_back(p2); + G[p2].push_back(p1); + } + } else if( e[j].x2 == x ) { + point p1(x, y); + point p2(e[j].x2, e[j].y); + if( G[p2].size() < 2 ) { + G[p1].push_back(p2); + G[p2].push_back(p1); + } + } else if( e[j].x1 < x && x < e[j].x2 ) { + break; + } + } + } + + for(graph::iterator it=G.begin(); it!=G.end(); ++it) + if( it->second.size() != 2 ) + return 0; + + point s = G.begin()->first; + point p = s, p2 = G[p][0]; + for(int c=2 ;; ++c) { + point q = (G[p2][0] == p ? G[p2][1] : G[p2][0]); + p=p2, p2=q; + if( q == s ) + return c==e.size()*2 ? c : 0; + } + } +}; + +// 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 string print_array(const vector &V) { ostringstream os; os << "{ "; for (typename vector::const_iterator iter = V.begin(); iter != V.end(); ++iter) os << '\"' << *iter << "\","; os << " }"; return os.str(); } +int verify_case(const int &Expected, const int &Received) { if (Expected == Received) cerr << "PASSED" << timer() << endl; else { cerr << "FAILED" << timer() << endl; cerr << "\tExpected: \"" << Expected << '\"' << endl; cerr << "\tReceived: \"" << Received << '\"' << endl; } return 0;} + +template struct Case_ { Case_(){start_time=clock();} }; +char Test_(...); +int Test_(Case_<0>) { + int x1_[] = {1,2,3,1}; + vector x1(x1_, x1_+sizeof(x1_)/sizeof(*x1_)); + int x2_[] = {2,3,5,5}; + vector x2(x2_, x2_+sizeof(x2_)/sizeof(*x2_)); + int y_[] = {1,4,2,0}; + vector y(y_, y_+sizeof(y_)/sizeof(*y_)); + int RetVal = 8; + return verify_case(RetVal, RestoringPolygon().restore(x1, x2, y)); } +int Test_(Case_<1>) { + int x1_[] = {1,1,2,2}; + vector x1(x1_, x1_+sizeof(x1_)/sizeof(*x1_)); + int x2_[] = {3,3,4,4}; + vector x2(x2_, x2_+sizeof(x2_)/sizeof(*x2_)); + int y_[] = {0,2,1,3}; + vector y(y_, y_+sizeof(y_)/sizeof(*y_)); + int RetVal = 4; + return verify_case(RetVal, RestoringPolygon().restore(x1, x2, y)); } +int Test_(Case_<2>) { + int x1_[] = {1}; + vector x1(x1_, x1_+sizeof(x1_)/sizeof(*x1_)); + int x2_[] = {2}; + vector x2(x2_, x2_+sizeof(x2_)/sizeof(*x2_)); + int y_[] = {1}; + vector y(y_, y_+sizeof(y_)/sizeof(*y_)); + int RetVal = 0; + return verify_case(RetVal, RestoringPolygon().restore(x1, x2, y)); } +int Test_(Case_<3>) { + int x1_[] = {0,0,0}; + vector x1(x1_, x1_+sizeof(x1_)/sizeof(*x1_)); + int x2_[] = {1000,1000,1000}; + vector x2(x2_, x2_+sizeof(x2_)/sizeof(*x2_)); + int y_[] = {0,1,2}; + vector y(y_, y_+sizeof(y_)/sizeof(*y_)); + int RetVal = 4; + return verify_case(RetVal, RestoringPolygon().restore(x1, x2, y)); } +int Test_(Case_<4>) { + int x1_[] = {0,1,1,2}; + vector x1(x1_, x1_+sizeof(x1_)/sizeof(*x1_)); + int x2_[] = {1,0,2,1}; + vector x2(x2_, x2_+sizeof(x2_)/sizeof(*x2_)); + int y_[] = {0,4,2,3}; + vector y(y_, y_+sizeof(y_)/sizeof(*y_)); + int RetVal = 8; + return verify_case(RetVal, RestoringPolygon().restore(x1, x2, y)); } +int Test_(Case_<5>) { + int x1_[] = {696, -193, -193, 367}; + vector x1(x1_, x1_+sizeof(x1_)/sizeof(*x1_)); + int x2_[] = {367, -276, -276, 696}; + vector x2(x2_, x2_+sizeof(x2_)/sizeof(*x2_)); + int y_[] = {-14, 168, -14, 168}; + vector y(y_, y_+sizeof(y_)/sizeof(*y_)); + int RetVal = 4; + return verify_case(RetVal, RestoringPolygon().restore(x1, x2, y)); } +int Test_(Case_<6>) { + int x1_[] = { 0, 1, 6, 5, 5, 1, 6, 1, 0, 0, 9}; + vector x1(x1_, x1_+sizeof(x1_)/sizeof(*x1_)); + int x2_[] = { 5, 9, 9, 6, 6, 0, 9, 0, 1, 5, 6}; + vector x2(x2_, x2_+sizeof(x2_)/sizeof(*x2_)); + int y_[] = { 1, 4, 5, 3, 2, 0, 0, 2, 3, 5, 1}; + vector y(y_, y_+sizeof(y_)/sizeof(*y_)); + int RetVal = 16; + return verify_case(RetVal, RestoringPolygon().restore(x1, x2, y)); } + +template void Run_() { cerr << "Test Case #" << N << "..." << flush; Test_(Case_()); Run_()))==1 ? -1 : N+1>(); } +template<> void Run_<-1>() {} +int main() { Run_<0>(); } +// END CUT HERE + ADDED SRM/332/1C.cpp Index: SRM/332/1C.cpp ================================================================== --- SRM/332/1C.cpp +++ SRM/332/1C.cpp @@ -0,0 +1,126 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +using namespace std; +typedef long long LL; +typedef complex CMP; + +class LadderPermutation { public: + vector createLadder(int n, int m, int k) + { + try { + vector v = gen(n-k, m-1, k); + for(int i=0; i(); + } + } + + vector gen(int n, int m, int k) // make m blocks of length <=k + { + if( m==0 ) { + if( n==0 ) + return vector(); + throw "fail"; + } + int x = min(k, n-(m-1)); + if( x <= 0 ) + throw "fail"; + vector v = gen(n-x, m-1, k); + for(int i=0; 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 vector & Expected, const vector & 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(_, LadderPermutation().createLadder(n, m, k));} +int main(){ + +CASE(0) + int n = 4; + int m = 2; + int k = 2; + int __[] = {2, 1, 4, 3 }; + vector _(__, __+sizeof(__)/sizeof(*__)); +END +CASE(1) + int n = 3; + int m = 2; + int k = 2; + int __[] = {1, 3, 2 }; + vector _(__, __+sizeof(__)/sizeof(*__)); +END +CASE(2) + int n = 2; + int m = 1; + int k = 1; + vector _; +END +CASE(3) + int n = 6; + int m = 3; + int k = 2; + int __[] = {2, 1, 4, 3, 6, 5 }; + vector _(__, __+sizeof(__)/sizeof(*__)); +END +CASE(4) + int n = 6; + int m = 2; + int k = 3; + int __[] = {3, 2, 1, 6, 5, 4 }; + vector _(__, __+sizeof(__)/sizeof(*__)); +END +CASE(5) + int n = 7; + int m = 4; + int k = 4; + int __[] = {1, 2, 3, 7, 6, 5, 4 }; + vector _(__, __+sizeof(__)/sizeof(*__)); +END +/* +CASE(6) + int n = ; + int m = ; + int k = ; + int __[] = ; + vector _(__, __+sizeof(__)/sizeof(*__)); +END +CASE(7) + int n = ; + int m = ; + int k = ; + int __[] = ; + vector _(__, __+sizeof(__)/sizeof(*__)); +END +*/ +} +// END CUT HERE ADDED SRM/332/2C.cpp Index: SRM/332/2C.cpp ================================================================== --- SRM/332/2C.cpp +++ SRM/332/2C.cpp @@ -0,0 +1,97 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +using namespace std; +typedef long long LL; +typedef complex CMP; + +class Squares { public: + int countSquares(vector field) + { + const int H = field.size(); + const int W = field[0].size(); + + int cnt = 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 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(_, Squares().countSquares(field));} +int main(){ + +CASE(0) + string field_[] = {"ABA", "BAB", "ABA"}; + vector field(field_, field_+sizeof(field_)/sizeof(*field_)); + int _ = 2; +END +CASE(1) + string field_[] = {"AA", "AA"}; + vector field(field_, field_+sizeof(field_)/sizeof(*field_)); + int _ = 1; +END +CASE(2) + string field_[] = {"ABC", "DEF", "GHI"}; + vector field(field_, field_+sizeof(field_)/sizeof(*field_)); + int _ = 0; +END +CASE(3) + string field_[] = {"AABCA", "AAAAA", "BAAAB", "AAAEA", "ADBFA"}; + vector field(field_, field_+sizeof(field_)/sizeof(*field_)); + int _ = 11; +END +/* +CASE(4) + string field_[] = ; + vector field(field_, field_+sizeof(field_)/sizeof(*field_)); + int _ = ; +END +CASE(5) + string field_[] = ; + vector field(field_, field_+sizeof(field_)/sizeof(*field_)); + int _ = ; +END +*/ +} +// END CUT HERE ADDED SRM/333/1A.cpp Index: SRM/333/1A.cpp ================================================================== --- SRM/333/1A.cpp +++ SRM/333/1A.cpp @@ -0,0 +1,98 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +using namespace std; +typedef long long LL; + +int to_i(const string& s) { + int n; + stringstream(s)>>n; + return n; +} + +class BirthNumbersValidator { +public: + vector validate(vector test) + { + vector ans; + transform(test.begin(), test.end(), back_inserter(ans), &testeach); + return ans; + } + + static string testeach( const string& x ) + { + int y = to_i(x.substr(0,2)); + int m = to_i(x.substr(2,2)); + int d = to_i(x.substr(4,2)); + if( 1<=m && m<=12 ) {} + else if( 51<=m && m<=62 ) { m-=50; } + else return "NO"; + + bool leap = (y%4)==0; + int dd[] = {31, leap?29:28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; + if( 1<=d && d<=dd[m-1] ) {} + else return "NO"; + + LL cd; stringstream(x)>>cd; + return cd%11==0 ? "YES" : "NO"; + } +}; + +// 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 string print_array(const vector &V) { ostringstream os; os << "{ "; for (typename vector::const_iterator iter = V.begin(); iter != V.end(); ++iter) os << '\"' << *iter << "\","; os << " }"; return os.str(); } +int verify_case(const vector &Expected, const vector &Received) { if (Expected == Received) cerr << "PASSED" << timer() << endl; else { cerr << "FAILED" << timer() << endl; cerr << "\tExpected: " << print_array(Expected) << endl; cerr << "\tReceived: " << print_array(Received) << endl; } return 0;} + +template struct Case_ { Case_(){start_time=clock();} }; +char Test_(...); +int Test_(Case_<0>) { + string test_[] = {"8104121234"}; + vector test(test_, test_+sizeof(test_)/sizeof(*test_)); + string RetVal_[] = {"YES" }; + vector RetVal(RetVal_, RetVal_+sizeof(RetVal_)/sizeof(*RetVal_)); + return verify_case(RetVal, BirthNumbersValidator().validate(test)); } +int Test_(Case_<1>) { + string test_[] = {"8154121239"}; + vector test(test_, test_+sizeof(test_)/sizeof(*test_)); + string RetVal_[] = {"YES" }; + vector RetVal(RetVal_, RetVal_+sizeof(RetVal_)/sizeof(*RetVal_)); + return verify_case(RetVal, BirthNumbersValidator().validate(test)); } +int Test_(Case_<2>) { + string test_[] = {"8134120005"}; + vector test(test_, test_+sizeof(test_)/sizeof(*test_)); + string RetVal_[] = {"NO" }; + vector RetVal(RetVal_, RetVal_+sizeof(RetVal_)/sizeof(*RetVal_)); + return verify_case(RetVal, BirthNumbersValidator().validate(test)); } +int Test_(Case_<3>) { + string test_[] = {"8102310007","8104121235"}; + vector test(test_, test_+sizeof(test_)/sizeof(*test_)); + string RetVal_[] = {"NO", "NO" }; + vector RetVal(RetVal_, RetVal_+sizeof(RetVal_)/sizeof(*RetVal_)); + return verify_case(RetVal, BirthNumbersValidator().validate(test)); } +int Test_(Case_<4>) { + string test_[] = {"0411131237"}; + vector test(test_, test_+sizeof(test_)/sizeof(*test_)); + string RetVal_[] = {"YES" }; + vector RetVal(RetVal_, RetVal_+sizeof(RetVal_)/sizeof(*RetVal_)); + return verify_case(RetVal, BirthNumbersValidator().validate(test)); } + +template void Run_() { cerr << "Test Case #" << N << "..." << flush; Test_(Case_()); Run_()))==1 ? -1 : N+1>(); } +template<> void Run_<-1>() {} +int main() { Run_<0>(); } +// END CUT HERE + ADDED SRM/333/1B.cpp Index: SRM/333/1B.cpp ================================================================== --- SRM/333/1B.cpp +++ SRM/333/1B.cpp @@ -0,0 +1,160 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +using namespace std; +typedef long long LL; + +class RepeatedPatterns { +public: + long long findZeroSegment(string P1, string P2, string zeroCount) + { + LL zc; stringstream(zeroCount)>>zc; + + bool allz1 = (P1.find('1') == string::npos); + bool allz2 = (P2.find('1') == string::npos); + if( allz1 && allz2 ) + return 0; + + if( allz1 ) + { + LL z1 = P1.size()*1000000; + if( zc <= z1 ) + return 0; + + LL a2 = 0; while( P2[a2]=='0' ) ++a2; + LL b2 = 0; while( P2[P2.size()-1-b2]=='0' ) ++b2; + LL c2=0, i2; + for(int i=0; i c2 ) c2=ii-i, i2=i; + } + + if( zc <= z1+a2 ) + return 0; + if( zc <= c2 ) + return P1.size()*1000000 + i2; + if( zc <= b2+z1+a2 ) + return P1.size()*1000000 + P2.size() - b2; + return -1; + } + else if( allz2 ) + { + LL a1 = 0; while( P1[a1]=='0' ) ++a1; + LL b1 = 0; while( P1[P1.size()-1-b1]=='0' ) ++b1; + LL c1=0, i1; + for(int i=0; i c1 ) c1=ii-i, i1=i; + } + if( zc <= a1 ) + return 0; + if( zc <= c1 ) + return i1; + if( zc <= b1+a1 ) + return P1.size() - b1; + // zc <= b1 + k*P2.size() + a1; + LL k = (zc-b1-a1) / P2.size() + ((zc-b1-a1)%P2.size() ? 1 : 0); + LL idx = P1.size()*1000000*k + (k-1)*k/2*P2.size() - b1; + return 0<=idx && idx+zc<=10000000000000000LL ? idx : -1; + } + else + { + LL a1 = 0; while( P1[a1]=='0' ) ++a1; + LL b1 = 0; while( P1[P1.size()-1-b1]=='0' ) ++b1; + LL c1=0, i1; + for(int i=0; i c1 ) c1=ii-i, i1=i; + } + LL a2 = 0; while( P2[a2]=='0' ) ++a2; + LL b2 = 0; while( P2[P2.size()-1-b2]=='0' ) ++b2; + LL c2=0, i2; + for(int i=0; i c2 ) c2=ii-i, i2=i; + } + if( zc <= a1 ) + return 0; + if( zc <= c1 ) + return i1; + if( zc <= b1+a1 ) + return P1.size() - b1; + if( zc <= b1+a2 ) + return P1.size()*1000000 - b1; + if( zc <= c2 ) + return P1.size()*1000000 + i2; + if( zc <= b2+a1 ) + return P1.size()*1000000 + P2.size() - b2; + if( zc <= b2+a2 ) + return P1.size()*1000000 + P2.size() + P1.size()*1000000 + P2.size() - b2; + return -1; + } + } +}; + +// 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 string print_array(const vector &V) { ostringstream os; os << "{ "; for (typename vector::const_iterator iter = V.begin(); iter != V.end(); ++iter) os << '\"' << *iter << "\","; os << " }"; return os.str(); } +int verify_case(const long long &Expected, const long long &Received) { if (Expected == Received) cerr << "PASSED" << timer() << endl; else { cerr << "FAILED" << timer() << endl; cerr << "\tExpected: \"" << Expected << '\"' << endl; cerr << "\tReceived: \"" << Received << '\"' << endl; } return 0;} + +template struct Case_ { Case_(){start_time=clock();} }; +char Test_(...); +int Test_(Case_<0>) { + string P1 = "111010100001010"; + string P2 = "010000001000"; + string zeroCount = "3"; + long long RetVal = 7LL; + return verify_case(RetVal, RepeatedPatterns().findZeroSegment(P1, P2, zeroCount)); } +int Test_(Case_<1>) { + string P1 = "1101010010"; + string P2 = "0001000"; + string zeroCount = "3"; + long long RetVal = 9999999LL; + return verify_case(RetVal, RepeatedPatterns().findZeroSegment(P1, P2, zeroCount)); } +int Test_(Case_<2>) { + string P1 = "1101010010"; + string P2 = "0001000"; + string zeroCount = "5"; + long long RetVal = 20000011LL; + return verify_case(RetVal, RepeatedPatterns().findZeroSegment(P1, P2, zeroCount)); } +int Test_(Case_<3>) { + string P1 = "10101010"; + string P2 = "101010101010"; + string zeroCount = "9876543219876"; + long long RetVal = -1LL; + return verify_case(RetVal, RepeatedPatterns().findZeroSegment(P1, P2, zeroCount)); } +int Test_(Case_<4>) { + string P1 = "11111111111111111111111111"; + string P2 = "0"; + string zeroCount = "9876543219876"; + long long RetVal = -1LL; + return verify_case(RetVal, RepeatedPatterns().findZeroSegment(P1, P2, zeroCount)); } + +template void Run_() { cerr << "Test Case #" << N << "..." << flush; Test_(Case_()); Run_()))==1 ? -1 : N+1>(); } +template<> void Run_<-1>() {} +int main() { Run_<0>(); } +// END CUT HERE + ADDED SRM/333/1C.cpp Index: SRM/333/1C.cpp ================================================================== --- SRM/333/1C.cpp +++ SRM/333/1C.cpp @@ -0,0 +1,106 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +using namespace std; +typedef long long LL; + +class ThreeBuses { +public: + double getProbability(vector wait, vector travel, int timeLeft) + { + double t = timeLeft - accumulate(travel.begin(), travel.end(), 0); + sort(wait.begin(), wait.end()); + double w1 = wait[0]; + double w2 = wait[1]; + double w3 = wait[2]; + // [0,w1] + [0,w2] + [0,w3] <= t ?? + if( t >= w1+w2+w3 ) return 1; + if( t <= 0 ) return 0; + + if( w2==0 ) return t / w3; + if( w1==0 ) { + double vol = t*t/2; + if( t > w2 ) vol -= (t-w2)*(t-w2)/2; + if( t > w3 ) vol -= (t-w3)*(t-w3)/2; + return vol / (w2*w3); + } + + double vol = t*t*t/6; + if( t > w1 ) vol -= (t-w1)*(t-w1)*(t-w1)/6; + if( t > w2 ) vol -= (t-w2)*(t-w2)*(t-w2)/6; + if( t > w3 ) vol -= (t-w3)*(t-w3)*(t-w3)/6; + if( t > w1+w2 ) vol += (t-w1-w2)*(t-w1-w2)*(t-w1-w2)/6; + if( t > w2+w3 ) vol += (t-w2-w3)*(t-w2-w3)*(t-w2-w3)/6; + if( t > w3+w1 ) vol += (t-w3-w1)*(t-w3-w1)*(t-w3-w1)/6; + return vol / (w1*w2*w3); + } +}; + +// 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 string print_array(const vector &V) { ostringstream os; os << "{ "; for (typename vector::const_iterator iter = V.begin(); iter != V.end(); ++iter) os << '\"' << *iter << "\","; os << " }"; return os.str(); } +int verify_case(const double &Expected, const double &Received) { double diff = Expected - Received; if (diff < 0) diff = -diff; if (diff < 1e-9) cerr << "PASSED" << timer() << endl; else { cerr << "FAILED" << timer() << endl; cerr << "\tExpected: \"" << Expected << '\"' << endl; cerr << "\tReceived: \"" << Received << '\"' << endl; } return 0;} + +template struct Case_ { Case_(){start_time=clock();} }; +char Test_(...); +int Test_(Case_<0>) { + int wait_[] = {0, 0, 0}; + vector wait(wait_, wait_+sizeof(wait_)/sizeof(*wait_)); + int travel_[] = {10, 15, 10}; + vector travel(travel_, travel_+sizeof(travel_)/sizeof(*travel_)); + int timeLeft = 47; + double RetVal = 1.0; + return verify_case(RetVal, ThreeBuses().getProbability(wait, travel, timeLeft)); } +int Test_(Case_<1>) { + int wait_[] = {0, 0, 0}; + vector wait(wait_, wait_+sizeof(wait_)/sizeof(*wait_)); + int travel_[] = {10, 15, 10}; + vector travel(travel_, travel_+sizeof(travel_)/sizeof(*travel_)); + int timeLeft = 35; + double RetVal = 1.0; + return verify_case(RetVal, ThreeBuses().getProbability(wait, travel, timeLeft)); } +int Test_(Case_<2>) { + int wait_[] = {1, 100, 1}; + vector wait(wait_, wait_+sizeof(wait_)/sizeof(*wait_)); + int travel_[] = {10, 10, 10}; + vector travel(travel_, travel_+sizeof(travel_)/sizeof(*travel_)); + int timeLeft = 52; + double RetVal = 0.21; + return verify_case(RetVal, ThreeBuses().getProbability(wait, travel, timeLeft)); } +int Test_(Case_<3>) { + int wait_[] = {100, 100, 70}; + vector wait(wait_, wait_+sizeof(wait_)/sizeof(*wait_)); + int travel_[] = {1, 1, 1}; + vector travel(travel_, travel_+sizeof(travel_)/sizeof(*travel_)); + int timeLeft = 47; + double RetVal = 0.020281904761904737; + return verify_case(RetVal, ThreeBuses().getProbability(wait, travel, timeLeft)); } +int Test_(Case_<4>) { + int wait_[] = {43210, 31730, 0}; + vector wait(wait_, wait_+sizeof(wait_)/sizeof(*wait_)); + int travel_[] = {1, 1, 1}; + vector travel(travel_, travel_+sizeof(travel_)/sizeof(*travel_)); + int timeLeft = 43212; + double RetVal = 0.6328164776672066; + return verify_case(RetVal, ThreeBuses().getProbability(wait, travel, timeLeft)); } + +template void Run_() { cerr << "Test Case #" << N << "..." << flush; Test_(Case_()); Run_()))==1 ? -1 : N+1>(); } +template<> void Run_<-1>() {} +int main() { Run_<0>(); } +// END CUT HERE + ADDED SRM/334/1A.cpp Index: SRM/334/1A.cpp ================================================================== --- SRM/334/1A.cpp +++ SRM/334/1A.cpp @@ -0,0 +1,103 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +using namespace std; +typedef long long LL; + +class EncodedSum +{ +public: + long long maximumSum(vector numbers) + { + set nonzero; + for(int i=0; i=0; --j,d*=10) + v[numbers[i][j]-'A'] += d; + + LL d[] = {0,1,2,3,4,5,6,7,8,9}; + LL sumMax = 0; + do { + LL sum = 0; + for(int c=0; c<10; ++c) + if( d[c]==0 && nonzero.count(c) ) + goto nextPerm; + else + sum += v[c] * d[c]; + sumMax = max(sumMax, sum); + nextPerm:; + } while( next_permutation(d+0, d+10) ); + return sumMax; + } +}; + +// 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 string print_array(const vector &V) { ostringstream os; os << "{ "; for (typename vector::const_iterator iter = V.begin(); iter != V.end(); ++iter) os << '\"' << *iter << "\","; os << " }"; return os.str(); } +int verify_case(const long long &Expected, const long long &Received) { if (Expected == Received) cerr << "PASSED" << timer() << endl; else { cerr << "FAILED" << timer() << endl; cerr << "\tExpected: \"" << Expected << '\"' << endl; cerr << "\tReceived: \"" << Received << '\"' << endl; } return 0;} + +template struct Case_ { Case_(){start_time=clock();} }; +char Test_(...); +int Test_(Case_<0>) { + string numbers_[] = {"ABC", + "BCA"}; + vector numbers(numbers_, numbers_+sizeof(numbers_)/sizeof(*numbers_)); + long long RetVal = 1875LL; + return verify_case(RetVal, EncodedSum().maximumSum(numbers)); } +int Test_(Case_<1>) { + string numbers_[] = {"ABCDEFGHIJ"}; + vector numbers(numbers_, numbers_+sizeof(numbers_)/sizeof(*numbers_)); + long long RetVal = 9876543210LL; + return verify_case(RetVal, EncodedSum().maximumSum(numbers)); } +int Test_(Case_<2>) { + string numbers_[] = {"ABCDEFGHIJ", + "J"}; + vector numbers(numbers_, numbers_+sizeof(numbers_)/sizeof(*numbers_)); + long long RetVal = 9876543202LL; + return verify_case(RetVal, EncodedSum().maximumSum(numbers)); } +int Test_(Case_<3>) { + string numbers_[] = {"A", + "BB", + "CCC", + "DDDD", + "EEEEE", + "FFFFFF", + "GGGGGGG", + "HHHHHHHH", + "IIIIIIIII", + "AJJJJJJJJJ"}; + vector numbers(numbers_, numbers_+sizeof(numbers_)/sizeof(*numbers_)); + long long RetVal = 9973936905LL; + return verify_case(RetVal, EncodedSum().maximumSum(numbers)); } +int Test_(Case_<4>) { + string numbers_[] = {"GHJIDDD", + "AHHCCCA", + "IIJCEJ", + "F", + "HDBIGFJAAJ"}; + vector numbers(numbers_, numbers_+sizeof(numbers_)/sizeof(*numbers_)); + long long RetVal = 9888114550LL; + return verify_case(RetVal, EncodedSum().maximumSum(numbers)); } + +template void Run_() { cerr << "Test Case #" << N << "..." << flush; Test_(Case_()); Run_()))==1 ? -1 : N+1>(); } +template<> void Run_<-1>() {} +int main() { Run_<0>(); } +// END CUT HERE + ADDED SRM/334/1B.cpp Index: SRM/334/1B.cpp ================================================================== --- SRM/334/1B.cpp +++ SRM/334/1B.cpp @@ -0,0 +1,122 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +using namespace std; +typedef long long LL; + +class ExtendedHappyNumbers +{ +public: + long long calcTheSum(int A, int B, int K) + { + vector happy(9*9*9*9*9*9*7); +// map happy; + LL sum = 0; + for(int n=A; n<=B; ++n) + sum += rec(n, K, happy); + return sum; + } + + int S(int K, int n) + { + int sum = 0; + for(int d=1; d<=n; d*=10) + { + int v = n/d%10; + int t = 1; + for(int i=0; i& happy) + { + int LOOP = -1; + stack vs; + int m = 0x7fffffff; + for(;;) + { + if( happy[n] == LOOP ) + {m = looped(n, K); break;} + if( happy[n] ) + {m = happy[n]; break;} + happy[n] = LOOP; + vs.push(n); + n = S(K, n); + } + while(!vs.empty()) { + int v = vs.top(); vs.pop(); + happy[v] = m = min(m, v); + } + return m; + } + + int looped(int n, int K) + { + int mn = n; + vector vs(1, n); + for(int v=S(K,n); v!=n; v=S(K,v)) + mn = min(mn, v); + return mn; + } +}; + +// 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 string print_array(const vector &V) { ostringstream os; os << "{ "; for (typename vector::const_iterator iter = V.begin(); iter != V.end(); ++iter) os << '\"' << *iter << "\","; os << " }"; return os.str(); } +int verify_case(const long long &Expected, const long long &Received) { if (Expected == Received) cerr << "PASSED" << timer() << endl; else { cerr << "FAILED" << timer() << endl; cerr << "\tExpected: \"" << Expected << '\"' << endl; cerr << "\tReceived: \"" << Received << '\"' << endl; } return 0;} + +template struct Case_ { Case_(){start_time=clock();} }; +char Test_(...); +int Test_(Case_<0>) { + int A = 13; + int B = 13; + int K = 2; + long long RetVal = 1LL; + return verify_case(RetVal, ExtendedHappyNumbers().calcTheSum(A, B, K)); } +int Test_(Case_<1>) { + int A = 1; + int B = 5; + int K = 2; + long long RetVal = 14LL; + return verify_case(RetVal, ExtendedHappyNumbers().calcTheSum(A, B, K)); } +int Test_(Case_<2>) { + int A = 10; + int B = 99; + int K = 1; + long long RetVal = 450LL; + return verify_case(RetVal, ExtendedHappyNumbers().calcTheSum(A, B, K)); } +int Test_(Case_<3>) { + int A = 535; + int B = 538; + int K = 3; + long long RetVal = 820LL; + return verify_case(RetVal, ExtendedHappyNumbers().calcTheSum(A, B, K)); } +int Test_(Case_<4>) { + int A = 100000; + int B = 400000; + int K = 6; + long long RetVal = 5169721292LL; + return verify_case(RetVal, ExtendedHappyNumbers().calcTheSum(A, B, K)); } + +template void Run_() { cerr << "Test Case #" << N << "..." << flush; Test_(Case_()); Run_()))==1 ? -1 : N+1>(); } +template<> void Run_<-1>() {} +int main() { Run_<0>(); } +// END CUT HERE + ADDED SRM/334/1C.cpp Index: SRM/334/1C.cpp ================================================================== --- SRM/334/1C.cpp +++ SRM/334/1C.cpp @@ -0,0 +1,137 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +using namespace std; +typedef long long LL; + +typedef int cost; +typedef int vert; +typedef vector< vector > graph; + +cost bidi_minCut( graph G, int N) +{ + vector V; + for(int v=0; v1; --n) + { + // invariant: + // the current set of vertices = {V[0] .. V[n-1]} + + // order the vertices + vector vs; + cost lastCut = 0; + { + vector ws(n, 0); + ws[0] = 1; + + for(int i=0; i roads) + { + int N = roads.size(); + graph G(N, vector(N)); + + for(int i=0; i +double start_time;string timer() { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) << " msec)"; return os.str(); } + +template string print_array(const vector &V) { ostringstream os; os << "{ "; for (typename vector::const_iterator iter = V.begin(); iter != V.end(); ++iter) os << '\"' << *iter << "\","; os << " }"; return os.str(); } +int verify_case(const int &Expected, const int &Received) { if (Expected == Received) cerr << "PASSED" << timer() << endl; else { cerr << "FAILED" << timer() << endl; cerr << "\tExpected: \"" << Expected << '\"' << endl; cerr << "\tReceived: \"" << Received << '\"' << endl; } return 0;} + +template struct Case_ { Case_(){start_time=clock();} }; +char Test_(...); +int Test_(Case_<0>) { + string roads_[] = {"0911", + "9011", + "1109", + "1190"}; + vector roads(roads_, roads_+sizeof(roads_)/sizeof(*roads_)); + int RetVal = 4; + return verify_case(RetVal, Terrorists().requiredCost(roads)); } +int Test_(Case_<1>) { + string roads_[] = {"0399", + "3033", + "9309", + "9390"}; + vector roads(roads_, roads_+sizeof(roads_)/sizeof(*roads_)); + int RetVal = 9; + return verify_case(RetVal, Terrorists().requiredCost(roads)); } +int Test_(Case_<2>) { + string roads_[] = {"030900", + "304120", + "040174", + "911021", + "027207", + "004170"}; + vector roads(roads_, roads_+sizeof(roads_)/sizeof(*roads_)); + int RetVal = 8; + return verify_case(RetVal, Terrorists().requiredCost(roads)); } +int Test_(Case_<3>) { + string roads_[] = {"044967263", + "409134231", + "490642938", + "916036261", + "634306024", + "742660550", + "229205069", + "633625604", + "318140940"}; + vector roads(roads_, roads_+sizeof(roads_)/sizeof(*roads_)); + int RetVal = 27; + return verify_case(RetVal, Terrorists().requiredCost(roads)); } + +template void Run_() { cerr << "Test Case #" << N << "..." << flush; Test_(Case_()); Run_()))==1 ? -1 : N+1>(); } +template<> void Run_<-1>() {} +int main() { Run_<0>(); } +// END CUT HERE + ADDED SRM/335/1A.cpp Index: SRM/335/1A.cpp ================================================================== --- SRM/335/1A.cpp +++ SRM/335/1A.cpp @@ -0,0 +1,81 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +using namespace std; +typedef long long LL; + +class Multifactorial +{ +public: + string calcMultiFact(int n, int k) + { + LL ans = 1; + for(LL v=n; v>0; v-=k) + { + if( ans*v1000000000000000000LL ) + return "overflow"; + ans *= v; + } + stringstream sout; + sout << ans; + return sout.str(); + } +}; + +// 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 string print_array(const vector &V) { ostringstream os; os << "{ "; for (typename vector::const_iterator iter = V.begin(); iter != V.end(); ++iter) os << '\"' << *iter << "\","; os << " }"; return os.str(); } +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;} + +template struct Case_ { Case_(){start_time=clock();} }; +char Test_(...); +int Test_(Case_<0>) { + int n = 14; + int k = 3; + string RetVal = "12320"; + return verify_case(RetVal, Multifactorial().calcMultiFact(n, k)); } +int Test_(Case_<1>) { + int n = 5; + int k = 4; + string RetVal = "5"; + return verify_case(RetVal, Multifactorial().calcMultiFact(n, k)); } +int Test_(Case_<2>) { + int n = 1000; + int k = 2; + string RetVal = "overflow"; + return verify_case(RetVal, Multifactorial().calcMultiFact(n, k)); } +int Test_(Case_<3>) { + int n = 2000000000; + int k = 1900000000; + string RetVal = "200000000000000000"; + return verify_case(RetVal, Multifactorial().calcMultiFact(n, k)); } +int Test_(Case_<4>) { + int n = 1000; + int k = 256; + string RetVal = "84232704000"; + return verify_case(RetVal, Multifactorial().calcMultiFact(n, k)); } +int Test_(Case_<5>) { + int n = 2000000000; + int k = 1; + string RetVal = "overflow"; + return verify_case(RetVal, Multifactorial().calcMultiFact(n, k)); } + +template void Run_() { cerr << "Test Case #" << N << "..." << flush; Test_(Case_()); Run_()))==1 ? -1 : N+1>(); } +template<> void Run_<-1>() {} +int main() { Run_<0>(); } +// END CUT HERE + ADDED SRM/335/1B.cpp Index: SRM/335/1B.cpp ================================================================== --- SRM/335/1B.cpp +++ SRM/335/1B.cpp @@ -0,0 +1,175 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +using namespace std; +typedef long long LL; + +typedef pair vert; +typedef int cost; + +// dijkstra +template +void neighbors(cost e[50][50], int Y, int X, vert v, bool vis[50][50], OutIt out) +{ + typedef pair cedge; + priority_queue< cedge, vector, greater > Q; + Q.push( cedge(e[v.first][v.second],v) ); + + set visited_now; + while( !Q.empty() ) + { + cedge ce = Q.top(); Q.pop(); + cost cc = ce.first; + vert cv = ce.second; + if( visited_now.count(cv) ) + continue; + visited_now.insert(cv); + + if( !vis[cv.first][cv.second] ) { + *out++ = cv; + vis[cv.first][cv.second] = true; + } + + // for 4 neibs + int dy[]={0,0,-1,+1}; + int dx[]={-1,+1,0,0}; + for(int i=0; i<4; ++i) + { + int yyy = cv.first + dy[i]; + int xxx = cv.second + dx[i]; + if( 0<=yyy && yyy Q(1, s); + bool vis[50][50] = {}; vis[s.first][s.second] = true; + + for(int step=0; !Q.empty(); ++step) + { + vector Q2; + for(int i=0; i!=Q.size(); ++i) + { + if( Q[i] == d ) + return step; + neighbors(e, Y, X, Q[i], vis, back_inserter(Q2)); + } + Q.swap(Q2); + } + return -1; +} + +class ExpensiveTravel +{ +public: + int minTime(vector m, int startRow, int startCol, int endRow, int endCol) + { + int Y = m.size(), X = m[0].size(); + int Sy = startRow-1, Sx = startCol-1; + int Ey = endRow-1, Ex = endCol-1; + cost e[50][50]; + 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 string print_array(const vector &V) { ostringstream os; os << "{ "; for (typename vector::const_iterator iter = V.begin(); iter != V.end(); ++iter) os << '\"' << *iter << "\","; os << " }"; return os.str(); } +int verify_case(const int &Expected, const int &Received) { if (Expected == Received) cerr << "PASSED" << timer() << endl; else { cerr << "FAILED" << timer() << endl; cerr << "\tExpected: \"" << Expected << '\"' << endl; cerr << "\tReceived: \"" << Received << '\"' << endl; } return 0;} + +template struct Case_ { Case_(){start_time=clock();} }; +char Test_(...); +int Test_(Case_<0>) { + string m_[] = {"22334"}; + vector m(m_, m_+sizeof(m_)/sizeof(*m_)); + int startRow = 1; + int startCol = 1; + int endRow = 1; + int endCol = 5; + int RetVal = 3; + return verify_case(RetVal, ExpensiveTravel().minTime(m, startRow, startCol, endRow, endCol)); } +int Test_(Case_<1>) { + string m_[] = {"55", + "52", + "55"}; + vector m(m_, m_+sizeof(m_)/sizeof(*m_)); + int startRow = 1; + int startCol = 2; + int endRow = 3; + int endCol = 2; + int RetVal = 1; + return verify_case(RetVal, ExpensiveTravel().minTime(m, startRow, startCol, endRow, endCol)); } +int Test_(Case_<2>) { + string m_[] = {"251", + "212", + "122"}; + vector m(m_, m_+sizeof(m_)/sizeof(*m_)); + int startRow = 1; + int startCol = 1; + int endRow = 3; + int endCol = 3; + int RetVal = -1; + return verify_case(RetVal, ExpensiveTravel().minTime(m, startRow, startCol, endRow, endCol)); } +int Test_(Case_<3>) { + string m_[] = {"452232", + "287979", + "219872", + "928234", + "767676"}; + vector m(m_, m_+sizeof(m_)/sizeof(*m_)); + int startRow = 1; + int startCol = 6; + int endRow = 3; + int endCol = 1; + int RetVal = 3; + return verify_case(RetVal, ExpensiveTravel().minTime(m, startRow, startCol, endRow, endCol)); } +int Test_(Case_<4>) { + string m_[] = {"11"}; + vector m(m_, m_+sizeof(m_)/sizeof(*m_)); + int startRow = 1; + int startCol = 1; + int endRow = 1; + int endCol = 2; + int RetVal = -1; + return verify_case(RetVal, ExpensiveTravel().minTime(m, startRow, startCol, endRow, endCol)); } +int Test_(Case_<5>) { + string m_[] = {"123456789987654321"}; + vector m(m_, m_+sizeof(m_)/sizeof(*m_)); + int startRow = 1; + int startCol = 2; + int endRow = 1; + int endCol = 16; + int RetVal = 5; + return verify_case(RetVal, ExpensiveTravel().minTime(m, startRow, startCol, endRow, endCol)); } + +template void Run_() { cerr << "Test Case #" << N << "..." << flush; Test_(Case_()); Run_()))==1 ? -1 : N+1>(); } +template<> void Run_<-1>() {} +int main() { Run_<0>(); } +// END CUT HERE + ADDED SRM/335/1C.cpp Index: SRM/335/1C.cpp ================================================================== --- SRM/335/1C.cpp +++ SRM/335/1C.cpp @@ -0,0 +1,133 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +using namespace std; +typedef long long LL; + +void gen( vector& X, vector& R, int n ) +{ + int j = 0; + int m = X.size(); + for(int i=0; i Aseed, vector Bseed, int n) + { + vector A(n), B(n); + gen(Aseed, A, n); + gen(Bseed, B, n); + + // expected gain by A[i] = sqSum{B[j] | B[j]A[i]} + + sort(B.begin(), B.end()); + vector B2(n); + for(int i=0; i L1(n), R1(n), L2(n), R2(n); + partial_sum( B.begin(), B.end(), L1.begin() ); + partial_sum( B.rbegin(), B.rend(), R1.rbegin() ); + partial_sum( B2.begin(), B2.end(), L2.begin() ); + partial_sum( B2.rbegin(), B2.rend(), R2.rbegin() ); + + LL e = 0; + for(int i=0; i +double start_time;string timer() { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) << " msec)"; return os.str(); } + +template string print_array(const vector &V) { ostringstream os; os << "{ "; for (typename vector::const_iterator iter = V.begin(); iter != V.end(); ++iter) os << '\"' << *iter << "\","; os << " }"; return os.str(); } +int verify_case(const double &Expected, const double &Received) { double diff = Expected - Received; if (diff < 0) diff = -diff; if (diff < 1e-9) cerr << "PASSED" << timer() << endl; else { cerr << "FAILED" << timer() << endl; cerr << "\tExpected: \"" << Expected << '\"' << endl; cerr << "\tReceived: \"" << Received << '\"' << endl; } return 0;} + +template struct Case_ { Case_(){start_time=clock();} }; +char Test_(...); +int Test_(Case_<0>) { + int A_[] = {6}; + vector A(A_, A_+sizeof(A_)/sizeof(*A_)); + int B_[] = {4}; + vector B(B_, B_+sizeof(B_)/sizeof(*B_)); + int n = 1; + double RetVal = 4.0; + return verify_case(RetVal, RandomFights().expectedNrOfPoints(A, B, n)); } +int Test_(Case_<1>) { + int A_[] = {1,7}; + vector A(A_, A_+sizeof(A_)/sizeof(*A_)); + int B_[] = {3,5}; + vector B(B_, B_+sizeof(B_)/sizeof(*B_)); + int n = 2; + double RetVal = 0.0; + return verify_case(RetVal, RandomFights().expectedNrOfPoints(A, B, n)); } +int Test_(Case_<2>) { + int A_[] = {3,7}; + vector A(A_, A_+sizeof(A_)/sizeof(*A_)); + int B_[] = {1,5}; + vector B(B_, B_+sizeof(B_)/sizeof(*B_)); + int n = 2; + double RetVal = 20.0; + return verify_case(RetVal, RandomFights().expectedNrOfPoints(A, B, n)); } +int Test_(Case_<3>) { + int A_[] = {45,35,236,2342,5543,21,32,2,6,23,24,23,41,1}; + vector A(A_, A_+sizeof(A_)/sizeof(*A_)); + int B_[] = {2345,45,2345,345}; + vector B(B_, B_+sizeof(B_)/sizeof(*B_)); + int n = 50; + double RetVal = 1.2721986164E8; + return verify_case(RetVal, RandomFights().expectedNrOfPoints(A, B, n)); } +int Test_(Case_<4>) { + int A_[] = {34,3245,2534,53,53,46,346,246,346,2,624,624,6,245,6324,6542,624,6}; + vector A(A_, A_+sizeof(A_)/sizeof(*A_)); + int B_[] = {345,234,523,4624,6,2456,345,634,634,7,3457,376,34,6234,62,523,52,35,32}; + vector B(B_, B_+sizeof(B_)/sizeof(*B_)); + int n = 7; + double RetVal = -9713701.714285715; + return verify_case(RetVal, RandomFights().expectedNrOfPoints(A, B, n)); } +int Test_(Case_<5>) { + int A_[] = {1,2,3,4}; + vector A(A_, A_+sizeof(A_)/sizeof(*A_)); + int B_[] = {1,2,3,4}; + vector B(B_, B_+sizeof(B_)/sizeof(*B_)); + int n = 50000; + double RetVal = 0.0; + return verify_case(RetVal, RandomFights().expectedNrOfPoints(A, B, n)); } + +template void Run_() { cerr << "Test Case #" << N << "..." << flush; Test_(Case_()); Run_()))==1 ? -1 : N+1>(); } +template<> void Run_<-1>() {} +int main() { Run_<0>(); } +// END CUT HERE + ADDED SRM/336/1A.cpp Index: SRM/336/1A.cpp ================================================================== --- SRM/336/1A.cpp +++ SRM/336/1A.cpp @@ -0,0 +1,61 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +using namespace std; +typedef long long LL; + +class ServiceNames +{ +public: + vector makeList(vector services) + { + map< string, set > lst; + for(int i=0; i>h; + for(string s; sin>>s; ) + lst[s].insert(h); + } + + vector out; + transform(lst.begin(), lst.end(), back_inserter(out), &pr); + return out; + } + + static string pr( const pair< string, set >& p ) + { + stringstream sout; + sout << p.first; + string sep = " ==> "; + for(set::const_iterator it=p.second.begin(); it!=p.second.end(); ++it) + sout << sep << *it, sep=", "; + return sout.str(); + } + +// BEGIN CUT HERE + public: + void run_test(int Case) { if ((Case == -1) || (Case == 0)) test_case_0(); if ((Case == -1) || (Case == 1)) test_case_1(); if ((Case == -1) || (Case == 2)) test_case_2(); } + private: + template string print_array(const vector &V) { ostringstream os; os << "{ "; for (typename vector::const_iterator iter = V.begin(); iter != V.end(); ++iter) os << '\"' << *iter << "\","; os << " }"; return os.str(); } + void verify_case(int Case, const vector &Expected, const vector &Received) { cerr << "Test Case #" << Case << "..."; if (Expected == Received) cerr << "PASSED" << endl; else { cerr << "FAILED" << endl; cerr << "\tExpected: " << print_array(Expected) << endl; cerr << "\tReceived: " << print_array(Received) << endl; } } + void test_case_0() { string Arr0[] = {"BLAST Genome Annotation Sensitivity","PING","X Annotation"}; vector Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); string Arr1[] = {"Annotation ==> BLAST, X", "Genome ==> BLAST", "Sensitivity ==> BLAST" }; vector Arg1(Arr1, Arr1 + (sizeof(Arr1) / sizeof(Arr1[0]))); verify_case(0, Arg1, makeList(Arg0)); } + void test_case_1() { string Arr0[] = {"PING"}; vector Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); vector Arg1; verify_case(1, Arg1, makeList(Arg0)); } + void test_case_2() { string Arr0[] = {"BLAST Genome annotation Sensitivity","PING","X Annotation","Apple X ample"}; vector Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); string Arr1[] = {"Annotation ==> X", "Genome ==> BLAST", "Sensitivity ==> BLAST", "X ==> Apple", "ample ==> Apple", "annotation ==> BLAST" }; vector Arg1(Arr1, Arr1 + (sizeof(Arr1) / sizeof(Arr1[0]))); verify_case(2, Arg1, makeList(Arg0)); } + +// END CUT HERE +}; +// BEGIN CUT HERE +int main() { ServiceNames().run_test(-1); } +// END CUT HERE ADDED SRM/336/1B.cpp Index: SRM/336/1B.cpp ================================================================== --- SRM/336/1B.cpp +++ SRM/336/1B.cpp @@ -0,0 +1,95 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +using namespace std; +typedef long long LL; + +class LikelyWord +{ +public: + int likely(vector dictionary, int k) + { + vector dl, dr; + dl.push_back(-1); + dr.push_back(-1); + for(int i=0; imax ) + max=hm, maxi=i; + else if(hm==max) + maxi=-1; + } + return maxi; + } + + LL toLLl( string s, LL k ) + { + if( s.size() > k ) + s.resize(k); + else if( s.size() < k ) + s.resize(k, 'a'), s[s.size()-1]-=1; + return toLL(s,k); + } + + LL toLLr( string s, LL k ) + { + if( s.size() > k ) + s.resize(k), s[s.size()-1]+=1; + else if( s.size() < k ) + s.resize(k, 'a'); + return toLL(s,k); + } + + LL toLL( string s, LL k ) + { + LL n = 0; + for(int i=0; i string print_array(const vector &V) { ostringstream os; os << "{ "; for (typename vector::const_iterator iter = V.begin(); iter != V.end(); ++iter) os << '\"' << *iter << "\","; os << " }"; return os.str(); } + void verify_case(int Case, const int &Expected, const int &Received) { cerr << "Test Case #" << Case << "..."; if (Expected == Received) cerr << "PASSED" << endl; else { cerr << "FAILED" << endl; cerr << "\tExpected: \"" << Expected << '\"' << endl; cerr << "\tReceived: \"" << Received << '\"' << endl; } } + void test_case_0() { string Arr0[] = {"time","zoology"}; vector Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); int Arg1 = 1; int Arg2 = 0; verify_case(0, Arg2, likely(Arg0, Arg1)); } + void test_case_1() { string Arr0[] = {"az","ma","xz"}; vector Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); int Arg1 = 1; int Arg2 = 1; verify_case(1, Arg2, likely(Arg0, Arg1)); } + void test_case_2() { string Arr0[] = {"az","ma","xz"}; vector Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); int Arg1 = 2; int Arg2 = 2; verify_case(2, Arg2, likely(Arg0, Arg1)); } + void test_case_3() { string Arr0[] = {"a","m","y"}; vector Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); int Arg1 = 1; int Arg2 = -1; verify_case(3, Arg2, likely(Arg0, Arg1)); } + void test_case_4() { string Arr0[] = {"a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y"}; vector Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); int Arg1 = 1; int Arg2 = 25; verify_case(4, Arg2, likely(Arg0, Arg1)); } + void test_case_5() { string Arr0[] = {"aa", "ab", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"}; vector Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); int Arg1 = 1; int Arg2 = 0; verify_case(5, Arg2, likely(Arg0, Arg1)); } + +// END CUT HERE +}; +// BEGIN CUT HERE +int main() { LikelyWord().run_test(-1); } +// END CUT HERE ADDED SRM/336/1C.cpp Index: SRM/336/1C.cpp ================================================================== --- SRM/336/1C.cpp +++ SRM/336/1C.cpp @@ -0,0 +1,134 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +using namespace std; +typedef long long LL; + +typedef complex pt; + +bool byY( pt a, pt b ) +{ + if( a.imag() == b.imag() ) + return a.real() < b.real(); + return a.imag() < b.imag(); +} + +bool byArg( pt a, pt b ) +{ + return arg(a) < arg(b); +} + +bool isRight( pt a, pt b, pt c ) +{ + return arg((c-b) / (b-a)) < 0; +} + +vector convex_hull( vector p ) +{ + vector::iterator it = min_element( p.begin(), p.end(), byY ); + pt o = *it; + p.erase(it); + for(int i=0; i q; + q.push_back( pt(0,0) ); + q.push_back( p[0] ); + for(int i=1; i& q ) +{ + double a = 0.0; + + pt o = q[0]; + for(int i=1; i+1 tree, vector light) + { + double x1 = min(tree[0], tree[3]); + double x2 = max(tree[0], tree[3]); + double y1 = min(tree[1], tree[4]); + double y2 = max(tree[1], tree[4]); + double z1 = min(tree[2], tree[5]); + double z2 = max(tree[2], tree[5]); + double x = light[0]; + double y = light[1]; + double z = light[2]; + + if(x1==x2 && y1==y2 || y1==y2 && z1==z2 || z1==z2 && x1==x2 ) + return 0; // line + if(x1==x2 && x==x1 || y1==y2 && y==y1 || z1==z2 && z==z1 ) + return 0; // seen as a line + if( y1 >= y ) + return 0; // tree above light + if( y2 >= y ) + return -1; // infinite + + // otherwise project all vertices two y=0 plain + vector p; + for(int i=0; i<8; ++i) + { + double X=(i&1 ? x1 : x2); + double Y=(i&2 ? y1 : y2); + double Z=(i&4 ? z1 : z2); + + // (Xp-X) : (x-X) = (0-Y) : (y-Y) + double Xp = (X-x)*Y/(y-Y) + X; + double Zp = (Z-z)*Y/(y-Y) + Z; + p.push_back( pt(Xp,Zp) ); + } + + // calc convex hull and its area + p = convex_hull(p); + return ::area(p); + } + +// BEGIN CUT HERE + public: + void run_test(int Case) { if ((Case == -1) || (Case == 0)) test_case_0(); if ((Case == -1) || (Case == 1)) test_case_1(); if ((Case == -1) || (Case == 2)) test_case_2(); if ((Case == -1) || (Case == 3)) test_case_3(); } + private: + template string print_array(const vector &V) { ostringstream os; os << "{ "; for (typename vector::const_iterator iter = V.begin(); iter != V.end(); ++iter) os << '\"' << *iter << "\","; os << " }"; return os.str(); } + void verify_case(int Case, const double &Expected, const double &Received) { cerr << "Test Case #" << Case << "..."; if (Expected == Received) cerr << "PASSED" << endl; else { cerr << "FAILED" << endl; cerr << "\tExpected: \"" << Expected << '\"' << endl; cerr << "\tReceived: \"" << Received << '\"' << endl; } } + void test_case_0() { int Arr0[] = {1,1,1, 10,1,1}; vector Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); int Arr1[] = {5,5,5}; vector Arg1(Arr1, Arr1 + (sizeof(Arr1) / sizeof(Arr1[0]))); double Arg2 = 0.0; verify_case(0, Arg2, area(Arg0, Arg1)); } + void test_case_1() { int Arr0[] = {1,3,1, 10,1,1}; vector Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); int Arr1[] = {2,2,2}; vector Arg1(Arr1, Arr1 + (sizeof(Arr1) / sizeof(Arr1[0]))); double Arg2 = -1.0; verify_case(1, Arg2, area(Arg0, Arg1)); } + void test_case_2() { int Arr0[] = {1,1,1, 2,2,2}; vector Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); int Arr1[] = {3,3,3}; vector Arg1(Arr1, Arr1 + (sizeof(Arr1) / sizeof(Arr1[0]))); double Arg2 = 15.75; verify_case(2, Arg2, area(Arg0, Arg1)); } + void test_case_3() { int Arr0[] = {1,1,1, 3,3,3} ; vector Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); int Arr1[] = {2,2,2}; vector Arg1(Arr1, Arr1 + (sizeof(Arr1) / sizeof(Arr1[0]))); double Arg2 = -1.0; verify_case(3, Arg2, area(Arg0, Arg1)); } + +// END CUT HERE +}; +// BEGIN CUT HERE +int main() { Shadow().run_test(-1); } +// END CUT HERE ADDED SRM/337/1A.cpp Index: SRM/337/1A.cpp ================================================================== --- SRM/337/1A.cpp +++ SRM/337/1A.cpp @@ -0,0 +1,67 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +using namespace std; +typedef long long LL; + +class CardStraights +{ +public: + int longestStraight(vector cards) + { + int jok = count(cards.begin(), cards.end(), 0); + + set cs(cards.begin(), cards.end()); + cs.erase(0); + if( cs.empty() ) + return jok; // Ouggggggggg + + int ans = 0; + for(set::iterator it=cs.begin(); it!=cs.end(); ++it) + ans = max(ans, asLongAsPossible(it, cs.end(), jok)); + return ans; + } + + int asLongAsPossible(set::iterator it, set::iterator end, int jok) + { + int b = *it; + int e = *it; + for(++it; it!=end; e=*it++) + if( *it-e-1 <= jok ) + jok -= *it-e-1; + else + break; + int nAfter = min(1000000-e, jok); + e += nAfter; + b -= min(b-1, jok-nAfter); + return e-b+1; + } + +// BEGIN CUT HERE + public: + void run_test(int Case) { if ((Case == -1) || (Case == 0)) test_case_0(); if ((Case == -1) || (Case == 1)) test_case_1(); if ((Case == -1) || (Case == 2)) test_case_2(); if ((Case == -1) || (Case == 3)) test_case_3(); if ((Case == -1) || (Case == 4)) test_case_4(); } + private: + template string print_array(const vector &V) { ostringstream os; os << "{ "; for (typename vector::const_iterator iter = V.begin(); iter != V.end(); ++iter) os << '\"' << *iter << "\","; os << " }"; return os.str(); } + void verify_case(int Case, const int &Expected, const int &Received) { cerr << "Test Case #" << Case << "..."; if (Expected == Received) cerr << "PASSED" << endl; else { cerr << "FAILED" << endl; cerr << "\tExpected: \"" << Expected << '\"' << endl; cerr << "\tReceived: \"" << Received << '\"' << endl; } } + void test_case_0() { int Arr0[] = {0,6,5,10,3,0,11}; vector Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); int Arg1 = 5; verify_case(0, Arg1, longestStraight(Arg0)); } + void test_case_1() { int Arr0[] = {100,100,100,101,100,99,97,103}; vector Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); int Arg1 = 3; verify_case(1, Arg1, longestStraight(Arg0)); } + void test_case_2() { int Arr0[] = {0,0,0,1,2,6,8,1000}; vector Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); int Arg1 = 6; verify_case(2, Arg1, longestStraight(Arg0)); } + void test_case_3() { int Arr0[] = {1,9,5,7,3,4,0,0,0,10}; vector Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); int Arg1 = 10; verify_case(3, Arg1, longestStraight(Arg0)); } + void test_case_4() { int Arr0[] = {0,0,999999}; vector Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); int Arg1 = 3; verify_case(4, Arg1, longestStraight(Arg0)); } + +// END CUT HERE +}; +// BEGIN CUT HERE +int main() { CardStraights().run_test(-1); } +// END CUT HERE ADDED SRM/337/1B+.cpp Index: SRM/337/1B+.cpp ================================================================== --- SRM/337/1B+.cpp +++ SRM/337/1B+.cpp @@ -0,0 +1,104 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +using namespace std; +typedef long long LL; + +template +struct RMQ +{ + vector< vector > rm; + vector d; + + RMQ( const vector& d ) : d(d) { + int n = d.size(); + + // rm[k][x] = z s.t. d[z] is the minimum in [x, x+2^k) + rm.push_back( vector(n) ); + for(int x=0; x d[rm[k-1][x + (1< hseed, int n) + { + // input + LL j = 0; + LL m = hseed.size(); + vector h(n); + for(int i=0; i(h), h, 0, n-1); + } + LL rec(const RMQ& rmq, vector& h, LL L, LL R) + { + LL result = 0; + + stack< pair > S; + S.push( make_pair(L,R) ); + while( !S.empty() ) + { + L = S.top().first; + R = S.top().second; + S.pop(); + + LL C = rmq(L, R); + if( L < C ) S.push( make_pair(L,C-1) ); + if( C < R ) S.push( make_pair(C+1,R) ); + result = max(result, (R-L+1) * h[C]); + } + + return result; + } + +// BEGIN CUT HERE + public: + void run_test(int Case) { if ((Case == -1) || (Case == 0)) test_case_0(); if ((Case == -1) || (Case == 1)) test_case_1(); if ((Case == -1) || (Case == 2)) test_case_2(); if ((Case == -1) || (Case == 3)) test_case_3(); } + private: + template string print_array(const vector &V) { ostringstream os; os << "{ "; for (typename vector::const_iterator iter = V.begin(); iter != V.end(); ++iter) os << '\"' << *iter << "\","; os << " }"; return os.str(); } + void verify_case(int Case, const long long &Expected, const long long &Received) { cerr << "Test Case #" << Case << "..."; if (Expected == Received) cerr << "PASSED" << endl; else { cerr << "FAILED" << endl; cerr << "\tExpected: \"" << Expected << '\"' << endl; cerr << "\tReceived: \"" << Received << '\"' << endl; } } + void test_case_0() { int Arr0[] = {3,6,5,6,2,4}; vector Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); int Arg1 = 6; long long Arg2 = 15LL; verify_case(0, Arg2, getMaxArea(Arg0, Arg1)); } + void test_case_1() { int Arr0[] = {5,0,7,0,2,6,2}; vector Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); int Arg1 = 7; long long Arg2 = 7LL; verify_case(1, Arg2, getMaxArea(Arg0, Arg1)); } + void test_case_2() { int Arr0[] = {1048589,2097165}; vector Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); int Arg1 = 100000; long long Arg2 = 104858900000LL; verify_case(2, Arg2, getMaxArea(Arg0, Arg1)); } + void test_case_3() { int Arr0[] = {1,7,2,5,3,1}; vector Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); int Arg1 = 6; long long Arg2 = 8LL; verify_case(3, Arg2, getMaxArea(Arg0, Arg1)); } + +// END CUT HERE +}; +// BEGIN CUT HERE +int main() { BuildingAdvertise().run_test(-1); } +// END CUT HERE ADDED SRM/337/1B.cpp Index: SRM/337/1B.cpp ================================================================== --- SRM/337/1B.cpp +++ SRM/337/1B.cpp @@ -0,0 +1,81 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +using namespace std; +typedef long long LL; + +class BuildingAdvertise +{ +public: + long long getMaxArea(vector h, int n) + { + // input + LL j = 0; + LL m = h.size(); + vector R(n); + for(int i=0; i left(n); + { + map h; h[-1] = -1; + for(int i=0; i::iterator it = h.lower_bound(R[i]); + left[i] = (--it)->second+1; + h.erase( ++it, h.end() ); + h[ R[i] ] = i; + } + } + vector right(n); + { + map h; h[-1] = n; + for(int i=n-1; i>=0; --i) { + // position of the highest building < R[i] + map::iterator it = h.lower_bound(R[i]); + right[i] = (--it)->second-1; + h.erase( ++it, h.end() ); + h[ R[i] ] = i; + } + } + LL ans = 0; + for(int i=0; i string print_array(const vector &V) { ostringstream os; os << "{ "; for (typename vector::const_iterator iter = V.begin(); iter != V.end(); ++iter) os << '\"' << *iter << "\","; os << " }"; return os.str(); } + void verify_case(int Case, const long long &Expected, const long long &Received) { cerr << "Test Case #" << Case << "..."; if (Expected == Received) cerr << "PASSED" << endl; else { cerr << "FAILED" << endl; cerr << "\tExpected: \"" << Expected << '\"' << endl; cerr << "\tReceived: \"" << Received << '\"' << endl; } } + void test_case_0() { int Arr0[] = {3,6,5,6,2,4}; vector Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); int Arg1 = 6; long long Arg2 = 15LL; verify_case(0, Arg2, getMaxArea(Arg0, Arg1)); } + void test_case_1() { int Arr0[] = {5,0,7,0,2,6,2}; vector Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); int Arg1 = 7; long long Arg2 = 7LL; verify_case(1, Arg2, getMaxArea(Arg0, Arg1)); } + void test_case_2() { int Arr0[] = {1048589,2097165}; vector Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); int Arg1 = 100000; long long Arg2 = 104858900000LL; verify_case(2, Arg2, getMaxArea(Arg0, Arg1)); } + void test_case_3() { int Arr0[] = {1,7,2,5,3,1}; vector Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); int Arg1 = 6; long long Arg2 = 8LL; verify_case(3, Arg2, getMaxArea(Arg0, Arg1)); } + void test_case_4() { int Arr0[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12}; vector Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); int Arg1 = 9999; long long Arg2 = 1903656LL; verify_case(4, Arg2, getMaxArea(Arg0, Arg1)); } +// END CUT HERE +}; +// BEGIN CUT HERE +int main() { BuildingAdvertise().run_test(-1); } +// END CUT HERE ADDED SRM/337/1C.cpp Index: SRM/337/1C.cpp ================================================================== --- SRM/337/1C.cpp +++ SRM/337/1C.cpp @@ -0,0 +1,106 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +using namespace std; +typedef long long LL; + +static const LL MODVAL = 835454957; +int memo[50*15*101*2]; +int memo_k[101]; + +class CountPalindromes +{ +public: + vector w; + int count(vector words, int k) + { + w = words; + memset(memo, 0xff, sizeof(memo)); + + LL ans = 0; + memo_k[0] = 0; + for(int kk=1; kk<=k; ++kk) { + LL ak = 0; + for(int i=0; i=0 ) + return memo[key]; + + LL ans = 0; + for(int j=0; j=0 ) + return memo[key]; + + LL ans = 0; + for(int i=0; i=2 && ix string print_array(const vector &V) { ostringstream os; os << "{ "; for (typename vector::const_iterator iter = V.begin(); iter != V.end(); ++iter) os << '\"' << *iter << "\","; os << " }"; return os.str(); } + void verify_case(int Case, const int &Expected, const int &Received) { cerr << "Test Case #" << Case << "..."; if (Expected == Received) cerr << "PASSED" << endl; else { cerr << "FAILED" << endl; cerr << "\tExpected: \"" << Expected << '\"' << endl; cerr << "\tReceived: \"" << Received << '\"' << endl; } } + void test_case_0() { string Arr0[] = {"tragic","cigar"}; vector Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); int Arg1 = 24; int Arg2 = 1; verify_case(0, Arg2, count(Arg0, Arg1)); } + void test_case_1() { string Arr0[] = {"z","zz"}; vector Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); int Arg1 = 4; int Arg2 = 5; verify_case(1, Arg2, count(Arg0, Arg1)); } + void test_case_2() { string Arr0[] = {"aba","acaba","baca","cac","b","c","a"}; vector Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); int Arg1 = 70; int Arg2 = 370786966; verify_case(2, Arg2, count(Arg0, Arg1)); } + void test_case_3() { string Arr0[] = {"hello"}; vector Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); int Arg1 = 100; int Arg2 = 0; verify_case(3, Arg2, count(Arg0, Arg1)); } + void test_case_4() { string Arr0[] = {"a", "aaaaaaaaab", "aaa", "aaaaaaaaaaaa", "aabbaaa", "babaaaaaaaa", "aaaaaabaaaba", "aaaa", "aaaaaaaaa", "aaaaaaaaaa", "aab", "aaaaaababaaaaa", "aaaaaaaaaaabba", "aaaaaabaa", "aaabaaaaaa", "abaaaaaaaa", "aaaaaa", "aabaaaaaaaaaba", "ab", "bbbaabaaaab", "aaaabaaaaa", "aa", "baaaaaaaaaaaa", "aabaa", "aabaaaaaaaabbaa", "aaaaaaaabaaaaaa", "aaaaaaaa", "aaaaa", "aaaaaaaaaaa", "aabaaaaa", "baabaaaaaaaaa", "baaaaa", "aaaaaaaabaaaaa", "aaaaaaaaaaaaaaa", "abaaaaaaaaaaa", "aaabababaaaa", "aaaaaaaaaabaaaa", "abaaa", "aaaaabaaaab", "aaaabaaaaaaa", "aaaaaaaba", "babbbaaaaaba", "aaaaaaaaaaaaa", "aaababaabaab", "aaabaabaaab", "aaaabbaaabaaaaa", "abaaaaaaaaaaaaa", "aaaaaaa", "abaaaaabaaaaaaa", "aaabaaaaaaaaa"}; vector Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); int Arg1 = 100; int Arg2 = 570277357; verify_case(4, Arg2, count(Arg0, Arg1)); } +// END CUT HERE +}; +// BEGIN CUT HERE +int main() { CountPalindromes().run_test(-1); } +// END CUT HERE ADDED SRM/338/1A.cpp Index: SRM/338/1A.cpp ================================================================== --- SRM/338/1A.cpp +++ SRM/338/1A.cpp @@ -0,0 +1,52 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +using namespace std; +typedef long long LL; + +class ImprovingStatistics +{ +public: + int howManyGames(int played, int won) + { + LL p = played, w = won; + LL z = w*100 / p; + + // z+1 <= (w+X)*100 / (p+X) + // (z+1)(p+X) <= (w+X)*100 + // (z+1)*p-w*100 <= (99-z)X + if( z >= 99 ) + return -1; + LL a = (z+1)*p-w*100, b = 99-z; + // a <= b*X + return int(a%b==0 ? a/b : a/b+1); + } + +// BEGIN CUT HERE + public: + void run_test(int Case) { if ((Case == -1) || (Case == 0)) test_case_0(); if ((Case == -1) || (Case == 1)) test_case_1(); if ((Case == -1) || (Case == 2)) test_case_2(); if ((Case == -1) || (Case == 3)) test_case_3(); if ((Case == -1) || (Case == 4)) test_case_4(); } + private: + template string print_array(const vector &V) { ostringstream os; os << "{ "; for (typename vector::const_iterator iter = V.begin(); iter != V.end(); ++iter) os << '\"' << *iter << "\","; os << " }"; return os.str(); } + void verify_case(int Case, const int &Expected, const int &Received) { cerr << "Test Case #" << Case << "..."; if (Expected == Received) cerr << "PASSED" << endl; else { cerr << "FAILED" << endl; cerr << "\tExpected: \"" << Expected << '\"' << endl; cerr << "\tReceived: \"" << Received << '\"' << endl; } } + void test_case_0() { int Arg0 = 10; int Arg1 = 8; int Arg2 = 1; verify_case(0, Arg2, howManyGames(Arg0, Arg1)); } + void test_case_1() { int Arg0 = 100; int Arg1 = 80; int Arg2 = 6; verify_case(1, Arg2, howManyGames(Arg0, Arg1)); } + void test_case_2() { int Arg0 = 47; int Arg1 = 47; int Arg2 = -1; verify_case(2, Arg2, howManyGames(Arg0, Arg1)); } + void test_case_3() { int Arg0 = 99000; int Arg1 = 0; int Arg2 = 1000; verify_case(3, Arg2, howManyGames(Arg0, Arg1)); } + void test_case_4() { int Arg0 = 1000000000; int Arg1 = 470000000; int Arg2 = 19230770; verify_case(4, Arg2, howManyGames(Arg0, Arg1)); } + +// END CUT HERE +}; +// BEGIN CUT HERE +int main() { ImprovingStatistics().run_test(-1); } +// END CUT HERE ADDED SRM/338/1B.cpp Index: SRM/338/1B.cpp ================================================================== --- SRM/338/1B.cpp +++ SRM/338/1B.cpp @@ -0,0 +1,88 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +using namespace std; +typedef long long LL; + +vector< vector > mMul( const vector< vector >& A, const vector< vector >& B ) +{ + const int n = A.size(); + + vector< vector > C(n, vector(n)); + for(int i=0; i > mPow( vector< vector > M, int t ) // t>0 +{ + vector< vector > R; + for(; t; t>>=1, M=mMul(M,M)) + if( t&1 ) + R = (R.empty() ? M : mMul(R,M)); + return R; +} + +class RandomSwaps +{ +public: + double getProbability(int arrayLength, int swapCount, int a, int b) + { + double P = 1 / double(arrayLength * (arrayLength-1)/2); + double Q = 1 - (arrayLength-1)*P; + if( a != b ) { + vector< vector > M(3, vector(3)); + M[0][0] = Q; + M[0][1] = P; + M[0][2] = 1 - P - Q; + M[1][0] = P; + M[1][1] = Q; + M[1][2] = 1 - P - Q; + M[2][0] = P; + M[2][1] = P; + M[2][2] = 1 - M[2][0] - M[2][1]; + M = mPow( M, swapCount ); + return M[0][1]; + } else { + vector< vector > M(2, vector(2)); + M[0][0] = Q; + M[0][1] = 1 - M[0][0]; + M[1][0] = P; + M[1][1] = 1 - P; + M = mPow( M, swapCount ); + return M[0][0]; + } + } + +// BEGIN CUT HERE + public: + void run_test(int Case) { if ((Case == -1) || (Case == 0)) test_case_0(); if ((Case == -1) || (Case == 1)) test_case_1(); if ((Case == -1) || (Case == 2)) test_case_2(); if ((Case == -1) || (Case == 3)) test_case_3(); } + private: + template string print_array(const vector &V) { ostringstream os; os << "{ "; for (typename vector::const_iterator iter = V.begin(); iter != V.end(); ++iter) os << '\"' << *iter << "\","; os << " }"; return os.str(); } + void verify_case(int Case, const double &Expected, const double &Received) { cerr << "Test Case #" << Case << "..."; if (Expected == Received) cerr << "PASSED" << endl; else { cerr << "FAILED" << endl; cerr << "\tExpected: \"" << Expected << '\"' << endl; cerr << "\tReceived: \"" << Received << '\"' << endl; } } + void test_case_0() { int Arg0 = 5; int Arg1 = 1; int Arg2 = 0; int Arg3 = 0; double Arg4 = 0.6; verify_case(0, Arg4, getProbability(Arg0, Arg1, Arg2, Arg3)); } + void test_case_1() { int Arg0 = 5; int Arg1 = 1; int Arg2 = 0; int Arg3 = 3; double Arg4 = 0.1; verify_case(1, Arg4, getProbability(Arg0, Arg1, Arg2, Arg3)); } + void test_case_2() { int Arg0 = 5; int Arg1 = 2; int Arg2 = 0; int Arg3 = 0; double Arg4 = 0.4; verify_case(2, Arg4, getProbability(Arg0, Arg1, Arg2, Arg3)); } + void test_case_3() { int Arg0 = 100; int Arg1 = 500; int Arg2 = 3; int Arg3 = 3; double Arg4 = 0.010036635745123007; verify_case(3, Arg4, getProbability(Arg0, Arg1, Arg2, Arg3)); } + +// END CUT HERE +}; +// BEGIN CUT HERE +int main() { RandomSwaps().run_test(-1); } +// END CUT HERE ADDED SRM/338/1C.cpp Index: SRM/338/1C.cpp ================================================================== --- SRM/338/1C.cpp +++ SRM/338/1C.cpp @@ -0,0 +1,172 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +using namespace std; +typedef long long LL; + +class CakeParty +{ +public: + string makeMove(vector pieces) + { + // Win iff #{i | pieces[i]==max(pieces)} is odd + // --> if #==1 move not to make the opponent winning pos + // if #==odd>=3 any move will do. choose the lexic-first one + // if #==even any move leads to lose. choose the lex1st +/* +<> + +定理のステートメント + + 対象となるゲーム + - 二人ゲーム + - 二人とも動かせる手の集合は同じ + - 動かせなくなった方が負け + - 無限ループしない + + nim + - サイズ s1, s2, .., sk の山がある + - どれか一つ山を選んで 1 〜 山サイズ の任意個取り除ける + - 最後の山を取った方が勝ち(山がなくなって打つ手が無くなった方が負け) + + *n をサイズ n の山が1個だけの nim とする + - *0 は負け + - *n は勝ち n>=1 + + ゲーム + - 状態 G から打てる手で遷移する先を G1, ..., Gk とする + G = {G1, ..., Gk} と書く + + 等価 + - 二つのゲームG, Fが等価なことを G ≡ F と書く + 等価というのは勝ち負け一致よりもっと強い。二つのゲームの和を取ると必敗になること + *n と *n は同サイズの山2つのnimは必敗なので等価。同サイズでなければ必勝なので等価でない + + 定理 + - G1≡*n1, ..., Gk≡*nk ならば、G={G1, ..., Gk} ≡ *mex(n1,...,nk) + where mex(S) = min(nat \setminus S) + 帰納的に、全てのゲームはなんらかの *n と等価 + +適用 + + "#max == 1 なら必勝" + [X,...] = { [X-1,...], [X-2,...], ..., [1, ...], [...] } + で + [X-1,...] = { [X-2,...], ..., [1, ...], [...] } + なので + [X,...] の nim 値は mex(mex{[X-2,...], ..., [1, ...], [...]}, [X-2,...], ..., [1, ...], [...]) + になるが、これはmex2回がけなので *0 ではない。よって必勝 + "#max == 2 なら必敗" + どうやっても相手に#max==1で渡ってしまう + "#max == 3 なら必勝" + どうやっても相手に#max==2で渡る + ... + +*/ + LL maxI = max_element(pieces.begin(), pieces.end()) - pieces.begin(); + LL maxP = pieces[maxI]; + if( count(pieces.begin(), pieces.end(), maxP) == 1 ) + { + pieces[maxI] = 0; + LL second = *max_element(pieces.begin(), pieces.end()); + + // if there's odd number of the second largests, make it even + if( count(pieces.begin(), pieces.end(), second)%2 == 1 ) + { + stringstream sout; + sout << "CAKE " << maxI << " PIECES " << (maxP - second); + return sout.str(); + } + // otherwise, leave it as is. choose the lex-first move + else + { + LL eat = maxP - second + 1; + + // d : lexicographically first number more than "eat" + LL d = 1; + while( !(eat <= d) ) + d *= 10; + if( d <= maxP ) // if edible + eat = d; + stringstream sout; + sout << "CAKE " << maxI << " PIECES " << eat; + return sout.str(); + } + } + else + { + string ans = "Z"; + for(LL i=0; i +double start_time;string timer() { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) << " msec)"; return os.str(); } + +template string print_array(const vector &V) { ostringstream os; os << "{ "; for (typename vector::const_iterator iter = V.begin(); iter != V.end(); ++iter) os << '\"' << *iter << "\","; os << " }"; return os.str(); } +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;} + +template struct Case_ { Case_(){start_time=clock();} }; +char Test_(...); +int Test_(Case_<0>) { + int pieces_[] = {47}; + vector pieces(pieces_, pieces_+sizeof(pieces_)/sizeof(*pieces_)); + string RetVal = "CAKE 0 PIECES 47"; + return verify_case(RetVal, CakeParty().makeMove(pieces)); } +int Test_(Case_<1>) { + int pieces_[] = {3,3}; + vector pieces(pieces_, pieces_+sizeof(pieces_)/sizeof(*pieces_)); + string RetVal = "CAKE 0 PIECES 1"; + return verify_case(RetVal, CakeParty().makeMove(pieces)); } +int Test_(Case_<2>) { + int pieces_[] = {3,5}; + vector pieces(pieces_, pieces_+sizeof(pieces_)/sizeof(*pieces_)); + string RetVal = "CAKE 1 PIECES 2"; + return verify_case(RetVal, CakeParty().makeMove(pieces)); } +int Test_(Case_<3>) { + int pieces_[] = {1,1,1,1,1,1,1,1,1,1,1,1,1,1}; + vector pieces(pieces_, pieces_+sizeof(pieces_)/sizeof(*pieces_)); + string RetVal = "CAKE 0 PIECES 1"; + return verify_case(RetVal, CakeParty().makeMove(pieces)); } +int Test_(Case_<4>) { + int pieces_[] = {3,3,112}; + vector pieces(pieces_, pieces_+sizeof(pieces_)/sizeof(*pieces_)); + string RetVal = "CAKE 2 PIECES 110"; + return verify_case(RetVal, CakeParty().makeMove(pieces)); } +int Test_(Case_<5>) { + int pieces_[] = {3,3,1}; + vector pieces(pieces_, pieces_+sizeof(pieces_)/sizeof(*pieces_)); + string RetVal = "CAKE 0 PIECES 1"; + return verify_case(RetVal, CakeParty().makeMove(pieces)); } +int Test_(Case_<6>) { + int pieces_[] = {4,7,4,7,4,7,4,7,47,47,47,47}; + vector pieces(pieces_, pieces_+sizeof(pieces_)/sizeof(*pieces_)); + string RetVal = "CAKE 10 PIECES 1"; + return verify_case(RetVal, CakeParty().makeMove(pieces)); } + +template void Run_() { cerr << "Test Case #" << N << "..." << flush; Test_(Case_()); Run_()))==1 ? -1 : N+1>(); } +template<> void Run_<-1>() {} +int main() { Run_<0>(); } +// END CUT HERE + ADDED SRM/339/1A.cpp Index: SRM/339/1A.cpp ================================================================== --- SRM/339/1A.cpp +++ SRM/339/1A.cpp @@ -0,0 +1,86 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +using namespace std; +typedef long long LL; + +class BusTrip +{ +public: + int returnTime(int, vector buses) + { + int t=-1, v=0; + do + onestep( t, v, buses ); + while( v ); + return t; + } + + void onestep(int& t, int& v, vector& buses) + { + int mt=1001, mv=0; + for(int j=0; j vs; + for(int x; sin>>x;) + vs.push_back(x); + + int loop_time = 0; + for(int i=0; i= nt ) + nt += loop_time; + if( nt < mt ) { + mt = nt; + mv = vs[(i+1)%vs.size()]; + } + } + rem += abs(vs[(i+1)%vs.size()] - vs[i]); + } + } + + t = mt + abs(mv-v); + v = mv; + if( t > 1000 ) + t=-1, v=0; + } + +// BEGIN CUT HERE + public: + void run_test(int Case) { if ((Case == -1) || (Case == 0)) test_case_0(); if ((Case == -1) || (Case == 1)) test_case_1(); if ((Case == -1) || (Case == 2)) test_case_2(); if ((Case == -1) || (Case == 3)) test_case_3(); if ((Case == -1) || (Case == 4)) test_case_4(); if ((Case == -1) || (Case == 5)) test_case_5(); } + private: + template string print_array(const vector &V) { ostringstream os; os << "{ "; for (typename vector::const_iterator iter = V.begin(); iter != V.end(); ++iter) os << '\"' << *iter << "\","; os << " }"; return os.str(); } + void verify_case(int Case, const int &Expected, const int &Received) { cerr << "Test Case #" << Case << "..."; if (Expected == Received) cerr << "PASSED" << endl; else { cerr << "FAILED" << endl; cerr << "\tExpected: \"" << Expected << '\"' << endl; cerr << "\tReceived: \"" << Received << '\"' << endl; } } + void test_case_0() { int Arg0 = 3; string Arr1[] = {"0 1 2"}; vector Arg1(Arr1, Arr1 + (sizeof(Arr1) / sizeof(Arr1[0]))); int Arg2 = 12; verify_case(0, Arg2, returnTime(Arg0, Arg1)); } + void test_case_1() { int Arg0 = 51; string Arr1[] = {"0 5 10 15 20 25 30 35 40 50"}; vector Arg1(Arr1, Arr1 + (sizeof(Arr1) / sizeof(Arr1[0]))); int Arg2 = 1000; verify_case(1, Arg2, returnTime(Arg0, Arg1)); } + void test_case_2() { int Arg0 = 3; string Arr1[] = {"0 1 2", "2 1 0"}; vector Arg1(Arr1, Arr1 + (sizeof(Arr1) / sizeof(Arr1[0]))); int Arg2 = -1; verify_case(2, Arg2, returnTime(Arg0, Arg1)); } + void test_case_3() { int Arg0 = 5; string Arr1[] = {"0 1 2 3 4", "3 1 2 0", "4 1 2 3 0", "1 2 0 3 4", "4 0"}; vector Arg1(Arr1, Arr1 + (sizeof(Arr1) / sizeof(Arr1[0]))); int Arg2 = 12; verify_case(3, Arg2, returnTime(Arg0, Arg1)); } + void test_case_4() { int Arg0 = 25; string Arr1[] = {"24 14 9 7 2", "21 4 18 24 7 1 2 11 8 9 14 16 5 17 13 23 19 15 22", "12 22 24 9 1 5 10 8 7 18 16 19 4 13 17", + "14 5 17 9 23 7 16 22 10 4 6", "19 8 1 9 24 3 5 22 16 7 6 4 10 23 17 0 13 15", + "2 16 10 13 14 1 11 20 0 24 22 23 19 4 18", "19 15 18 0", "15 9 22 5 20 8 23 14 24 18 21 6 13 19", + "2 6 19 3 21 10 20 22 24 13 16 15 8 18 17 14 5", "19 10 1 7 5 11 21 8 14 0 17 23 12 2 3 16"}; vector Arg1(Arr1, Arr1 + (sizeof(Arr1) / sizeof(Arr1[0]))); int Arg2 = 157; verify_case(4, Arg2, returnTime(Arg0, Arg1)); } + void test_case_5() { int Arg0 = 100; string Arr1[] = {"0 10 30 45 60 46 39 31 20", "9 20 0 86"}; vector Arg1(Arr1, Arr1 + (sizeof(Arr1) / sizeof(Arr1[0]))); int Arg2 = -1; verify_case(5, Arg2, returnTime(Arg0, Arg1)); } + +// END CUT HERE +}; +// BEGIN CUT HERE +int main() { BusTrip().run_test(-1); } +// END CUT HERE ADDED SRM/339/1B.cpp Index: SRM/339/1B.cpp ================================================================== --- SRM/339/1B.cpp +++ SRM/339/1B.cpp @@ -0,0 +1,57 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +using namespace std; +typedef long long LL; + +class TestBettingStrategy +{ +public: + map memo; + double winProbability(int initSum, int goalSum, int rounds, int prob) + { + memo.clear(); + return rec(initSum, 1, rounds, goalSum, prob/100.0); + } + + double rec(int cur, int bet, int rest, int goal, double pWin) + { + if( cur >= goal ) return 1.0; + if( cur < bet ) return 0.0; + if( rest == 0 ) return 0.0; + + int key = rest*2000*2000 + cur*2000 + bet; + if( memo.count(key) ) + return memo[key]; + return memo[key] + = pWin *rec(cur+bet, 1, rest-1, goal, pWin) + + (1-pWin)*rec(cur-bet, bet*2, rest-1, goal, pWin); + } + +// BEGIN CUT HERE + public: + void run_test(int Case) { if ((Case == -1) || (Case == 0)) test_case_0(); if ((Case == -1) || (Case == 1)) test_case_1(); if ((Case == -1) || (Case == 2)) test_case_2(); if ((Case == -1) || (Case == 3)) test_case_3(); } + private: + template string print_array(const vector &V) { ostringstream os; os << "{ "; for (typename vector::const_iterator iter = V.begin(); iter != V.end(); ++iter) os << '\"' << *iter << "\","; os << " }"; return os.str(); } + void verify_case(int Case, const double &Expected, const double &Received) { cerr << "Test Case #" << Case << "..."; if (Expected == Received) cerr << "PASSED" << endl; else { cerr << "FAILED" << endl; cerr << "\tExpected: \"" << Expected << '\"' << endl; cerr << "\tReceived: \"" << Received << '\"' << endl; } } + void test_case_0() { int Arg0 = 10; int Arg1 = 11; int Arg2 = 4; int Arg3 = 50; double Arg4 = 0.875; verify_case(0, Arg4, winProbability(Arg0, Arg1, Arg2, Arg3)); } + void test_case_1() { int Arg0 = 10; int Arg1 = 20; int Arg2 = 20; int Arg3 = 50; double Arg4 = 0.3441343307495117; verify_case(1, Arg4, winProbability(Arg0, Arg1, Arg2, Arg3)); } + void test_case_2() { int Arg0 = 10; int Arg1 = 20; int Arg2 = 10; int Arg3 = 90; double Arg4 = 0.34867844010000015; verify_case(2, Arg4, winProbability(Arg0, Arg1, Arg2, Arg3)); } + void test_case_3() { int Arg0 = 24; int Arg1 = 38; int Arg2 = 24; int Arg3 = 60; double Arg4 = 0.5940784635646947; verify_case(3, Arg4, winProbability(Arg0, Arg1, Arg2, Arg3)); } + +// END CUT HERE +}; +// BEGIN CUT HERE +int main() { TestBettingStrategy().run_test(-1); } +// END CUT HERE ADDED SRM/339/1C.cpp Index: SRM/339/1C.cpp ================================================================== --- SRM/339/1C.cpp +++ SRM/339/1C.cpp @@ -0,0 +1,75 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +using namespace std; +typedef long long LL; + +class RPSTournament +{ +public: + int greatestSeed(int rounds, int C) + { + int L=1, R=(1< 1) { + int M = (L+R)/2; + (can_win(M, rounds, C) ? L : R) = M; + } + return L; + } + + bool can_win(int X, int R, int C) + { + queue< pair > Q; + Q.push( make_pair(R, X) ); + + set unused; + for(int n=0; n<=(1<::iterator it = unused.lower_bound(xl); + if( it==unused.end() ) + return false; + if(r > 1) { + Q.push( make_pair(r-1,max(*it,x)) ); + Q.push( make_pair(r-1,min(*it,x)) ); + } + unused.erase(it); + } + return true; + } + +// BEGIN CUT HERE + public: + void run_test(int Case) { if ((Case == -1) || (Case == 0)) test_case_0(); if ((Case == -1) || (Case == 1)) test_case_1(); if ((Case == -1) || (Case == 2)) test_case_2(); if ((Case == -1) || (Case == 3)) test_case_3(); if ((Case == -1) || (Case == 4)) test_case_4(); } + private: + template string print_array(const vector &V) { ostringstream os; os << "{ "; for (typename vector::const_iterator iter = V.begin(); iter != V.end(); ++iter) os << '\"' << *iter << "\","; os << " }"; return os.str(); } + void verify_case(int Case, const int &Expected, const int &Received) { cerr << "Test Case #" << Case << "..."; if (Expected == Received) cerr << "PASSED" << endl; else { cerr << "FAILED" << endl; cerr << "\tExpected: \"" << Expected << '\"' << endl; cerr << "\tReceived: \"" << Received << '\"' << endl; } } + void test_case_0() { int Arg0 = 2; int Arg1 = 0; int Arg2 = 1; verify_case(0, Arg2, greatestSeed(Arg0, Arg1)); } + void test_case_1() { int Arg0 = 3; int Arg1 = 1; int Arg2 = 6; verify_case(1, Arg2, greatestSeed(Arg0, Arg1)); } + void test_case_2() { int Arg0 = 4; int Arg1 = 1; int Arg2 = 9; verify_case(2, Arg2, greatestSeed(Arg0, Arg1)); } + void test_case_3() { int Arg0 = 7; int Arg1 = 3; int Arg2 = 50; verify_case(3, Arg2, greatestSeed(Arg0, Arg1)); } + void test_case_4() { int Arg0 = 15; int Arg1 = 180; int Arg2 = 9755; verify_case(4, Arg2, greatestSeed(Arg0, Arg1)); } + +// END CUT HERE +}; +// BEGIN CUT HERE +int main() { RPSTournament().run_test(-1); } +// END CUT HERE ADDED SRM/340-U/1A.cpp Index: SRM/340-U/1A.cpp ================================================================== --- SRM/340-U/1A.cpp +++ SRM/340-U/1A.cpp @@ -0,0 +1,52 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +using namespace std; +typedef long long LL; + +class ProblemsToSolve +{ +public: + int minNumber(vector pre, int variety) + { + int ans = pre.size(); + for(int i=0; i= variety ) + ans = min(ans, solve(min(i,j), max(i,j))); + return ans; + } + + int solve(int S, int D) + { + return (S+1)/2+1 + (D-S+1)/2; + } + +// BEGIN CUT HERE + public: + void run_test(int Case) { if ((Case == -1) || (Case == 0)) test_case_0(); if ((Case == -1) || (Case == 1)) test_case_1(); if ((Case == -1) || (Case == 2)) test_case_2(); if ((Case == -1) || (Case == 3)) test_case_3(); if ((Case == -1) || (Case == 4)) test_case_4(); } + private: + template string print_array(const vector &V) { ostringstream os; os << "{ "; for (typename vector::const_iterator iter = V.begin(); iter != V.end(); ++iter) os << '\"' << *iter << "\","; os << " }"; return os.str(); } + void verify_case(int Case, const int &Expected, const int &Received) { cerr << "Test Case #" << Case << "..."; if (Expected == Received) cerr << "PASSED" << endl; else { cerr << "FAILED" << endl; cerr << "\tExpected: \"" << Expected << '\"' << endl; cerr << "\tReceived: \"" << Received << '\"' << endl; } } + void test_case_0() { int Arr0[] = {1, 2, 3}; vector Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); int Arg1 = 2; int Arg2 = 2; verify_case(0, Arg2, minNumber(Arg0, Arg1)); } + void test_case_1() { int Arr0[] = {1, 2, 3, 4, 5}; vector Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); int Arg1 = 4; int Arg2 = 3; verify_case(1, Arg2, minNumber(Arg0, Arg1)); } + void test_case_2() { int Arr0[] = {10, 1, 12, 101}; vector Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); int Arg1 = 100; int Arg2 = 3; verify_case(2, Arg2, minNumber(Arg0, Arg1)); } + void test_case_3() { int Arr0[] = {10, 1}; vector Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); int Arg1 = 9; int Arg2 = 2; verify_case(3, Arg2, minNumber(Arg0, Arg1)); } + void test_case_4() { int Arr0[] = {6, 2, 6, 2, 6, 3, 3, 3, 7}; vector Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); int Arg1 = 4; int Arg2 = 2; verify_case(4, Arg2, minNumber(Arg0, Arg1)); } + +// END CUT HERE +}; +// BEGIN CUT HERE +int main() { ProblemsToSolve().run_test(-1); } +// END CUT HERE ADDED SRM/340-U/1B.cpp Index: SRM/340-U/1B.cpp ================================================================== --- SRM/340-U/1B.cpp +++ SRM/340-U/1B.cpp @@ -0,0 +1,80 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +using namespace std; +typedef long long LL; + +class CsCourses +{ +public: + vector getOrder(vector theoreticalValue, vector practicalValue, vector expire, int skillBound) + { + typedef pair State; + + vector Q; + Q.push_back( State(0,0) ); + map prev; + map lect; + lect[State(0,0)] = -1; + + for(int Month=0; !Q.empty(); ++Month) + { + vector Q2; + for(int i=0; i= skillBound ) + { + vector ans; + for(State p=s; lect[p]>=0; p=prev[p]) + ans.push_back(lect[p]); + reverse(ans.begin(), ans.end()); + return ans; + } + + for(int k=0; k= theoreticalValue[k]-1 + && pr >= practicalValue[k]-1 + && Month < expire[k] ) + { + State s2( max(th,theoreticalValue[k]), max(pr, practicalValue[k]) ); + if( !lect.count(s2) ) + Q2.push_back(s2), prev[s2]=s, lect[s2]=k; + } + } + Q.swap(Q2); + } + + return vector(); + } + +// BEGIN CUT HERE + public: + void run_test(int Case) { if ((Case == -1) || (Case == 0)) test_case_0(); if ((Case == -1) || (Case == 1)) test_case_1(); if ((Case == -1) || (Case == 2)) test_case_2(); if ((Case == -1) || (Case == 3)) test_case_3(); if ((Case == -1) || (Case == 4)) test_case_4(); if ((Case == -1) || (Case == 5)) test_case_5(); } + private: + template string print_array(const vector &V) { ostringstream os; os << "{ "; for (typename vector::const_iterator iter = V.begin(); iter != V.end(); ++iter) os << '\"' << *iter << "\","; os << " }"; return os.str(); } + void verify_case(int Case, const vector &Expected, const vector &Received) { cerr << "Test Case #" << Case << "..."; if (Expected == Received) cerr << "PASSED" << endl; else { cerr << "FAILED" << endl; cerr << "\tExpected: " << print_array(Expected) << endl; cerr << "\tReceived: " << print_array(Received) << endl; } } + void test_case_0() { int Arr0[] = {1}; vector Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); int Arr1[] = {1}; vector Arg1(Arr1, Arr1 + (sizeof(Arr1) / sizeof(Arr1[0]))); int Arr2[] = {1}; vector Arg2(Arr2, Arr2 + (sizeof(Arr2) / sizeof(Arr2[0]))); int Arg3 = 1; int Arr4[] = {0 }; vector Arg4(Arr4, Arr4 + (sizeof(Arr4) / sizeof(Arr4[0]))); verify_case(0, Arg4, getOrder(Arg0, Arg1, Arg2, Arg3)); } + void test_case_1() { int Arr0[] = {1, 2, 1}; vector Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); int Arr1[] = {2, 1, 1}; vector Arg1(Arr1, Arr1 + (sizeof(Arr1) / sizeof(Arr1[0]))); int Arr2[] = {5, 5, 5}; vector Arg2(Arr2, Arr2 + (sizeof(Arr2) / sizeof(Arr2[0]))); int Arg3 = 2; int Arr4[] = {2, 0, 1 }; vector Arg4(Arr4, Arr4 + (sizeof(Arr4) / sizeof(Arr4[0]))); verify_case(1, Arg4, getOrder(Arg0, Arg1, Arg2, Arg3)); } + void test_case_2() { int Arr0[] = {1, 2, 1}; vector Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); int Arr1[] = {2, 1, 1}; vector Arg1(Arr1, Arr1 + (sizeof(Arr1) / sizeof(Arr1[0]))); int Arr2[] = {1, 1, 1}; vector Arg2(Arr2, Arr2 + (sizeof(Arr2) / sizeof(Arr2[0]))); int Arg3 = 2; vector Arg4; verify_case(2, Arg4, getOrder(Arg0, Arg1, Arg2, Arg3)); } + void test_case_3() { int Arr0[] = {1, 2, 1}; vector Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); int Arr1[] = {2, 1, 1}; vector Arg1(Arr1, Arr1 + (sizeof(Arr1) / sizeof(Arr1[0]))); int Arr2[] = {3, 2, 1}; vector Arg2(Arr2, Arr2 + (sizeof(Arr2) / sizeof(Arr2[0]))); int Arg3 = 2; int Arr4[] = {2, 1, 0 }; vector Arg4(Arr4, Arr4 + (sizeof(Arr4) / sizeof(Arr4[0]))); verify_case(3, Arg4, getOrder(Arg0, Arg1, Arg2, Arg3)); } + void test_case_4() { int Arr0[] = {1, 2, 3, 4, 5, 6, 7}; vector Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); int Arr1[] = {1, 2, 3, 4, 5, 6, 7}; vector Arg1(Arr1, Arr1 + (sizeof(Arr1) / sizeof(Arr1[0]))); int Arr2[] = {1, 2, 3, 4, 5, 6, 7}; vector Arg2(Arr2, Arr2 + (sizeof(Arr2) / sizeof(Arr2[0]))); int Arg3 = 7; int Arr4[] = {0, 1, 2, 3, 4, 5, 6 }; vector Arg4(Arr4, Arr4 + (sizeof(Arr4) / sizeof(Arr4[0]))); verify_case(4, Arg4, getOrder(Arg0, Arg1, Arg2, Arg3)); } + void test_case_5() { int Arr0[] = {0, 1, 2, 2, 1}; vector Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); int Arr1[] = {0, 0, 1, 2, 1}; vector Arg1(Arr1, Arr1 + (sizeof(Arr1) / sizeof(Arr1[0]))); int Arr2[] = {9, 9, 9, 9, 9}; vector Arg2(Arr2, Arr2 + (sizeof(Arr2) / sizeof(Arr2[0]))); int Arg3 = 2; int Arr4[] = {4, 3 }; vector Arg4(Arr4, Arr4 + (sizeof(Arr4) / sizeof(Arr4[0]))); verify_case(5, Arg4, getOrder(Arg0, Arg1, Arg2, Arg3)); } + +// END CUT HERE +}; +// BEGIN CUT HERE +int main() { CsCourses().run_test(-1); } +// END CUT HERE ADDED SRM/341/1A.cpp Index: SRM/341/1A.cpp ================================================================== --- SRM/341/1A.cpp +++ SRM/341/1A.cpp @@ -0,0 +1,65 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +using namespace std; +typedef long long LL; + +class KLastNonZeroDigits +{ +public: + string getKDigits(int N, int K) + { + // 20! = 20*..*11*10! < 20*..*11*2^22 + // < 16^10 * 2^22 + // = 2^62 + + // 10^K + LL Z = 1; + for(int i=0; i string print_array(const vector &V) { ostringstream os; os << "{ "; for (typename vector::const_iterator iter = V.begin(); iter != V.end(); ++iter) os << '\"' << *iter << "\","; os << " }"; return os.str(); } + void verify_case(int Case, const string &Expected, const string &Received) { cerr << "Test Case #" << Case << "..."; if (Expected == Received) cerr << "PASSED" << endl; else { cerr << "FAILED" << endl; cerr << "\tExpected: \"" << Expected << '\"' << endl; cerr << "\tReceived: \"" << Received << '\"' << endl; } } + void test_case_0() { int Arg0 = 10; int Arg1 = 3; string Arg2 = "288"; verify_case(0, Arg2, getKDigits(Arg0, Arg1)); } + void test_case_1() { int Arg0 = 6; int Arg1 = 1; string Arg2 = "2"; verify_case(1, Arg2, getKDigits(Arg0, Arg1)); } + void test_case_2() { int Arg0 = 6; int Arg1 = 3; string Arg2 = "72"; verify_case(2, Arg2, getKDigits(Arg0, Arg1)); } + void test_case_3() { int Arg0 = 7; int Arg1 = 2; string Arg2 = "04"; verify_case(3, Arg2, getKDigits(Arg0, Arg1)); } + void test_case_4() { int Arg0 = 20; int Arg1 = 9; string Arg2 = "200817664"; verify_case(4, Arg2, getKDigits(Arg0, Arg1)); } + void test_case_5() { int Arg0 = 1; int Arg1 = 1; string Arg2 = "1"; verify_case(5, Arg2, getKDigits(Arg0, Arg1)); } + +// END CUT HERE +}; +// BEGIN CUT HERE +int main() { KLastNonZeroDigits().run_test(-1); } +// END CUT HERE ADDED SRM/341/1B.cpp Index: SRM/341/1B.cpp ================================================================== --- SRM/341/1B.cpp +++ SRM/341/1B.cpp @@ -0,0 +1,170 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +using namespace std; +typedef long long LL; + +int dy[] = {-1,1, 0,0,-1,-1, 1,1}; +int dx[] = { 0,0,-1,1,-1, 1,-1,1}; + +class LandAndSea +{ +public: + vector howManyIslands(vector seaMap) + { + const int Y = seaMap.size(); + const int X = seaMap[0].size(); + + // paint + vector< vector > idMap(Y, vector(X, -1)); + int nIsland = 0; + for(int y=0; y > S; + S.push( make_pair(y,x) ); + while( !S.empty() ) { + pair p = S.top(); S.pop(); + for(int i=0; i<8; ++i) { + int yy = p.first +dy[i]; + int xx = p.second+dx[i]; + if( 0<=yy && yy refered(nIsland); + vector ct(nIsland, -1); + + set outer_world; + while( outer_world.size() < nIsland ) + { + set outer2; + for(int I=0; I > S; + set< pair > vis; + for(int y=0; y p = S.top(); S.pop(); + for(int i=0; i<4; ++i) { + int yy = p.first+dy[i]; + int xx = p.second+dx[i]; + if( !(0<=yy && yy pp(yy,xx); + if( !vis.count(pp) && seaMap[yy][xx]=='.' ) + S.push(pp), vis.insert(S.top()); + } + } + continue; + notContained: + outer2.insert(I); + if(sID != -1) { + ct[I] = sID; + refered[sID]++; + } + } + outer_world.insert(outer2.begin(), outer2.end()); + } + + // topological sort + vector level(nIsland, -1); + for(int L=0;;++L) + { + vector victim; + + for(int I=0; I ans; + for(int i=0; i ans.size() ) + ans.resize( level[i]+1 ); + ans[level[i]]++; + } + return ans; + } + +// BEGIN CUT HERE + public: + void run_test(int Case) { if ((Case == -1) || (Case == 0)) test_case_0(); if ((Case == -1) || (Case == 1)) test_case_1(); if ((Case == -1) || (Case == 2)) test_case_2(); if ((Case == -1) || (Case == 3)) test_case_3(); if ((Case == -1) || (Case == 4)) test_case_4(); } + private: + template string print_array(const vector &V) { ostringstream os; os << "{ "; for (typename vector::const_iterator iter = V.begin(); iter != V.end(); ++iter) os << '\"' << *iter << "\","; os << " }"; return os.str(); } + void verify_case(int Case, const vector &Expected, const vector &Received) { cerr << "Test Case #" << Case << "..."; if (Expected == Received) cerr << "PASSED" << endl; else { cerr << "FAILED" << endl; cerr << "\tExpected: " << print_array(Expected) << endl; cerr << "\tReceived: " << print_array(Received) << endl; } } + void test_case_0() { string Arr0[] = {"x"}; vector Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); int Arr1[] = {1 }; vector Arg1(Arr1, Arr1 + (sizeof(Arr1) / sizeof(Arr1[0]))); verify_case(0, Arg1, howManyIslands(Arg0)); } + void test_case_1() { string Arr0[] = { +"xxxxx", +"x...x", +"x.x.x", +"x...x", +"xxxxx" +}; vector Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); int Arr1[] = {1, 1 }; vector Arg1(Arr1, Arr1 + (sizeof(Arr1) / sizeof(Arr1[0]))); verify_case(1, Arg1, howManyIslands(Arg0)); } + void test_case_2() { string Arr0[] = { +"xxxxx", +"x...x", +"x.x.x", +"x...x", +"xxxxx", +"xxxxx", +"x...x", +"x.x.x", +"x...x", +"xxxxx" +}; vector Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); int Arr1[] = {2, 1 }; vector Arg1(Arr1, Arr1 + (sizeof(Arr1) / sizeof(Arr1[0]))); verify_case(2, Arg1, howManyIslands(Arg0)); } + void test_case_3() { string Arr0[] = { +"..", +".." +}; vector Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); vector Arg1; verify_case(3, Arg1, howManyIslands(Arg0)); } + void test_case_4() { string Arr0[] = { +"............", +".......xxxx.", +"..xxx.x...x.", +"..x..x..x.x.", +"..x.x.x...x.", +"..xx...xxx.." +}; vector Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); int Arr1[] = {1, 1 }; vector Arg1(Arr1, Arr1 + (sizeof(Arr1) / sizeof(Arr1[0]))); verify_case(4, Arg1, howManyIslands(Arg0)); } + +// END CUT HERE +}; +// BEGIN CUT HERE +int main() { LandAndSea().run_test(-1); } +// END CUT HERE ADDED SRM/341/1C.cpp Index: SRM/341/1C.cpp ================================================================== --- SRM/341/1C.cpp +++ SRM/341/1C.cpp @@ -0,0 +1,175 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +using namespace std; +typedef long long LL; + +vector vMul( const vector< vector >& A, const vector& B ) +{ + const int n = A.size(); + + vector C(n); + for(int i=0; i > mMul( const vector< vector >& A, const vector< vector >& B ) +{ + const int n = A.size(); + + vector< vector > C(n, vector(n)); + for(int i=0; i > mPow( vector< vector > M, int t ) +{ + if( t == 0 ) { + vector< vector > R(M.size(), vector(M.size())); + for(int i=0; i > R; + for(; t; t>>=1, M=mMul(M,M)) + if( t&1 ) + R = (R.empty() ? M : mMul(R,M)); + return R; +} + +class ValidPlates +{ +public: + map, LL> memo; + + string getPlate(vector profane, int seqno) + { + // parse + bool proh[10][10] = {}; + for(int i=0; i>n; ) + proh[n/10][n%10] = true; + } + + // matrix + vector N(11, 1); N[10] = 0; + vector< vector > M(11, vector(11)); + for(int i=0; i<10; ++i) + for(int j=0; j<10; ++j) + if( !proh[i][j] ) + M[i][j] = 1; + for(int j=1; j<11; ++j) + M[10][j] = 1; + + // binary search... + LL L=-1, R=0; + for(LL atprev=-1;; R=R*2+1) + { + vector< vector > Mt = mPow(M, R); + vector Nt = vMul(Mt, N); + LL at = accumulate(Nt.begin()+1, Nt.end(), 0LL); + if( atprev == at ) + return ""; + atprev = at; + if( seqno <= at ) + break; + } + while(R-L>1) + { + LL C=(L+R)/2; + vector< vector > Mt = mPow(M, C); + vector Nt = vMul(Mt, N); + (seqno <= accumulate(Nt.begin()+1, Nt.end(), 0LL) ? R : L) = C; + } + LL d = R+1; // #digits + seqno -= vMul(mPow(M,R), N)[10]; + + string buf; + int prev = -1; + for(int i=0; i<(d>50 ? 47 : d); ++i) + if( prev == -1 ) + { + vector Nt = vMul(mPow(M, d-1-i), N); + LL n = 0; + for(int a=1; a<10; ++a) + { + n += Nt[a]; + if( seqno <= n ) + {buf += char('0'+a); prev=a; seqno-=n-Nt[a]; break;} + } + } + else + { + vector Nt = vMul(mPow(M, d-1-i), N); + LL n = 0; + for(int a=0; a<10; ++a) if( !proh[prev][a] ) + { + n += Nt[a]; + if( seqno <= n ) + {buf += char('0'+a); prev=a; seqno-=n-Nt[a]; break;} + } + } + return (d>50 ? buf+"..." : buf); + } + +// BEGIN CUT HERE + public: + void run_test(int Case) { if ((Case == -1) || (Case == 0)) test_case_0(); if ((Case == -1) || (Case == 1)) test_case_1(); if ((Case == -1) || (Case == 2)) test_case_2(); if ((Case == -1) || (Case == 3)) test_case_3(); if ((Case == -1) || (Case == 4)) test_case_4(); if ((Case == -1) || (Case == 5)) test_case_5(); if ((Case == -1) || (Case == 6)) test_case_6(); } + private: + template string print_array(const vector &V) { ostringstream os; os << "{ "; for (typename vector::const_iterator iter = V.begin(); iter != V.end(); ++iter) os << '\"' << *iter << "\","; os << " }"; return os.str(); } + void verify_case(int Case, const string &Expected, const string &Received) { cerr << "Test Case #" << Case << "..."; if (Expected == Received) cerr << "PASSED" << endl; else { cerr << "FAILED" << endl; cerr << "\tExpected: \"" << Expected << '\"' << endl; cerr << "\tReceived: \"" << Received << '\"' << endl; } } + void test_case_0() { vector Arg0; int Arg1 = 1000; string Arg2 = "1000"; verify_case(0, Arg2, getPlate(Arg0, Arg1)); } + void test_case_1() { string Arr0[] = {"10"}; vector Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); int Arg1 = 10; string Arg2 = "11"; verify_case(1, Arg2, getPlate(Arg0, Arg1)); } + void test_case_2() { string Arr0[] = {"10"}; vector Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); int Arg1 = 2000000000; string Arg2 = "2277659869"; verify_case(2, Arg2, getPlate(Arg0, Arg1)); } + void test_case_3() { string Arr0[] = {"00 01 02 03 04 05 06 07 08 09 11 12 13 14 15 16 17", + "18 19 22 23 24 25 26 27 28 29 33 34 35 36 37 38 39", + "44 45 46 47 48 49 55 56 57 58 59 66 67 68 69 77 78", + "79 88 89 99 99 99 99 99"}; vector Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); int Arg1 = 1023; string Arg2 = ""; verify_case(3, Arg2, getPlate(Arg0, Arg1)); } + void test_case_4() { string Arr0[] = {"00 01 02 03 04 05 07 08 09", + "10 11 12 13 14 15 17 18 19", + "20 21 22 24 25 26 27 28 29", + "30 31 32 33 34 36 37 38 39", + "41 43 45 46 48", + "52 53 54 55 56 58 59", + "60 61 63 64 66 67 68 69", + "70 72 73 74 75 76 77 78", + "80 81 82 83 84 86 87 88 89", + "90 91 92 94 95 96 97 98"}; vector Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); int Arg1 = 2000000000; string Arg2 = "79999999351623516571657999935799993"; verify_case(4, Arg2, getPlate(Arg0, Arg1)); } + void test_case_5() { string Arr0[] = {"00 01 02 03 04 05 06 07 08 09", + "10 11 12 13 14 16 17 19", + "20 21 22 23 24 25 26 27 28 29", + "30 31 32 33 34 35 36 38 39", + "41 42 43 44 45 46 49", + "50 52 53 54 55 57 58 59", + "60 61 62 63 64 65 66 67 68 69", + "70 72 73 74 75 76 77 78 79", + "80 81 82 83 84 85 86 87 88 89", + "90 91 92 93 94 95 98 99"}; vector Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); int Arg1 = 2000000000; string Arg2 = "37151515151515151515151515151515151515151515151..."; verify_case(5, Arg2, getPlate(Arg0, Arg1)); } + void test_case_6() { string Arr0[] = {"00 01 02 03 04 05 06 07 08 09", "10 12 13 14 15 16 17 18 19", "20 21 22 23 24 25 26 27 28 29", "30 31 32 33 34 35 36 37 38 39", "40 41 42 43 44 45 46 47 48 49", "50 51 52 53 54 55 56 57 58 59", "60 61 62 63 64 65 66 67 68 69", "70 71 72 73 74 75 76 77 78 79", "80 81 82 83 84 85 86 87 88 89", "90 91 92 93 94 95 96 97 98 99"}; + vector Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); int Arg1 = 2000000000; string Arg2 = "11111111111111111111111111111111111111111111111..."; verify_case(6, Arg2, getPlate(Arg0, Arg1)); } + +// END CUT HERE +}; +// BEGIN CUT HERE +int main() { ValidPlates().run_test(-1); } +// END CUT HERE ADDED SRM/342/1A.cpp Index: SRM/342/1A.cpp ================================================================== --- SRM/342/1A.cpp +++ SRM/342/1A.cpp @@ -0,0 +1,77 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +using namespace std; +typedef long long LL; + +int get_and_go( const string& a, int& i ) +{ + int dict[] = + {'a','b','k','d','e','g','h','i','l','m','n','n'/*g*/,'o','p','r','s','t','u','w','y'}; + for(int k=0;; ++k) + if( dict[k] == a[i] ) + if( a[i]=='n' && i+1 sortWords(vector words) + { + sort( words.begin(), words.end(), &tgcmp ); + return words; + } + +// BEGIN CUT HERE + public: + void run_test(int Case) { if ((Case == -1) || (Case == 0)) test_case_0(); if ((Case == -1) || (Case == 1)) test_case_1(); if ((Case == -1) || (Case == 2)) test_case_2(); if ((Case == -1) || (Case == 3)) test_case_3(); if ((Case == -1) || (Case == 4)) test_case_4(); if ((Case == -1) || (Case == 5)) test_case_5(); } + private: + template string print_array(const vector &V) { ostringstream os; os << "{ "; for (typename vector::const_iterator iter = V.begin(); iter != V.end(); ++iter) os << '\"' << *iter << "\","; os << " }"; return os.str(); } + void verify_case(int Case, const vector &Expected, const vector &Received) { cerr << "Test Case #" << Case << "..."; if (Expected == Received) cerr << "PASSED" << endl; else { cerr << "FAILED" << endl; cerr << "\tExpected: " << print_array(Expected) << endl; cerr << "\tReceived: " << print_array(Received) << endl; } } + void test_case_0() { string Arr0[] = {"abakada","alpabet","tagalog","ako"}; vector Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); string Arr1[] = {"abakada", "ako", "alpabet", "tagalog" }; vector Arg1(Arr1, Arr1 + (sizeof(Arr1) / sizeof(Arr1[0]))); verify_case(0, Arg1, sortWords(Arg0)); } + void test_case_1() { string Arr0[] = {"ang","ano","anim","alak","alam","alab"}; vector Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); string Arr1[] = {"alab", "alak", "alam", "anim", "ano", "ang" }; vector Arg1(Arr1, Arr1 + (sizeof(Arr1) / sizeof(Arr1[0]))); verify_case(1, Arg1, sortWords(Arg0)); } + void test_case_2() { string Arr0[] = {"siya","niya","kaniya","ikaw","ito","iyon"}; vector Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); string Arr1[] = {"kaniya", "ikaw", "ito", "iyon", "niya", "siya" }; vector Arg1(Arr1, Arr1 + (sizeof(Arr1) / sizeof(Arr1[0]))); verify_case(2, Arg1, sortWords(Arg0)); } + void test_case_3() { string Arr0[] = {"kaba","baka","naba","ngipin","nipin"}; vector Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); string Arr1[] = {"baka", "kaba", "naba", "nipin", "ngipin" }; vector Arg1(Arr1, Arr1 + (sizeof(Arr1) / sizeof(Arr1[0]))); verify_case(3, Arg1, sortWords(Arg0)); } + void test_case_4() { string Arr0[] = {"knilngiggnngginggn","ingkigningg","kingkong","dingdong","dindong","dingdont","ingkblot"}; vector Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); string Arr1[] = {"kingkong", "knilngiggnngginggn", "dindong", "dingdont", "dingdong", "ingkblot", "ingkigningg" }; vector Arg1(Arr1, Arr1 + (sizeof(Arr1) / sizeof(Arr1[0]))); verify_case(4, Arg1, sortWords(Arg0)); } + void test_case_5() { string Arr0[] = {"silangang", "baka", "bada", "silang"}; vector Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); string Arr1[] = {"baka", "bada", "silang", "silangang" }; vector Arg1(Arr1, Arr1 + (sizeof(Arr1) / sizeof(Arr1[0]))); verify_case(5, Arg1, sortWords(Arg0)); } + +// END CUT HERE +}; +// BEGIN CUT HERE +int main() { + TagalogDictionary().run_test(-1); +} +// END CUT HERE ADDED SRM/342/1B.cpp Index: SRM/342/1B.cpp ================================================================== --- SRM/342/1B.cpp +++ SRM/342/1B.cpp @@ -0,0 +1,112 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +using namespace std; +typedef long long LL; + +static const int MODNUM = 1000000007; + +// make [i,j) by k[z..$] +LL dp[31][31][51][51]; + +class ReverseResources +{ +public: + int findDecompositions(string str, vector resources) + { + memset(dp, 0xff, sizeof(dp)); + + LL sum = 0; + for(int k=0; k& rs, LL dp[31][31][51][51] ) + { + if( dp[i][j][k][z] >= 0 ) + return dp[i][j][k][z]; + + string& r = rs[k]; + if( z==r.size() ) + return dp[i][j][k][z] = (i==j ? 1 : 0); + + if( r[z] == '%' ) + { + LL sum = 0; + if( z+2 == r.size() ) + { + for(int kk=0; kk string print_array(const vector &V) { ostringstream os; os << "{ "; for (typename vector::const_iterator iter = V.begin(); iter != V.end(); ++iter) os << '\"' << *iter << "\","; os << " }"; return os.str(); } + void verify_case(int Case, const int &Expected, const int &Received) { cerr << "Test Case #" << Case << "..."; if (Expected == Received) cerr << "PASSED" << endl; else { cerr << "FAILED" << endl; cerr << "\tExpected: \"" << Expected << '\"' << endl; cerr << "\tReceived: \"" << Received << '\"' << endl; } } + void test_case_0() { string Arg0 = "Error in module foo, code 123."; string Arr1[] = {"foo", "123", "Error in module %s.", "%s, code %s"}; vector Arg1(Arr1, Arr1 + (sizeof(Arr1) / sizeof(Arr1[0]))); int Arg2 = 1; verify_case(0, Arg2, findDecompositions(Arg0, Arg1)); } + void test_case_1() { string Arg0 = "The fox jumped over the dog."; string Arr1[] = {"The fox %s over the dog.", + "lazy", + "brown", + "jump%s", + "s", + "ed", + "The %s", + "fox %s", + "%s over %s", + "the dog."}; vector Arg1(Arr1, Arr1 + (sizeof(Arr1) / sizeof(Arr1[0]))); int Arg2 = 5; verify_case(1, Arg2, findDecompositions(Arg0, Arg1)); } + void test_case_2() { string Arg0 = "abcde"; string Arr1[] = {"%sc%s", "b", "de", "a%s"}; vector Arg1(Arr1, Arr1 + (sizeof(Arr1) / sizeof(Arr1[0]))); int Arg2 = 2; verify_case(2, Arg2, findDecompositions(Arg0, Arg1)); } + void test_case_3() { string Arg0 = "abcde"; string Arr1[] = {"%se", "a%s", "%sb%s", "%sc%s", "%sd%s"}; vector Arg1(Arr1, Arr1 + (sizeof(Arr1) / sizeof(Arr1[0]))); int Arg2 = 0; verify_case(3, Arg2, findDecompositions(Arg0, Arg1)); } + void test_case_4() { string Arg0 = "aaaaa"; string Arr1[] = {"a","%s%s"}; vector Arg1(Arr1, Arr1 + (sizeof(Arr1) / sizeof(Arr1[0]))); int Arg2 = 14; verify_case(4, Arg2, findDecompositions(Arg0, Arg1)); } + void test_case_5() { string Arg0 = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"; string Arr1[] = {"a","aa","%s%s","%sa","a%s","aaa","aa%s","a%sa", +"a%s%s","%saa","%sa%s","%s%sa","%s%s%s","aaaa", +"aaa%s","aa%sa","aa%s%s","a%saa","a%sa%s","a%s%sa", +"a%s%s%s","%saaa","%saa%s","%sa%sa","%sa%s%s", +"%s%saa","%s%sa%s","%s%s%sa","%s%s%s%s","aaaaa", +"aaaa%s","aaa%sa","aaa%s%s","aa%saa","aa%sa%s", +"aa%s%sa","aa%s%s%s","a%saaa","a%saa%s","a%sa%sa", +"a%sa%s%s","a%s%saa","a%s%sa%s","a%s%s%sa", +"a%s%s%s%s","%saaaa","%saaa%s","%saa%sa","%saa%s%s"}; vector Arg1(Arr1, Arr1 + (sizeof(Arr1) / sizeof(Arr1[0]))); int Arg2 = 879704799; verify_case(5, Arg2, findDecompositions(Arg0, Arg1)); } + void test_case_6() { string Arg0 = "aa"; string Arr1[] = {"a", "a", "%s%s", "%s%s"}; vector Arg1(Arr1, Arr1 + (sizeof(Arr1) / sizeof(Arr1[0]))); int Arg2 = 8; verify_case(6, Arg2, findDecompositions(Arg0, Arg1)); } + +// END CUT HERE +}; +// BEGIN CUT HERE +int main() { ReverseResources().run_test(-1); } +// END CUT HERE ADDED SRM/342/1C.cpp Index: SRM/342/1C.cpp ================================================================== --- SRM/342/1C.cpp +++ SRM/342/1C.cpp @@ -0,0 +1,96 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +using namespace std; +typedef long long LL; + +vector< vector > mMul( const vector< vector >& A, const vector< vector >& B ) +{ + const int n = A.size(); + + vector< vector > C(n, vector(n)); + for(int i=0; i > mPow( vector< vector > M, int t ) // t>0 +{ + vector< vector > R; + for(; t; t>>=1, M=mMul(M,M)) + if( t&1 ) + R = (R.empty() ? M : mMul(R,M)); + return R; +} + +class DrivingAround +{ +public: + int numberOfWays(vector adj, int start, int finish, int time) + { + int n = adj.size()*5; + vector< vector > M(n, vector(n)); + for(int u=0; u string print_array(const vector &V) { ostringstream os; os << "{ "; for (typename vector::const_iterator iter = V.begin(); iter != V.end(); ++iter) os << '\"' << *iter << "\","; os << " }"; return os.str(); } + void verify_case(int Case, const int &Expected, const int &Received) { cerr << "Test Case #" << Case << "..."; if (Expected == Received) cerr << "PASSED" << endl; else { cerr << "FAILED" << endl; cerr << "\tExpected: \"" << Expected << '\"' << endl; cerr << "\tReceived: \"" << Received << '\"' << endl; } } + void test_case_0() { string Arr0[] = {".12", + "2.1", + "12."}; vector Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); int Arg1 = 0; int Arg2 = 2; int Arg3 = 5; int Arg4 = 8; verify_case(0, Arg4, numberOfWays(Arg0, Arg1, Arg2, Arg3)); } + void test_case_1() { string Arr0[] = {"....52....", + "..5.......", + "..........", + ".......1..", + "......42.2", + "5...4.....", + ".5...4...1", + "......5...", + ".3244.....", + ".........."}; vector Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); int Arg1 = 2; int Arg2 = 2; int Arg3 = 10; int Arg4 = 0; verify_case(1, Arg4, numberOfWays(Arg0, Arg1, Arg2, Arg3)); } + void test_case_2() { string Arr0[] = {"...14....1", + "......13..", + ".2...4....", + "....52.5..", + "1.3..4....", + ".3....35.5", + "4......1.1", + "..4.4.1.54", + "....4.11.5", + "31144.2.4."}; vector Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); int Arg1 = 7; int Arg2 = 2; int Arg3 = 100; int Arg4 = 316984; verify_case(2, Arg4, numberOfWays(Arg0, Arg1, Arg2, Arg3)); } + void test_case_3() { string Arr0[] = {".555555555", "5.55555555", "55.5554555", "555.555555", "5555.55555", "55555.5555", "555555.555", "5555555.55", "55553555.5", "555555555."}; +vector Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); int Arg1 = 0; int Arg2 = 7; int Arg3 = 1000000000; int Arg4 = 255218; verify_case(3, Arg4, numberOfWays(Arg0, Arg1, Arg2, Arg3)); } +// END CUT HERE +}; +// BEGIN CUT HERE +int main() { DrivingAround().run_test(-1); } +// END CUT HERE ADDED SRM/343/1A.cpp Index: SRM/343/1A.cpp ================================================================== --- SRM/343/1A.cpp +++ SRM/343/1A.cpp @@ -0,0 +1,84 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +using namespace std; +typedef long long LL; + +class CDPlayer +{ +public: + int isRandom(vector songlist, int n) + { + string s; + for(int i=0; i(e,s.size())) ) + return false; + return true; + } + + bool distinct( const string& s, int i, int e ) + { + set x; + for(; i string print_array(const vector &V) { ostringstream os; os << "{ "; for (typename vector::const_iterator iter = V.begin(); iter != V.end(); ++iter) os << '\"' << *iter << "\","; os << " }"; return os.str(); } + void verify_case(int Case, const int &Expected, const int &Received) { cerr << "Test Case #" << Case << "..."; if (Expected == Received) cerr << "PASSED" << endl; else { cerr << "FAILED" << endl; cerr << "\tExpected: \"" << Expected << '\"' << endl; cerr << "\tReceived: \"" << Received << '\"' << endl; } } + void test_case_0() { string Arr0[] = {"BBAC"}; vector Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); int Arg1 = 3; int Arg2 = 1; verify_case(0, Arg2, isRandom(Arg0, Arg1)); } + void test_case_1() { string Arr0[] = {"BACAB", + "BCA"}; vector Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); int Arg1 = 3; int Arg2 = 2; verify_case(1, Arg2, isRandom(Arg0, Arg1)); } + void test_case_2() { string Arr0[] = {"AAACBACBACBACBACBACBACB"}; vector Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); int Arg1 = 3; int Arg2 = -1; verify_case(2, Arg2, isRandom(Arg0, Arg1)); } + void test_case_3() { string Arr0[] = {"ABCDEABDECBAECDEDACB"}; vector Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); int Arg1 = 5; int Arg2 = 0; verify_case(3, Arg2, isRandom(Arg0, Arg1)); } + void test_case_4() { string Arr0[] = {"ABCABCABCABCABCABCABC", + "ABCABCABCABCABCABCABC"}; vector Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); int Arg1 = 22; int Arg2 = -1; verify_case(4, Arg2, isRandom(Arg0, Arg1)); } + void test_case_5() { string Arr0[] = {"AAAAAAAAAAAAAAAA", + "AAAAAAAAAAAAAAAA", + "AAAAAAAAAAAAAAAA", + "AAAAAAAAAAAAAAAA", + "AAAAAAAAAAAAAAAA", + "AAAAAAAAAAAAAAAA", + "AAAAAAAAAAAAAAAA", + "AAAAAAAAAAAAAAAA", + "AAAAAAAAAAAAAAAA", + "AAAAAAAAAAAAAAAA"}; vector Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); int Arg1 = 1; int Arg2 = 0; verify_case(5, Arg2, isRandom(Arg0, Arg1)); } + void test_case_6() { string Arr0[] = {"ADEF"}; vector Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); int Arg1 = 12; int Arg2 = 0; verify_case(6, Arg2, isRandom(Arg0, Arg1)); } + +// END CUT HERE +}; +// BEGIN CUT HERE +int main() { + CDPlayer().run_test(-1); +} +// END CUT HERE ADDED SRM/343/1B.cpp Index: SRM/343/1B.cpp ================================================================== --- SRM/343/1B.cpp +++ SRM/343/1B.cpp @@ -0,0 +1,83 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +using namespace std; +typedef long long LL; + +class MoneyGame +{ +public: + int V1, V2, V3; + map memo; + int play(vector lennysCoins, vector fredsCoins, vector values) + { + V1 = values[0]; + V2 = values[1]; + V3 = values[2]; + memo.clear(); + return rec( lennysCoins[0], lennysCoins[1], lennysCoins[2], + fredsCoins[0], fredsCoins[1], fredsCoins[2], 0, 0, 0 ); + } + + int rec(int L1, int L2, int L3, int F1, int F2, int F3, int B1, int B2, int B3) + { + if( !L1 && !L2 && !L3 ) + return -(V1*F1+V2*F2+V3*F3); + int key = (L1<<20)|(L2<<16)|(L3<<12)|(F1<<8)|(F2<<4)|(F3<<0); + if( memo.count(key) ) + return memo[key]; + + int score = -0x7fffffff; + if(L1) { + for(int b1=0; b1<=B1 && V1*b1 string print_array(const vector &V) { ostringstream os; os << "{ "; for (typename vector::const_iterator iter = V.begin(); iter != V.end(); ++iter) os << '\"' << *iter << "\","; os << " }"; return os.str(); } + void verify_case(int Case, const int &Expected, const int &Received) { cerr << "Test Case #" << Case << "..."; if (Expected == Received) cerr << "PASSED" << endl; else { cerr << "FAILED" << endl; cerr << "\tExpected: \"" << Expected << '\"' << endl; cerr << "\tReceived: \"" << Received << '\"' << endl; } } + void test_case_0() { int Arr0[] = {0,1,1}; vector Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); int Arr1[] = {0,1,1}; vector Arg1(Arr1, Arr1 + (sizeof(Arr1) / sizeof(Arr1[0]))); int Arr2[] = {20,10,2}; vector Arg2(Arr2, Arr2 + (sizeof(Arr2) / sizeof(Arr2[0]))); int Arg3 = -2; verify_case(0, Arg3, play(Arg0, Arg1, Arg2)); } + void test_case_1() { int Arr0[] = {0,1,2}; vector Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); int Arr1[] = {0,1,1}; vector Arg1(Arr1, Arr1 + (sizeof(Arr1) / sizeof(Arr1[0]))); int Arr2[] = {20,10,2}; vector Arg2(Arr2, Arr2 + (sizeof(Arr2) / sizeof(Arr2[0]))); int Arg3 = 2; verify_case(1, Arg3, play(Arg0, Arg1, Arg2)); } + void test_case_2() { int Arr0[] = {1,1,0}; vector Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); int Arr1[] = {0,0,1}; vector Arg1(Arr1, Arr1 + (sizeof(Arr1) / sizeof(Arr1[0]))); int Arr2[] = {1,5,10}; vector Arg2(Arr2, Arr2 + (sizeof(Arr2) / sizeof(Arr2[0]))); int Arg3 = 0; verify_case(2, Arg3, play(Arg0, Arg1, Arg2)); } + void test_case_3() { int Arr0[] = {4,4,3}; vector Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); int Arr1[] = {4,3,4}; vector Arg1(Arr1, Arr1 + (sizeof(Arr1) / sizeof(Arr1[0]))); int Arr2[] = {100,753,100}; vector Arg2(Arr2, Arr2 + (sizeof(Arr2) / sizeof(Arr2[0]))); int Arg3 = 600; verify_case(3, Arg3, play(Arg0, Arg1, Arg2)); } + void test_case_4() { int Arr0[] = {0,0,0}; vector Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); int Arr1[] = {5,5,5}; vector Arg1(Arr1, Arr1 + (sizeof(Arr1) / sizeof(Arr1[0]))); int Arr2[] = {1000,1000,1000}; vector Arg2(Arr2, Arr2 + (sizeof(Arr2) / sizeof(Arr2[0]))); int Arg3 = -15000; verify_case(4, Arg3, play(Arg0, Arg1, Arg2)); } + void test_case_5() { int Arr0[] = {3,2,1}; vector Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); int Arr1[] = {0,0,0}; vector Arg1(Arr1, Arr1 + (sizeof(Arr1) / sizeof(Arr1[0]))); int Arr2[] = {6,8,14}; vector Arg2(Arr2, Arr2 + (sizeof(Arr2) / sizeof(Arr2[0]))); int Arg3 = 42; verify_case(5, Arg3, play(Arg0, Arg1, Arg2)); } + +// END CUT HERE +}; +// BEGIN CUT HERE +int main() { + MoneyGame().run_test(-1); +} +// END CUT HERE ADDED SRM/343/1C.cpp Index: SRM/343/1C.cpp ================================================================== --- SRM/343/1C.cpp +++ SRM/343/1C.cpp @@ -0,0 +1,77 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +using namespace std; +typedef long long LL; + +class RefactorableNumber +{ +public: + vector P; + + int count(int low, int high) + { + P.clear(); + vector isP(2000, true); + for(int p=2; p<2000; ++p) + if( isP[p] ) { + P.push_back(p); + for(int q=p*p; q<2000; q+=p) + isP[q] = false; + } + + int cnt = 0; + for(int n=low; n<=high; ++n) + if( ref(n) ) + ++cnt; + return cnt; + } + + bool ref(int N) + { + int n = N; + int x = 1; + for(int i=0; i1; ++i) + { + int p = P[i]; + if( p*p > n ) + break; + int c = 0; + while(n%p==0) + n/=p, ++c; + x *= c+1; + } + if(n>1) + x *= 2; + return N%x == 0; + } + +// BEGIN CUT HERE + public: + void run_test(int Case) { if ((Case == -1) || (Case == 0)) test_case_0(); if ((Case == -1) || (Case == 1)) test_case_1(); if ((Case == -1) || (Case == 2)) test_case_2(); if ((Case == -1) || (Case == 3)) test_case_3(); } + private: + template string print_array(const vector &V) { ostringstream os; os << "{ "; for (typename vector::const_iterator iter = V.begin(); iter != V.end(); ++iter) os << '\"' << *iter << "\","; os << " }"; return os.str(); } + void verify_case(int Case, const int &Expected, const int &Received) { cerr << "Test Case #" << Case << "..."; if (Expected == Received) cerr << "PASSED" << endl; else { cerr << "FAILED" << endl; cerr << "\tExpected: \"" << Expected << '\"' << endl; cerr << "\tReceived: \"" << Received << '\"' << endl; } } + void test_case_0() { int Arg0 = 1; int Arg1 = 10; int Arg2 = 4; verify_case(0, Arg2, count(Arg0, Arg1)); } + void test_case_1() { int Arg0 = 10; int Arg1 = 100; int Arg2 = 12; verify_case(1, Arg2, count(Arg0, Arg1)); } + void test_case_2() { int Arg0 = 25; int Arg1 = 35; int Arg2 = 0; verify_case(2, Arg2, count(Arg0, Arg1)); } + void test_case_3() { int Arg0 = 123; int Arg1 = 4567; int Arg2 = 315; verify_case(3, Arg2, count(Arg0, Arg1)); } + +// END CUT HERE +}; +// BEGIN CUT HERE +int main() { + RefactorableNumber().run_test(-1); +} +// END CUT HERE ADDED SRM/344/1A.cpp Index: SRM/344/1A.cpp ================================================================== --- SRM/344/1A.cpp +++ SRM/344/1A.cpp @@ -0,0 +1,80 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +using namespace std; +typedef long long LL; + +class VolleyballTournament +{ +public: + string reconstructResults(int wonMatches, int lostMatches, int wonSets, int lostSets) + { + //0-3, 1-3, 2-3, 3-0, 3-1, 3-2 + //Z0 Z1 Z2 W0 W1 W2 + + //Z0+ Z1 + Z2 == lostMatches + //Z0*0 + Z1*1 + Z2*2 + 3*wonMatches == wonSets + + //W0+W1+W2 == wonMatches + //W0*0+W1*1+W2*2+3*lostMatches == lostSets + + int a[6] = {-1}; + for(int Z1=0; Z1<=lostMatches; ++Z1) + { + int Z2 = (wonSets - 3*wonMatches - Z1) / 2; + int Z0 = lostMatches - Z1 - Z2; + if( Z0<0 || Z2<0 || lostMatches=0 ) + return "AMBIGUITY"; + else + a[0]=Z0, a[1]=Z1, a[2]=Z2, a[3]=W0, a[4]=W1, a[5]=W2; + } + } + const char* s[6] = {"0-3", "1-3", "2-3", "3-0", "3-1", "3-2"}; + + string ans; + for(int i=0; i<6; ++i) + for(int j=0; j string print_array(const vector &V) { ostringstream os; os << "{ "; for (typename vector::const_iterator iter = V.begin(); iter != V.end(); ++iter) os << '\"' << *iter << "\","; os << " }"; return os.str(); } + void verify_case(int Case, const string &Expected, const string &Received) { cerr << "Test Case #" << Case << "..."; if (Expected == Received) cerr << "PASSED" << endl; else { cerr << "FAILED" << endl; cerr << "\tExpected: \"" << Expected << '\"' << endl; cerr << "\tReceived: \"" << Received << '\"' << endl; } } + void test_case_0() { int Arg0 = 3; int Arg1 = 3; int Arg2 = 9; int Arg3 = 9; string Arg4 = "0-3,0-3,0-3,3-0,3-0,3-0"; verify_case(0, Arg4, reconstructResults(Arg0, Arg1, Arg2, Arg3)); } + void test_case_1() { int Arg0 = 0; int Arg1 = 3; int Arg2 = 6; int Arg3 = 9; string Arg4 = "2-3,2-3,2-3"; verify_case(1, Arg4, reconstructResults(Arg0, Arg1, Arg2, Arg3)); } + void test_case_2() { int Arg0 = 3; int Arg1 = 0; int Arg2 = 9; int Arg3 = 3; string Arg4 = "AMBIGUITY"; verify_case(2, Arg4, reconstructResults(Arg0, Arg1, Arg2, Arg3)); } + void test_case_3() { int Arg0 = 1; int Arg1 = 1; int Arg2 = 4; int Arg3 = 4; string Arg4 = "1-3,3-1"; verify_case(3, Arg4, reconstructResults(Arg0, Arg1, Arg2, Arg3)); } + +// END CUT HERE +}; +// BEGIN CUT HERE +int main() { + VolleyballTournament().run_test(-1); +} +// END CUT HERE ADDED SRM/344/1B.cpp Index: SRM/344/1B.cpp ================================================================== --- SRM/344/1B.cpp +++ SRM/344/1B.cpp @@ -0,0 +1,81 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +using namespace std; +typedef long long LL; + +class QuantumAlchemy +{ +public: + int minSteps(string initial, vector reactions) + { + map r; + for(int i=0; i g; + for(int i=0; i stk; stk.insert('X'); + return make('X', r, g, stk); + } + + int make(char X, map& r, map& g, set& stk) + { + if( !r.count(X) ) + return -1; + string s = r[X]; + + int rea = 1; + for(int i=0; i string print_array(const vector &V) { ostringstream os; os << "{ "; for (typename vector::const_iterator iter = V.begin(); iter != V.end(); ++iter) os << '\"' << *iter << "\","; os << " }"; return os.str(); } + void verify_case(int Case, const int &Expected, const int &Received) { cerr << "Test Case #" << Case << "..."; if (Expected == Received) cerr << "PASSED" << endl; else { cerr << "FAILED" << endl; cerr << "\tExpected: \"" << Expected << '\"' << endl; cerr << "\tReceived: \"" << Received << '\"' << endl; } } + void test_case_0() { string Arg0 = "ABCDE"; string Arr1[] = {"ABC->F", "FE->X"}; vector Arg1(Arr1, Arr1 + (sizeof(Arr1) / sizeof(Arr1[0]))); int Arg2 = 2; verify_case(0, Arg2, minSteps(Arg0, Arg1)); } + void test_case_1() { string Arg0 = "AABBB"; string Arr1[] = {"BZ->Y", "ZY->X", "AB->Z"}; vector Arg1(Arr1, Arr1 + (sizeof(Arr1) / sizeof(Arr1[0]))); int Arg2 = 4; verify_case(1, Arg2, minSteps(Arg0, Arg1)); } + void test_case_2() { string Arg0 = "AAABB"; string Arr1[] = {"BZ->Y", "ZY->X", "AB->Z"}; vector Arg1(Arr1, Arr1 + (sizeof(Arr1) / sizeof(Arr1[0]))); int Arg2 = -1; verify_case(2, Arg2, minSteps(Arg0, Arg1)); } + void test_case_3() { string Arg0 = "AAXB"; string Arr1[] = {"BZ->Y", "ZY->X", "AB->Z"}; vector Arg1(Arr1, Arr1 + (sizeof(Arr1) / sizeof(Arr1[0]))); int Arg2 = 0; verify_case(3, Arg2, minSteps(Arg0, Arg1)); } + void test_case_4() { string Arg0 = "ABCDEFGHIJKLMNOPQRSTUVWYZABCDEFGHIJKLMNOPQRSTUVWYZ"; string Arr1[] = {"ABCE->Z", "RTYUIO->P", "QWER->T", "MN->B"}; vector Arg1(Arr1, Arr1 + (sizeof(Arr1) / sizeof(Arr1[0]))); int Arg2 = -1; verify_case(4, Arg2, minSteps(Arg0, Arg1)); } + +// END CUT HERE +}; +// BEGIN CUT HERE +int main() { + QuantumAlchemy().run_test(-1); +} +// END CUT HERE ADDED SRM/344/1C.cpp Index: SRM/344/1C.cpp ================================================================== --- SRM/344/1C.cpp +++ SRM/344/1C.cpp @@ -0,0 +1,84 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +using namespace std; +typedef long long LL; + +class FairTournament +{ +public: + vector memo; + + string add(const string& a, const string& b) + { + int n = max(a.size(), b.size()), carry=0; + string c(n, '0'); + for(int i=0; i= (1< string print_array(const vector &V) { ostringstream os; os << "{ "; for (typename vector::const_iterator iter = V.begin(); iter != V.end(); ++iter) os << '\"' << *iter << "\","; os << " }"; return os.str(); } + void verify_case(int Case, const string &Expected, const string &Received) { cerr << "Test Case #" << Case << "..."; if (Expected == Received) cerr << "PASSED" << endl; else { cerr << "FAILED" << endl; cerr << "\tExpected: \"" << Expected << '\"' << endl; cerr << "\tReceived: \"" << Received << '\"' << endl; } } + void test_case_0() { int Arg0 = 3; int Arg1 = 1; string Arg2 = "3"; verify_case(0, Arg2, countPermutations(Arg0, Arg1)); } + void test_case_1() { int Arg0 = 3; int Arg1 = 2; string Arg2 = "6"; verify_case(1, Arg2, countPermutations(Arg0, Arg1)); } + void test_case_2() { int Arg0 = 10; int Arg1 = 3; string Arg2 = "19708"; verify_case(2, Arg2, countPermutations(Arg0, Arg1)); } + void test_case_3() { int Arg0 = 100; int Arg1 = 1; string Arg2 = "573147844013817084101"; verify_case(3, Arg2, countPermutations(Arg0, Arg1)); } + +// END CUT HERE +}; +// BEGIN CUT HERE +int main() { + FairTournament().run_test(-1); +} +// END CUT HERE ADDED SRM/345/1A.cpp Index: SRM/345/1A.cpp ================================================================== --- SRM/345/1A.cpp +++ SRM/345/1A.cpp @@ -0,0 +1,75 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +using namespace std; +typedef long long LL; + +class Pathfinding +{ +public: + int getDirections(int x, int y) + { + if( x==0 && y==0 ) + return 0; + + int xincr = 0; + int yincr = 0; + if(x<0) yincr=1, x=-x; + if(y<0) xincr=1, y=-y; + if( x==0 ) + swap(xincr, yincr), swap(x, y); + + if( y==0 ) + { + if( yincr == 0 ) + return x; + else + if( x%2 == 1 ) + return x+2; + else + return x+4; + } + + if( 0==xincr && y%2==yincr || 0==yincr && x%2==xincr ) + return x+y; + + if( 0==xincr && x%2==xincr || 0==yincr && y%2==yincr ) + return x+y; + + if( 0==xincr || 0==yincr ) + return x+y+2; + + if( x%2==xincr || y%2==yincr ) + return x+y+2; + + return x+y+4; + } + +// BEGIN CUT HERE + public: + void run_test(int Case) { if ((Case == -1) || (Case == 0)) test_case_0(); if ((Case == -1) || (Case == 1)) test_case_1(); if ((Case == -1) || (Case == 2)) test_case_2(); if ((Case == -1) || (Case == 3)) test_case_3(); if ((Case == -1) || (Case == 4)) test_case_4(); } + private: + template string print_array(const vector &V) { ostringstream os; os << "{ "; for (typename vector::const_iterator iter = V.begin(); iter != V.end(); ++iter) os << '\"' << *iter << "\","; os << " }"; return os.str(); } + void verify_case(int Case, const int &Expected, const int &Received) { cerr << "Test Case #" << Case << "..."; if (Expected == Received) cerr << "PASSED" << endl; else { cerr << "FAILED" << endl; cerr << "\tExpected: \"" << Expected << '\"' << endl; cerr << "\tReceived: \"" << Received << '\"' << endl; } } + void test_case_0() { int Arg0 = 0; int Arg1 = -4; int Arg2 = 8; verify_case(0, Arg2, getDirections(Arg0, Arg1)); } + void test_case_1() { int Arg0 = 5; int Arg1 = -4; int Arg2 = 9; verify_case(1, Arg2, getDirections(Arg0, Arg1)); } + void test_case_2() { int Arg0 = 5; int Arg1 = 4; int Arg2 = 9; verify_case(2, Arg2, getDirections(Arg0, Arg1)); } + void test_case_3() { int Arg0 = -1; int Arg1 = -4; int Arg2 = 7; verify_case(3, Arg2, getDirections(Arg0, Arg1)); } + void test_case_4() { int Arg0 = 0; int Arg1 = 0; int Arg2 = 0; verify_case(4, Arg2, getDirections(Arg0, Arg1)); } + +// END CUT HERE +}; +// BEGIN CUT HERE +int main() { Pathfinding().run_test(-1); } +// END CUT HERE ADDED SRM/345/1B.cpp Index: SRM/345/1B.cpp ================================================================== --- SRM/345/1B.cpp +++ SRM/345/1B.cpp @@ -0,0 +1,62 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +using namespace std; +typedef long long LL; + +class StoneGame +{ +public: + int getScore(vector treasure, vector stones) + { + // Note that we always lose 'even' piles... + + vector ones; + int numOdds = 0; + int sumGt1 = 0; + + int n = treasure.size(); + for(int i=0; i string print_array(const vector &V) { ostringstream os; os << "{ "; for (typename vector::const_iterator iter = V.begin(); iter != V.end(); ++iter) os << '\"' << *iter << "\","; os << " }"; return os.str(); } + void verify_case(int Case, const int &Expected, const int &Received) { cerr << "Test Case #" << Case << "..."; if (Expected == Received) cerr << "PASSED" << endl; else { cerr << "FAILED" << endl; cerr << "\tExpected: \"" << Expected << '\"' << endl; cerr << "\tReceived: \"" << Received << '\"' << endl; } } + void test_case_0() { int Arr0[] = {3,2}; vector Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); int Arr1[] = {1,2}; vector Arg1(Arr1, Arr1 + (sizeof(Arr1) / sizeof(Arr1[0]))); int Arg2 = 5; verify_case(0, Arg2, getScore(Arg0, Arg1)); } + void test_case_1() { int Arr0[] = {5,4,3,2,1}; vector Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); int Arr1[] = {1,1,1,1,1}; vector Arg1(Arr1, Arr1 + (sizeof(Arr1) / sizeof(Arr1[0]))); int Arg2 = 9; verify_case(1, Arg2, getScore(Arg0, Arg1)); } + void test_case_2() { int Arr0[] = {5,5}; vector Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); int Arr1[] = {2,2}; vector Arg1(Arr1, Arr1 + (sizeof(Arr1) / sizeof(Arr1[0]))); int Arg2 = 0; verify_case(2, Arg2, getScore(Arg0, Arg1)); } + void test_case_3() { int Arr0[] = {1}; vector Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); int Arr1[] = {10}; vector Arg1(Arr1, Arr1 + (sizeof(Arr1) / sizeof(Arr1[0]))); int Arg2 = 0; verify_case(3, Arg2, getScore(Arg0, Arg1)); } + +// END CUT HERE +}; +// BEGIN CUT HERE +int main() { StoneGame().run_test(-1); } +// END CUT HERE ADDED SRM/345/1C.cpp Index: SRM/345/1C.cpp ================================================================== --- SRM/345/1C.cpp +++ SRM/345/1C.cpp @@ -0,0 +1,143 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +using namespace std; +typedef long long LL; + +LL next_combination(LL p) +{ + LL lsb = p & -p; + LL rem = p + lsb; + LL rit = rem & ~p; + return rem|(((rit/lsb)>>1)-1); +} + +class ByteLand +{ +public: + int buildCastles(vector road, vector distance, vector castle, int k) + { + int n = road.size(); + + // Construct the Graph + int d[50][50] = {}; + for(int v=0; v 1 ) + (is_possible((L+R)/2, d, n, castle, k) ? R : L) = (L+R)/2; + return R; + } + + bool is_possible( int D, int d[50][50], int n, const vector& c, int k ) + { + // the set of cities not covered yet + LL goal = (1LL << n) - 1; + for(int v=0; v mask; + for(int v=0; v& mask, int k ) + { + // optimizer + for(bool update=true; update; ) { + update = false; + + // if *it \subseteq *jt for some jt, then we NEVER use it + // if *it is the only mask covering some city, we ALWAYS use it + for(set::iterator it=mask.begin(); it!=mask.end(); ) { + bool isSubset = false; + LL onlyByMe = *it & goal; + for(set::iterator jt=mask.begin(); jt!=mask.end(); ++jt) + if( it!=jt ) { + onlyByMe &= ~*jt; + isSubset |= (*it & *jt & goal) == (*it & goal); + } + + update |= isSubset | !!onlyByMe; + + if( isSubset ) + mask.erase(it++); + else if( onlyByMe ) { + if( --k < 0 ) + return false; + goal &= ~*it; + mask.erase(it++); + } + else + ++it; + } + + if( mask.size()<=k || goal==0 ) + return true; + } + + // exhaustive search + vector ms(mask.begin(), mask.end()); + for(LL i=(1LL< string print_array(const vector &V) { ostringstream os; os << "{ "; for (typename vector::const_iterator iter = V.begin(); iter != V.end(); ++iter) os << '\"' << *iter << "\","; os << " }"; return os.str(); } + void verify_case(int Case, const int &Expected, const int &Received) { cerr << "Test Case #" << Case << "..."; if (Expected == Received) cerr << "PASSED" << endl; else { cerr << "FAILED" << endl; cerr << "\tExpected: \"" << Expected << '\"' << endl; cerr << "\tReceived: \"" << Received << '\"' << endl; } } + void test_case_0() { int Arr0[] = {1,2,3,4,0}; vector Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); int Arr1[] = {1,1,1,1,1}; vector Arg1(Arr1, Arr1 + (sizeof(Arr1) / sizeof(Arr1[0]))); vector Arg2; int Arg3 = 1; int Arg4 = 2; verify_case(0, Arg4, buildCastles(Arg0, Arg1, Arg2, Arg3)); } + void test_case_1() { int Arr0[] = {1,2,0}; vector Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); int Arr1[] = {1,2,3}; vector Arg1(Arr1, Arr1 + (sizeof(Arr1) / sizeof(Arr1[0]))); int Arr2[] = {2}; vector Arg2(Arr2, Arr2 + (sizeof(Arr2) / sizeof(Arr2[0]))); int Arg3 = 1; int Arg4 = 1; verify_case(1, Arg4, buildCastles(Arg0, Arg1, Arg2, Arg3)); } + void test_case_2() { int Arr0[] = {0,1}; vector Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); int Arr1[] = {1,1}; vector Arg1(Arr1, Arr1 + (sizeof(Arr1) / sizeof(Arr1[0]))); int Arr2[] = {1}; vector Arg2(Arr2, Arr2 + (sizeof(Arr2) / sizeof(Arr2[0]))); int Arg3 = 1; int Arg4 = 0; verify_case(2, Arg4, buildCastles(Arg0, Arg1, Arg2, Arg3)); } + void test_case_3() { int Arr0[] = {0,2,0,0,2,2,8,3,8,7}; vector Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); int Arr1[] = {10,9,1,8,1,3,7,2,8,1}; vector Arg1(Arr1, Arr1 + (sizeof(Arr1) / sizeof(Arr1[0]))); int Arr2[] = {3,4,6}; vector Arg2(Arr2, Arr2 + (sizeof(Arr2) / sizeof(Arr2[0]))); int Arg3 = 3; int Arg4 = 3; verify_case(3, Arg4, buildCastles(Arg0, Arg1, Arg2, Arg3)); } + void test_case_4() { int Arr0[] = {1, 0}; vector Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); int Arr1[] = {5, 10}; vector Arg1(Arr1, Arr1 + (sizeof(Arr1) / sizeof(Arr1[0]))); vector Arg2; int Arg3 = 1; int Arg4 = 5; verify_case(4, Arg4, buildCastles(Arg0, Arg1, Arg2, Arg3)); } + +// END CUT HERE +}; +// BEGIN CUT HERE +int main() { ByteLand().run_test(-1); } +// END CUT HERE ADDED SRM/346-U/1A.cpp Index: SRM/346-U/1A.cpp ================================================================== --- SRM/346-U/1A.cpp +++ SRM/346-U/1A.cpp @@ -0,0 +1,108 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +using namespace std; +typedef long long LL; +typedef complex CMP; + +LL gcd(LL a, LL b) +{ + while(a) + swap(a, b%=a); + return b; +} + +LL lcm(LL a, LL b) +{ + return a/gcd(a,b)*b; +} + +class CommonMultiples { public: + int countCommMult(vector a, int lower, int upper) + { + LL m = 1; + for(int i=0; i upper ) + return 0; + } + return upper/m - (lower-1)/m; + } +}; + +// 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 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(_, CommonMultiples().countCommMult(a, lower, upper));} +int main(){ + +CASE(0) + int a_[] = {1,2,3}; + vector a(a_, a_+sizeof(a_)/sizeof(*a_)); + int lower = 5; + int upper = 15; + int _ = 2; +END +CASE(1) + int a_[] = {1,2,4,8,16,32,64}; + vector a(a_, a_+sizeof(a_)/sizeof(*a_)); + int lower = 128; + int upper = 128; + int _ = 1; +END +CASE(2) + int a_[] = {2,3,5,7,11,13,17,19,23,29,31,37,41,43,49}; + vector a(a_, a_+sizeof(a_)/sizeof(*a_)); + int lower = 1; + int upper = 2000000000; + int _ = 0; +END +CASE(3) + int a_[] = {1,1,1}; + vector a(a_, a_+sizeof(a_)/sizeof(*a_)); + int lower = 1; + int upper = 2000000000; + int _ = 2000000000; +END +CASE(4) + int a_[] = {1}; + vector a(a_, a_+sizeof(a_)/sizeof(*a_)); + int lower = 1; + int upper = 1; + int _ = 1; +END +CASE(5) + int a_[] = {2,4}; + vector a(a_, a_+sizeof(a_)/sizeof(*a_)); + int lower = 4; + int upper = 8; + int _ = 2; +END + +} +// END CUT HERE ADDED SRM/346-U/1B.cpp Index: SRM/346-U/1B.cpp ================================================================== --- SRM/346-U/1B.cpp +++ SRM/346-U/1B.cpp @@ -0,0 +1,144 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +using namespace std; +typedef long long LL; +typedef complex CMP; + +class HeightRound { public: + typedef pair,int> key_t; + map memo; + + int score( int F, int B, int I, const vector& h ) + { + if( I == h.size()-1 ) + return max(h[I]-h[F], h[I]-h[B]); + + key_t key(make_pair(F,B), I); + if( memo.count(key) ) + return memo[key]; + + int s1 = max(h[I]-h[F], score(I, B, I+1, h)); + int s2 = max(h[I]-h[B], score(F, I, I+1, h)); + return memo[key] = min(s1, s2); + } + + void best( int F, int B, int I, const vector& h, int acc, deque& result ) + { + if( I == h.size()-1 ) { + result = deque(1, h[I]); + result.push_front(h[F]); + result.push_back(h[B]); + return; + } + + int s1 = max(acc, max(h[I]-h[F], score(I, B, I+1, h))); + int s2 = max(acc, max(h[I]-h[B], score(F, I, I+1, h))); + + if( s1<=s2 ) { // if tie, choose lexicographically earlier one + best( I, B, I+1, h, s1, result ); + result.push_front( h[F] ); + } + else { + best( F, I, I+1, h, s2, result ); + result.push_back( h[B] ); + } + } + + vector getBestRound(vector h) + { + // The answer must be of form: + // h[0], h[1], (...ascending...), h[$-1], (...descending...). + // Thus, I begin from + // F=1, B=0: [h[1], .........., h[0]] + // and fill each h[I=2,3,...] either to the left or right of the blank space, + // and choose the best one. To choose lex-first candidate, I use the "jo-seki" + // technique, which first computes only scores and then choose the instance + // lexicographically afterwards. + + sort(h.begin(), h.end()); + + deque result; + best( 1, 0, 2, h, 0, result ); + + rotate(result.begin(), result.end()-1, result.end()); + return vector(result.begin(), result.end()); + } +}; + +// 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 vector & Expected, const vector & 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(_, HeightRound().getBestRound(heights));} +int main(){ + +CASE(0) + int heights_[] = {1,2,3,4}; + vector heights(heights_, heights_+sizeof(heights_)/sizeof(*heights_)); + int __[] = {1, 2, 4, 3 }; + vector _(__, __+sizeof(__)/sizeof(*__)); +END +CASE(1) + int heights_[] = {1000,500,1}; + vector heights(heights_, heights_+sizeof(heights_)/sizeof(*heights_)); + int __[] = {1, 500, 1000 }; + vector _(__, __+sizeof(__)/sizeof(*__)); +END +CASE(2) + int heights_[] = {1,3,4,5,7}; + vector heights(heights_, heights_+sizeof(heights_)/sizeof(*heights_)); + int __[] = {1, 3, 5, 7, 4 }; + vector _(__, __+sizeof(__)/sizeof(*__)); +END +CASE(3) + int heights_[] = {1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, + 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1}; + vector heights(heights_, heights_+sizeof(heights_)/sizeof(*heights_)); + int __[] = {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 }; + vector _(__, __+sizeof(__)/sizeof(*__)); +END +CASE(4) + int heights_[] = {1,2,1}; + vector heights(heights_, heights_+sizeof(heights_)/sizeof(*heights_)); + int __[] = {1,1,2}; + vector _(__, __+sizeof(__)/sizeof(*__)); +END +CASE(5) + int heights_[] = {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,1000}; + vector heights(heights_, heights_+sizeof(heights_)/sizeof(*heights_)); + int __[] = {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,1000,49}; + vector _(__, __+sizeof(__)/sizeof(*__)); +END +CASE(6) + int heights_[] = {7, 43, 3, 6, 32, 2, 42, 23, 1, 30}; + vector heights(heights_, heights_+sizeof(heights_)/sizeof(*heights_)); + int __[] = {1, 2, 3, 6, 7, 30, 32, 42, 43, 23}; + vector _(__, __+sizeof(__)/sizeof(*__)); +END + +} +// END CUT HERE ADDED SRM/346-U/2C.cpp Index: SRM/346-U/2C.cpp ================================================================== --- SRM/346-U/2C.cpp +++ SRM/346-U/2C.cpp @@ -0,0 +1,158 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +using namespace std; +typedef long long LL; +typedef complex CMP; + +class FastPostman { public: + struct vert + { + int Ldone, Rdone; + bool nowAtL; + + bool operator<( const vert& rhs ) const { + if( Ldone != rhs.Ldone ) return Ldone < rhs.Ldone; + if( Rdone != rhs.Rdone ) return Rdone < rhs.Rdone; + return nowAtL < rhs.nowAtL; + } + vert(int l, int r, bool b) : Ldone(l), Rdone(r), nowAtL(b) {} + }; + typedef vert edge; + typedef pair cedge; + + int minTime(vector address, vector maxTime, int initialPos) + { + const int N = address.size(); + + vector< pair > L, R; + R.push_back( make_pair(initialPos,0) ); + for(int i=0; i, greater > Q; + set V; + Q.push( cedge(0,S) ); + while( !Q.empty() ) + { + int c = Q.top().first; + vert v = Q.top().second; + Q.pop(); + if( V.count(v) ) + continue; + if( v.Ldone == L.size() && v.Rdone == R.size() ) + return c; + V.insert(v); + + if( v.Ldone < L.size() ) { + vert u(v.Ldone+1, v.Rdone, true); + int cu = c + abs( (v.nowAtL ? L[v.Ldone-1] : R[v.Rdone-1]).first - L[v.Ldone].first ); + if( !V.count(u) && cu <= L[v.Ldone].second ) + Q.push( cedge(cu,u) ); + } + if( v.Rdone < R.size() ) { + vert u(v.Ldone, v.Rdone+1, false); + int cu = c + abs( (v.nowAtL ? L[v.Ldone-1] : R[v.Rdone-1]).first - R[v.Rdone].first ); + if( !V.count(u) && cu <= R[v.Rdone].second ) + Q.push( cedge(cu,u) ); + } + } + return -1; + } +}; + +// 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 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(_, FastPostman().minTime(address, maxTime, initialPos));} +int main(){ + +CASE(0) + int address_[] = {1,3,5,7}; + vector address(address_, address_+sizeof(address_)/sizeof(*address_)); + int maxTime_[] = {9,2,5,100}; + vector maxTime(maxTime_, maxTime_+sizeof(maxTime_)/sizeof(*maxTime_)); + int initialPos = 4; + int _ = 13; +END +CASE(1) + int address_[] = {1,5}; + vector address(address_, address_+sizeof(address_)/sizeof(*address_)); + int maxTime_[] = {6,2}; + vector maxTime(maxTime_, maxTime_+sizeof(maxTime_)/sizeof(*maxTime_)); + int initialPos = 3; + int _ = 6; +END +CASE(2) + int address_[] = {1000000}; + vector address(address_, address_+sizeof(address_)/sizeof(*address_)); + int maxTime_[] = {45634}; + vector maxTime(maxTime_, maxTime_+sizeof(maxTime_)/sizeof(*maxTime_)); + int initialPos = 876; + int _ = -1; +END +CASE(3) + int address_[] = {1,7,10,4}; + vector address(address_, address_+sizeof(address_)/sizeof(*address_)); + int maxTime_[] = {15,6,28,39}; + vector maxTime(maxTime_, maxTime_+sizeof(maxTime_)/sizeof(*maxTime_)); + int initialPos = 2; + int _ = 20; +END +CASE(4) + int address_[] = {1000000,1000000,1000000,1000000}; + vector address(address_, address_+sizeof(address_)/sizeof(*address_)); + int maxTime_[] = {563,23452,32426,1}; + vector maxTime(maxTime_, maxTime_+sizeof(maxTime_)/sizeof(*maxTime_)); + int initialPos = 1000000; + int _ = 0; +END +CASE(5) + int address_[] = {1}; + vector address(address_, address_+sizeof(address_)/sizeof(*address_)); + int maxTime_[] = {2}; + vector maxTime(maxTime_, maxTime_+sizeof(maxTime_)/sizeof(*maxTime_)); + int initialPos = {3}; + int _ = 2; +END +CASE(6) + int address_[] = {0, 1000000}; + vector address(address_, address_+sizeof(address_)/sizeof(*address_)); + int maxTime_[] = {500000, 1500000}; + vector maxTime(maxTime_, maxTime_+sizeof(maxTime_)/sizeof(*maxTime_)); + int initialPos = 500000; + int _ = 1500000; +END + +} +// END CUT HERE ADDED SRM/347-U/1A.cpp Index: SRM/347-U/1A.cpp ================================================================== --- SRM/347-U/1A.cpp +++ SRM/347-U/1A.cpp @@ -0,0 +1,194 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +using namespace std; +typedef long long LL; +typedef complex CMP; + +class Aircraft { public: + double f(const double p[], const double v[], double t) + { + return sqrt( (p[0]+t*v[0])*(p[0]+t*v[0]) + (p[1]+t*v[1])*(p[1]+t*v[1]) + (p[2]+t*v[2])*(p[2]+t*v[2]) ); + } + string nearMiss(vector p1, vector v1, vector p2, vector v2, int R) + { + const double p[] = {p1[0]-p2[0], p1[1]-p2[1], p1[2]-p2[2]}; + const double v[] = {v1[0]-v2[0], v1[1]-v2[1], v1[2]-v2[2]}; + + double x = 0.0; + double w = 1e+10; + for(int k=0; k<1000000; ++k) + { + double y = 2*x/3 + w/3; + double z = x/3 + 2*w/3; + double fx = f(p,v,x); + double fy = f(p,v,y); + double fz = f(p,v,z); + double fw = f(p,v,w); + if( fx < fy ) + w = y; + else if( fz > fw ) + x = z; + else if( fy < fz ) + w = z; + else + x = y; + } + + return f(p,v,x)-0.000001<=R ? "YES" : "NO"; + } +}; + +// 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(_, Aircraft().nearMiss(p1, v1, p2, v2, R));} +int main(){ + +CASE(0) + int p1_[] = {15,50,5}; + vector p1(p1_, p1_+sizeof(p1_)/sizeof(*p1_)); + int v1_[] = {25,1,0}; + vector v1(v1_, v1_+sizeof(v1_)/sizeof(*v1_)); + int p2_[] = {161,102,9}; + vector p2(p2_, p2_+sizeof(p2_)/sizeof(*p2_)); + int v2_[] = {-10,-10,-1}; + vector v2(v2_, v2_+sizeof(v2_)/sizeof(*v2_)); + int R = 10; + string _ = "YES"; +END +CASE(1) + int p1_[] = {0,0,0}; + vector p1(p1_, p1_+sizeof(p1_)/sizeof(*p1_)); + int v1_[] = {2,2,0}; + vector v1(v1_, v1_+sizeof(v1_)/sizeof(*v1_)); + int p2_[] = {9,0,5}; + vector p2(p2_, p2_+sizeof(p2_)/sizeof(*p2_)); + int v2_[] = {-2,2,0}; + vector v2(v2_, v2_+sizeof(v2_)/sizeof(*v2_)); + int R = 5; + string _ = "YES"; +END +CASE(2) + int p1_[] = {0,0,0}; + vector p1(p1_, p1_+sizeof(p1_)/sizeof(*p1_)); + int v1_[] = {-2,2,0}; + vector v1(v1_, v1_+sizeof(v1_)/sizeof(*v1_)); + int p2_[] = {9,0,5}; + vector p2(p2_, p2_+sizeof(p2_)/sizeof(*p2_)); + int v2_[] = {2,2,0}; + vector v2(v2_, v2_+sizeof(v2_)/sizeof(*v2_)); + int R = 5; + string _ = "NO"; +END +CASE(3) + int p1_[] = {-2838,-7940,-2936}; + vector p1(p1_, p1_+sizeof(p1_)/sizeof(*p1_)); + int v1_[] = {1,1,-2}; + vector v1(v1_, v1_+sizeof(v1_)/sizeof(*v1_)); + int p2_[] = {532,3850,9590}; + vector p2(p2_, p2_+sizeof(p2_)/sizeof(*p2_)); + int v2_[] = {1,0,-3}; + vector v2(v2_, v2_+sizeof(v2_)/sizeof(*v2_)); + int R = 3410; + string _ = "YES"; +END +CASE(4) + int p1_[] = {-8509,9560,345}; + vector p1(p1_, p1_+sizeof(p1_)/sizeof(*p1_)); + int v1_[] = {-89,-33,62}; + vector v1(v1_, v1_+sizeof(v1_)/sizeof(*v1_)); + int p2_[] = {-5185,-1417,2846}; + vector p2(p2_, p2_+sizeof(p2_)/sizeof(*p2_)); + int v2_[] = {-58,24,26}; + vector v2(v2_, v2_+sizeof(v2_)/sizeof(*v2_)); + int R = 8344; + string _ = "YES"; +END +CASE(5) + int p1_[] = {-7163,-371,-2459}; + vector p1(p1_, p1_+sizeof(p1_)/sizeof(*p1_)); + int v1_[] = {-59,-41,-14}; + vector v1(v1_, v1_+sizeof(v1_)/sizeof(*v1_)); + int p2_[] = {-2398,-426,-5487}; + vector p2(p2_, p2_+sizeof(p2_)/sizeof(*p2_)); + int v2_[] = {-43,27,67}; + vector v2(v2_, v2_+sizeof(v2_)/sizeof(*v2_)); + int R = 5410; + string _ = "NO"; +END +CASE(6) + int p1_[] = {1774,-4491,7810}; + vector p1(p1_, p1_+sizeof(p1_)/sizeof(*p1_)); + int v1_[] = {-12,19,-24}; + vector v1(v1_, v1_+sizeof(v1_)/sizeof(*v1_)); + int p2_[] = {2322,3793,9897}; + vector p2(p2_, p2_+sizeof(p2_)/sizeof(*p2_)); + int v2_[] = {-12,19,-24}; + vector v2(v2_, v2_+sizeof(v2_)/sizeof(*v2_)); + int R = 10000; + string _ = "YES"; +END +CASE(7) + int p1_[] = {3731,8537,5661}; + vector p1(p1_, p1_+sizeof(p1_)/sizeof(*p1_)); + int v1_[] = {-70,71,32}; + vector v1(v1_, v1_+sizeof(v1_)/sizeof(*v1_)); + int p2_[] = {8701,-1886,-5115}; + vector p2(p2_, p2_+sizeof(p2_)/sizeof(*p2_)); + int v2_[] = {28,-13,7}; + vector v2(v2_, v2_+sizeof(v2_)/sizeof(*v2_)); + int R = 9766; + string _ = "NO"; +END +CASE(8) + int p1_[] = {0,0,0}; + vector p1(p1_, p1_+sizeof(p1_)/sizeof(*p1_)); + int v1_[] = {0,0,0}; + vector v1(v1_, v1_+sizeof(v1_)/sizeof(*v1_)); + int p2_[] = {1,0,0}; + vector p2(p2_, p2_+sizeof(p2_)/sizeof(*p2_)); + int v2_[] = {0,0,0}; + vector v2(v2_, v2_+sizeof(v2_)/sizeof(*v2_)); + int R = 1; + string _ = "YES"; +END +CASE(9) + int p1_[] = {0,0,0}; + vector p1(p1_, p1_+sizeof(p1_)/sizeof(*p1_)); + int v1_[] = {0,0,0}; + vector v1(v1_, v1_+sizeof(v1_)/sizeof(*v1_)); + int p2_[] = {2,0,0}; + vector p2(p2_, p2_+sizeof(p2_)/sizeof(*p2_)); + int v2_[] = {0,0,0}; + vector v2(v2_, v2_+sizeof(v2_)/sizeof(*v2_)); + int R = 1; + string _ = "NO"; +END + +} +// END CUT HERE ADDED SRM/347-U/1B.cpp Index: SRM/347-U/1B.cpp ================================================================== --- SRM/347-U/1B.cpp +++ SRM/347-U/1B.cpp @@ -0,0 +1,105 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +using namespace std; +typedef long long LL; +typedef complex CMP; + +class FlightScheduler { public: + double f(double n, int distance, int K, int emptyMass, int takeoffFuel) + { + return n * (exp(distance/n / K) * emptyMass - emptyMass + takeoffFuel); + } + double minimizeFuel(int distance, int K, int emptyMass, int takeoffFuel) + { + LL x = 1; + LL w = (1LL<<60); + while( w-x > 10 ) + { + LL y = 2*x/3 + w/3; + LL z = x/3 + 2*w/3; + double fx = f(x, distance, K, emptyMass, takeoffFuel); + double fy = f(y, distance, K, emptyMass, takeoffFuel); + double fz = f(z, distance, K, emptyMass, takeoffFuel); + double fw = f(w, distance, K, emptyMass, takeoffFuel); + if( fx < fy ) w = y; + else if( fz > fw ) x = z; + else if( fy < fz ) w = z; + else x = y; + } + + double fuMin = 1e+300; + for(LL n=x; n<=w; ++n) + fuMin = min(fuMin, f(n, distance, K, emptyMass, takeoffFuel)); + return fuMin; + } +}; + +// 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 double& Expected, const double& Received) { + double diff = Expected - Received; if (diff < 0) diff = -diff; bool ok = (diff < 1e-9); + 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(_, FlightScheduler().minimizeFuel(distance, K, emptyMass, takeoffFuel));} +int main(){ + +CASE(0) + int distance = 40000; + int K = 100000; + int emptyMass = 150000; + int takeoffFuel = 5000; + double _ = 76420.82744805096; +END +CASE(1) + int distance = 40000; + int K = 55000; + int emptyMass = 150000; + int takeoffFuel = 5000; + double _ = 138450.61724934017; +END +CASE(2) + int distance = 1000; + int K = 500; + int emptyMass = 1000; + int takeoffFuel = 50; + double _ = 2664.9853821314487; +END +CASE(3) + int distance = 10000; + int K = 100; + int emptyMass = 200; + int takeoffFuel = 5; + double _ = 24635.869665316768; +END +CASE(4) + int distance = 140000; + int K = 4; + int emptyMass = 10000; + int takeoffFuel = 10; + double _ = 3.6576871282155204E8; +END + +} +// END CUT HERE ADDED SRM/347-U/2C.cpp Index: SRM/347-U/2C.cpp ================================================================== --- SRM/347-U/2C.cpp +++ SRM/347-U/2C.cpp @@ -0,0 +1,172 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +using namespace std; +typedef long long LL; +typedef complex CMP; + +static const int INF = 0x3fffffff; + +class TaxiManager { public: + int schedule(vector roads, vector customers) + { + const int V = roads.size(); + vector< vector > d(V, vector(V)); + for(int i=0; i > c; + for(int i=0; i> f >> t; + c.push_back( make_pair(f,t) ); + } + + int best = INF; + for(int mask=0; mask<(1<, int> memo; + int tsp(const vector< vector >& d, const vector< pair >& c, int mask, int cur=0) + { + if( mask == 0 ) + return d[cur][0]; + + pair key(mask, cur); + if( memo.count(key) ) return memo[key]; + + int best = INF; + for(int next=0; (1< +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(_, TaxiManager().schedule(roads, customers));} +int main(){ + +CASE(0) + string roads_[] = {"020200" +,"202020" +,"020002" +,"200020" +,"020202" +,"002020"}; + vector roads(roads_, roads_+sizeof(roads_)/sizeof(*roads_)); + string customers_[] = {"5 3","2 4","1 5","3 2"}; + vector customers(customers_, customers_+sizeof(customers_)/sizeof(*customers_)); + int _ = 16; +END +CASE(1) + string roads_[] = +{"00020251090265906661" +,"00763002550100090081" +,"06003699000080062771" +,"00000710460400035310" +,"50000039119198350060" +,"66060004050810046028" +,"02333108565000200880" +,"40212560000209205231" +,"02601150098329905062" +,"00210383709951005203" +,"10111087340780827070" +,"05065800003095040140" +,"15604020082000100090" +,"83430030070580600750" +,"10588355007006001150" +,"14400080790005400536" +,"23400990400933060004" +,"11053016300602000090" +,"90040920084059282502" +,"61300007077904050900"}; + vector roads(roads_, roads_+sizeof(roads_)/sizeof(*roads_)); + string customers_[] = {"0 19","4 16","15 16","4 18","2 7","9 15","11 6","7 13","19 13","12 19","14 12","16 1"}; + vector customers(customers_, customers_+sizeof(customers_)/sizeof(*customers_)); + int _ = 33; +END +CASE(2) + string roads_[] = +{"095222800320504" +,"107600288090501" +,"760973530769345" +,"963093337510830" +,"338404069255826" +,"291700050155264" +,"002783031709004" +,"404730701707712" +,"068870030090995" +,"320025180036103" +,"468695042801904" +,"233626561000105" +,"070014432197086" +,"887301000143802" +,"230852749990330"}; + vector roads(roads_, roads_+sizeof(roads_)/sizeof(*roads_)); + string customers_[] = {"3 6","0 4","2 7","9 7","13 9","1 6","7 13","14 2","8 7","10 1","11 13","7 12"}; + vector customers(customers_, customers_+sizeof(customers_)/sizeof(*customers_)); + int _ = 28; +END +CASE(3) + string roads_[] = {"00401","50990","00062","08008","03000"}; + vector roads(roads_, roads_+sizeof(roads_)/sizeof(*roads_)); + string customers_[] = {"2 4"}; + vector customers(customers_, customers_+sizeof(customers_)/sizeof(*customers_)); + int _ = 14; +END +/* +CASE(4) + string roads_[] = ; + vector roads(roads_, roads_+sizeof(roads_)/sizeof(*roads_)); + string customers_[] = ; + vector customers(customers_, customers_+sizeof(customers_)/sizeof(*customers_)); + int _ = ; +END +CASE(5) + string roads_[] = ; + vector roads(roads_, roads_+sizeof(roads_)/sizeof(*roads_)); + string customers_[] = ; + vector customers(customers_, customers_+sizeof(customers_)/sizeof(*customers_)); + int _ = ; +END +*/ +} +// END CUT HERE ADDED SRM/348-U/1A.cpp Index: SRM/348-U/1A.cpp ================================================================== --- SRM/348-U/1A.cpp +++ SRM/348-U/1A.cpp @@ -0,0 +1,94 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +using namespace std; +typedef long long LL; +typedef complex CMP; + +class LostParentheses { public: + int minResult(string e) + { + int sum = 0; + bool prevOpPlus = true; + bool minusFound = false; + for(int p=0,i=0; i<=e.size(); ++i) + if( i==e.size() || e[i] == '-' ) + { + stringstream ss; + ss << e.substr(p, i-p); + int num; + ss >> num; + + sum += (prevOpPlus ? +num : -num); + p = i+1; + minusFound = true; + prevOpPlus = false; + } + else if( e[i] == '+' ) + { + stringstream ss; + ss << e.substr(p, i-p); + int num; + ss >> num; + + sum += (prevOpPlus ? +num : -num); + p = i+1; + prevOpPlus = !minusFound; + } + return sum; + } +}; + +// 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 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(_, LostParentheses().minResult(e));} +int main(){ + +CASE(0) + string e = "55-50+40"; + int _ = -35; +END +CASE(1) + string e = "10+20+30+40"; + int _ = 100; +END +CASE(2) + string e = "00009-00009"; + int _ = 0; +END +CASE(3) + string e = "1"; + int _ = 1; +END +CASE(4) + string e = "1-2+3"; + int _ = -4; +END + +} +// END CUT HERE ADDED SRM/348-U/1B-U.cpp Index: SRM/348-U/1B-U.cpp ================================================================== --- SRM/348-U/1B-U.cpp +++ SRM/348-U/1B-U.cpp @@ -0,0 +1,130 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +using namespace std; +typedef long long LL; +typedef complex CMP; + +template + To lexical_cast( const From& f ) + { + stringstream ss; + ss << f; + To t; + ss >> t; + return t; + } + + +class RailwayTickets { public: + int minRejects(vector travel, int seats) + { + typedef pair event; // [true,A --- false,B) + vector evs; + + for(int i=0; i>s; ) + { + int k = s.find('-'); + int A = lexical_cast( s.substr(0, k) ); + int B = lexical_cast( s.substr(k+1) ); + evs.push_back( event(true,A) ); + evs.push_back( event(false,B) ); + } + } + + sort( evs.begin(), evs.end() ); + + vector< vector > minDeny(evs.size()+1, vector(seats+1)); + for(int ti=evs.size()-1; ti>=0; --ti) + for(int si=0; si<=seats; ++si) + if( evs[ti].first ) + { + if( si==0 ) + minDeny[ti][si] = minDeny[ti+1][si]+1; + else + minDeny[ti][si] = min( minDeny[ti+1][si]+1, minDeny[ti+1][si-1] ); + } + else + { + minDeny[ti][si] = minDeny[ti+1][si+1]; + } + return minDeny[0][seats]; + } +}; + +// 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 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(_, RailwayTickets().minRejects(travel, seats));} +int main(){ + +CASE(0) + string travel_[] = {"1-3 3-5", + "2-4 4-6", + "1-2 2-3 3-4 4-5"}; + vector travel(travel_, travel_+sizeof(travel_)/sizeof(*travel_)); + int seats = 2; + int _ = 2; +END +CASE(1) + string travel_[] = {"1-10000","2-10000","1-9999","2-9999","5000-5001"}; + vector travel(travel_, travel_+sizeof(travel_)/sizeof(*travel_)); + int seats = 3; + int _ = 2; +END +CASE(2) + string travel_[] = {"1-2 2-5 2-8 234-236 56-878 6-34", + "234-776 3242-8000 999-1000 3-14", + "121-122 435-3455 123-987 77-999"}; + vector travel(travel_, travel_+sizeof(travel_)/sizeof(*travel_)); + int seats = 1000; + int _ = 0; +END +CASE(3) + string travel_[] = {"1-2 2-3 3-4 4-5 5-6 6-7 1-3 2-7 1-2 1-4"}; + vector travel(travel_, travel_+sizeof(travel_)/sizeof(*travel_)); + int seats = 1; + int _ = 4; +END +CASE(4) + string travel_[] = {"1-2","1-2"}; + vector travel(travel_, travel_+sizeof(travel_)/sizeof(*travel_)); + int seats = 1; + int _ = 1; +END +CASE(5) + string travel_[] = {"1-2"}; + vector travel(travel_, travel_+sizeof(travel_)/sizeof(*travel_)); + int seats = 1; + int _ = 0; +END + +} +// END CUT HERE ADDED SRM/348-U/2C.cpp Index: SRM/348-U/2C.cpp ================================================================== --- SRM/348-U/2C.cpp +++ SRM/348-U/2C.cpp @@ -0,0 +1,100 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +using namespace std; +typedef long long LL; +typedef complex CMP; + +class IncreasingSubsequences { public: + long long count(vector a) + { + a.insert(a.begin(), -1); + + int N = a.size(); + + vector< vector > next(N); + for(int i=0; i num(N); + for(int i=N-1; i>=0; --i) { + if( next[i].size() == 0 ) + num[i] = 1; + else + for(int j=0; j +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 long long& Expected, const long long& 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(_, IncreasingSubsequences().count(a));} +int main(){ + +CASE(0) + int a_[] = {1,3,2,6,4,5}; + vector a(a_, a_+sizeof(a_)/sizeof(*a_)); + long long _ = 4LL; +END +CASE(1) + int a_[] = {1000000000,100000000,10000000,1000000,100000,10000,1000,100,10,1}; + vector a(a_, a_+sizeof(a_)/sizeof(*a_)); + long long _ = 10LL; +END +CASE(2) + int a_[] = {1,10,100,1000,10000,100000,1000000,10000000,100000000,1000000000}; + vector a(a_, a_+sizeof(a_)/sizeof(*a_)); + long long _ = 1LL; +END +CASE(3) + int a_[] = {564,234,34,4365,424,2234,306,21,934,592,195,2395,2396,29345,13295423,23945,2}; + vector a(a_, a_+sizeof(a_)/sizeof(*a_)); + long long _ = 41LL; +END +CASE(4) + int a_[] = {1}; + vector a(a_, a_+sizeof(a_)/sizeof(*a_)); + long long _ = 1LL; +END +CASE(5) + int a_[] = {5,4,3,2,1}; + vector a(a_, a_+sizeof(a_)/sizeof(*a_)); + long long _ = 5LL; +END + +} +// END CUT HERE ADDED SRM/349-U/1A.cpp Index: SRM/349-U/1A.cpp ================================================================== --- SRM/349-U/1A.cpp +++ SRM/349-U/1A.cpp @@ -0,0 +1,134 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +using namespace std; +typedef long long LL; +typedef complex CMP; + +class RadarFinder { public: + int possibleLocations(int x1, int y1, int r1, int x2, int y2, int r2) + { + if( x1==x2 && y1==y2 ) + return r1==r2 ? -1 : 0; + LL rr = LL(x1-x2)*(x1-x2) + LL(y1-y2)*(y1-y2); + LL p12 = LL(r1+r2)*(r1+r2); + LL m12 = LL(r1-r2)*(r1-r2); + if( rr > p12 ) + return 0; + else if( rr == p12 ) + return 1; + else + if( rr < m12 ) + return 0; + else if( rr == m12 ) + return 1; + else + return 2; + } +}; + +// 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 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(_, RadarFinder().possibleLocations(x1, y1, r1, x2, y2, r2));} +int main(){ + +CASE(0) + int x1 = 0; + int y1 = 0; + int r1 = 13; + int x2 = 40; + int y2 = 0; + int r2 = 37; + int _ = 2; +END +CASE(1) + int x1 = 0; + int y1 = 0; + int r1 = 3; + int x2 = 0; + int y2 = 7; + int r2 = 4; + int _ = 1; +END +CASE(2) + int x1 = 0; + int y1 = 0; + int r1 = 5; + int x2 = 10; + int y2 = 10; + int r2 = 3; + int _ = 0; +END +CASE(3) + int x1 = 0; + int y1 = 0; + int r1 = 1; + int x2 = 0; + int y2 = 0; + int r2 = 1; + int _ = -1; +END +CASE(4) + int x1 = 1; + int y1 = 1; + int r1 = 1; + int x2 = 1; + int y2 = 1; + int r2 = 2; + int _ = 0; +END +CASE(5) + int x1 = -100000000; + int y1 = -100000000; + int r1 = 141421357; + int x2 = 100000000; + int y2 = 100000000; + int r2 = 141421357; + int _ = 2; +END +CASE(6) + int x1 = 0; + int y1 = 0; + int r1 = 5; + int x2 = 1; + int y2 = 0; + int r2 = 4; + int _ = 1; +END +CASE(7) + int x1 = -1000000000; + int y1 = -1000000000; + int r1 = 1000000000; + int x2 = -999999999; + int y2 = 1000000000; + int r2 = 1000000000; + int _ = 0; +END + +} +// END CUT HERE ADDED SRM/349-U/1B.cpp Index: SRM/349-U/1B.cpp ================================================================== --- SRM/349-U/1B.cpp +++ SRM/349-U/1B.cpp @@ -0,0 +1,88 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +using namespace std; +typedef long long LL; +typedef complex CMP; + +class DiceGames { public: + long long countFormations(vector sides) + { + sort(sides.begin(), sides.end()); + + vector dp(sides.back(), 0); + dp[0] = 1; + for(int i=0; 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 long long& Expected, const long long& 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(_, DiceGames().countFormations(sides));} +int main(){ + +CASE(0) + int sides_[] = {4}; + vector sides(sides_, sides_+sizeof(sides_)/sizeof(*sides_)); + long long _ = 4LL; +END +CASE(1) + int sides_[] = {2, 2}; + vector sides(sides_, sides_+sizeof(sides_)/sizeof(*sides_)); + long long _ = 3LL; +END +CASE(2) + int sides_[] = {4, 4}; + vector sides(sides_, sides_+sizeof(sides_)/sizeof(*sides_)); + long long _ = 10LL; +END +CASE(3) + int sides_[] = {3, 4}; + vector sides(sides_, sides_+sizeof(sides_)/sizeof(*sides_)); + long long _ = 9LL; +END +CASE(4) + int sides_[] = {4, 5, 6}; + vector sides(sides_, sides_+sizeof(sides_)/sizeof(*sides_)); + long long _ = 48LL; +END +CASE(5) + int sides_[] = {1}; + vector sides(sides_, sides_+sizeof(sides_)/sizeof(*sides_)); + long long _ = 1LL; +END +CASE(6) + int sides_[] = {1,1,1,1,1,1,1,1,1,1}; + vector sides(sides_, sides_+sizeof(sides_)/sizeof(*sides_)); + long long _ = 1LL; +END + +} +// END CUT HERE ADDED SRM/349-U/1C-U.cpp Index: SRM/349-U/1C-U.cpp ================================================================== --- SRM/349-U/1C-U.cpp +++ SRM/349-U/1C-U.cpp @@ -0,0 +1,147 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +using namespace std; +typedef long long LL; +typedef complex CMP; + +class LastMarble { public: + typedef pair rb; + typedef map state; + + double winningChance(int red, int blue, int removed) + { + state s; + addNextStates( red, blue, 1.0, removed, s ); + return winChance(s); + } + + double winChance( state& s ) + { + if( s.empty() ) + return 1.0; // any value is ok because it will be multiplied by 0.0 + + int r_plus_b = s.begin()->first.first + s.begin()->first.second; + + double lc = 1.0; + for(int take=1; take<=min(3,r_plus_b); ++take) + { + state ns; + for(state::iterator it=s.begin(); it!=s.end(); ++it) + addNextStates(it->first.first, it->first.second, it->second, take, ns); + + double nowLoseChance = 0.0; + for(state::iterator it=ns.begin(); it!=ns.end(); ) + if( it->first.first == 0 ) { + nowLoseChance += it->second; + ns.erase( it++ ); + } else { + ++it; + } + + double continueChance = 1.0 - nowLoseChance; + for(state::iterator it=ns.begin(); it!=ns.end(); ++it) + it->second /= continueChance; + double loseChance = nowLoseChance + winChance(ns)*continueChance; + lc = min(lc, loseChance); + } + return 1.0 - lc; + } + + void addNextStates(int r, int b, double p, int take, state& m) + { + if( take == 0 ) { + m[rb(r,b)] += p; + return; + } + + state tmp; + tmp[rb(r,b)] = p; + + while( take --> 0 ) + { + state tmp2; + state& tgt = (take ? tmp2 : m); + for(state::iterator it=tmp.begin(); it!=tmp.end(); ++it) + { + int r = it->first.first; + int b = it->first.second; + double p = it->second; + if( r>0 ) tgt[rb(r-1,b)] += p * r / (r+b); + if( b>0 ) tgt[rb(r,b-1)] += p * b / (r+b); + } + tmp.swap(tmp2); + } + } +}; + +// 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 double& Expected, const double& Received) { + double diff = Expected - Received; if (diff < 0) diff = -diff; bool ok = (diff < 1e-9); + 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(_, LastMarble().winningChance(red, blue, removed));} +int main(){ + +CASE(0) + int red = 1; + int blue = 1; + int removed = 0; + double _ = 0.5; +END +CASE(1) + int red = 1; + int blue = 2; + int removed = 0; + double _ = 0.3333333333333333; +END +CASE(2) + int red = 2; + int blue = 1; + int removed = 0; + double _ = 0.6666666666666666; +END +CASE(3) + int red = 2; + int blue = 2; + int removed = 1; + double _ = 0.5; +END +/* +CASE(4) + int red = ; + int blue = ; + int removed = ; + double _ = ; +END +CASE(5) + int red = ; + int blue = ; + int removed = ; + double _ = ; +END +*/ +} +// END CUT HERE ADDED SRM/350-U/1A.cpp Index: SRM/350-U/1A.cpp ================================================================== --- SRM/350-U/1A.cpp +++ SRM/350-U/1A.cpp @@ -0,0 +1,99 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +using namespace std; +typedef long long LL; +typedef complex CMP; + +class SumsOfPerfectPowers { public: + int howMany(int lowerBound, int upperBound) + { + set sq; + sq.insert(0); + sq.insert(1); + for(LL n=2; n*n<=upperBound; ++n) + for(LL nk=n*n; nk<=upperBound; nk*=n) + sq.insert(nk); + vector sqv( sq.begin(), sq.end() ); + + vector pp; + for(vector::iterator it=sqv.begin(); it!=sqv.end(); ++it) + for(vector::iterator jt=it; jt!=sqv.end() && *it+*jt<=upperBound; ++jt) + pp.push_back( *it + *jt ); + sort( pp.begin(), pp.end() ); + pp.erase( unique(pp.begin(),pp.end()), pp.end() ); + + vector::iterator s = lower_bound( pp.begin(), pp.end(), lowerBound ); + vector::iterator e = upper_bound( pp.begin(), pp.end(), upperBound ); + return distance(s, e); + } +}; + +// 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 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(_, SumsOfPerfectPowers().howMany(lowerBound, upperBound));} +int main(){ + +CASE(0) + int lowerBound = 0; + int upperBound = 1; + int _ = 2; +END +CASE(1) + int lowerBound = 5; + int upperBound = 6; + int _ = 1; +END +CASE(2) + int lowerBound = 25; + int upperBound = 30; + int _ = 5; +END +CASE(3) + int lowerBound = 103; + int upperBound = 103; + int _ = 0; +END +CASE(4) + int lowerBound = 1; + int upperBound = 100000; + int _ = 33604; +END +CASE(5) + int lowerBound = 0; + int upperBound = 0; + int _ = 1; +END +CASE(6) + int lowerBound = 0; + int upperBound = 5000000; + int _ = 1272868; +END + +} +// END CUT HERE ADDED SRM/350-U/1B.cpp Index: SRM/350-U/1B.cpp ================================================================== --- SRM/350-U/1B.cpp +++ SRM/350-U/1B.cpp @@ -0,0 +1,162 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +using namespace std; +typedef long long LL; +typedef complex CMP; + +LL comb(LL n, LL k) +{ + k = min(k, n-k); + + LL c = 1; + for(LL i=0; i C ) + return -1; + } + return (int) sum; + } + + int starryPaths(vector adjacencyMatrix, int C) + { + const int N = adjacencyMatrix.size(); + + vector sn; + for(int i=0; i v; + for(int i=0; i 0 ) + v.push_back(i); + + for(int step=0 ;; ++step) + { + if( v.empty() ) + return step; + if( step >= 3*N ) + break; // infinite + + vector v2; + for(int u=0; u +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(_, StarsInGraphs().starryPaths(adjacencyMatrix, C));} +int main(){ + +CASE(0) + string adjacencyMatrix_[] = {"01110", + "10111", + "00000", + "00000", + "00000"}; + vector adjacencyMatrix(adjacencyMatrix_, adjacencyMatrix_+sizeof(adjacencyMatrix_)/sizeof(*adjacencyMatrix_)); + int C = 1000; + int _ = 2; +END +CASE(1) + string adjacencyMatrix_[] = {"01011", + "00111", + "10011", + "00000", + "00000"}; + vector adjacencyMatrix(adjacencyMatrix_, adjacencyMatrix_+sizeof(adjacencyMatrix_)/sizeof(*adjacencyMatrix_)); + int C = 1; + int _ = -1; +END +CASE(2) + string adjacencyMatrix_[] = {"0111", + "0000", + "0000", + "0000"}; + vector adjacencyMatrix(adjacencyMatrix_, adjacencyMatrix_+sizeof(adjacencyMatrix_)/sizeof(*adjacencyMatrix_)); + int C = 1; + int _ = 1; +END +CASE(3) + string adjacencyMatrix_[] = {"01111", + "00000", + "00000", + "00000", + "00000"}; + vector adjacencyMatrix(adjacencyMatrix_, adjacencyMatrix_+sizeof(adjacencyMatrix_)/sizeof(*adjacencyMatrix_)); + int C = 4; + int _ = 0; +END +CASE(4) + string adjacencyMatrix_[] = {"010001100", + "001001100", + "000101110", + "000010111", + "000001111", + "010000000", + "000110000", + "000100001", + "100001000"}; + vector adjacencyMatrix(adjacencyMatrix_, adjacencyMatrix_+sizeof(adjacencyMatrix_)/sizeof(*adjacencyMatrix_)); + int C = 10; + int _ = 5; +END +CASE(5) + string adjacencyMatrix_[] = {"00","00"}; + vector adjacencyMatrix(adjacencyMatrix_, adjacencyMatrix_+sizeof(adjacencyMatrix_)/sizeof(*adjacencyMatrix_)); + int C = 9999999; + int _ = 0; +END +CASE(6) + string adjacencyMatrix_[] = {"0111","1011","1101","1110"}; + vector adjacencyMatrix(adjacencyMatrix_, adjacencyMatrix_+sizeof(adjacencyMatrix_)/sizeof(*adjacencyMatrix_)); + int C = 9999; + int _ = -1; +END + +} +// END CUT HERE ADDED SRM/351-U/1A.cpp Index: SRM/351-U/1A.cpp ================================================================== --- SRM/351-U/1A.cpp +++ SRM/351-U/1A.cpp @@ -0,0 +1,109 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +using namespace std; +typedef long long LL; +typedef complex CMP; + +class CoinsExchange { public: + int countExchanges(int G1, int S1, int B1, int G2, int S2, int B2) + { + if( G1 < G2 ) { + int n = G2-G1; + return check2( S1, B1, S2+n*11, B2, n ); // 11*n silver needed + } + if( B1 < B2 ) { + int n = (B2-B1+8)/9; + return check2( G1, S1, G2, S2+n, n ); // n silver needed + } + + // need silver + int n = 0; + while( S1=G2 ) S1+=9, G1-= 1, n++; + while( S1=B2 ) S1+=1, B1-=11, n++; + return S1 +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(_, CoinsExchange().countExchanges(G1, S1, B1, G2, S2, B2));} +int main(){ + +CASE(0) + int G1 = 1; + int S1 = 0; + int B1 = 0; + int G2 = 0; + int S2 = 0; + int B2 = 81; + int _ = 10; +END +CASE(1) + int G1 = 1; + int S1 = 100; + int B1 = 12; + int G2 = 5; + int S2 = 53; + int B2 = 33; + int _ = 7; +END +CASE(2) + int G1 = 1; + int S1 = 100; + int B1 = 12; + int G2 = 5; + int S2 = 63; + int B2 = 33; + int _ = -1; +END +CASE(3) + int G1 = 5; + int S1 = 10; + int B1 = 12; + int G2 = 3; + int S2 = 7; + int B2 = 9; + int _ = 0; +END + +} +// END CUT HERE ADDED SRM/351-U/1B.cpp Index: SRM/351-U/1B.cpp ================================================================== --- SRM/351-U/1B.cpp +++ SRM/351-U/1B.cpp @@ -0,0 +1,104 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +using namespace std; +typedef long long LL; +typedef complex CMP; + +template +struct DP5 +{ + int N1, N2, N3, N4, N5; + vector data; + DP5(int N1, int N2, int N3, int N4, int N5, const T& t = T()) + : N1(N1), N2(N2), N3(N3), N4(N4), N5(N5), data(N1*N2*N3*N4*N5, t) {} + T& operator()(int i1, int i2, int i3, int i4, int i5) + { return data[ ((((i1*N2)+i2)*N3+i3)*N4+i4)*N5+i5 ]; } +}; + + +class BoxesArrangement { public: + int maxUntouched(string boxes) + { + int NA = count( boxes.begin(), boxes.end(), 'A' ); + int NB = count( boxes.begin(), boxes.end(), 'B' ); + int NC = count( boxes.begin(), boxes.end(), 'C' ); + + DP5 dp(NA+1, NB+1, NC+1, 3, 3); + for(int na=NA; na>=0; --na) + for(int nb=NB; nb>=0; --nb) + for(int nc=NC; nc>=0; --nc) + for(int c=0; c<3; ++c) + for(int l=0; l<3; ++l) + { + int n = na+nb+nc; + if( n == boxes.size() ) + continue; + int m = -1; + for(int x=0; x<3; ++x) { + int nna = na+(x==0); + int nnb = nb+(x==1); + int nnc = nc+(x==2); + int nl = (x==c ? l+1 : 1); + if( nna<=NA && nnb<=NB && nnc<=NC && nl<3 && dp(nna,nnb,nnc,x,nl)>=0 ) + m = max(m, (boxes[n]=='A'+x) + dp(nna,nnb,nnc,x,nl)); + } + dp(na,nb,nc,c,l) = m; + } + + return dp(0,0,0,0,0); + } +}; + +// 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 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(_, BoxesArrangement().maxUntouched(boxes));} +int main(){ + +CASE(0) + string boxes = "AAABBBCCC"; + int _ = 6; +END +CASE(1) + string boxes = "AAAAAAAACBB"; + int _ = 7; +END +CASE(2) + string boxes = "CCCCCB"; + int _ = -1; +END +CASE(3) + string boxes = "BAACAABAACAAA"; + int _ = 5; +END +CASE(4) + string boxes = "CBBABA"; + int _ = 6; +END +} +// END CUT HERE ADDED SRM/352-U/1A.cpp Index: SRM/352-U/1A.cpp ================================================================== --- SRM/352-U/1A.cpp +++ SRM/352-U/1A.cpp @@ -0,0 +1,77 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +using namespace std; +typedef long long LL; +typedef complex CMP; + +class NumberofFiboCalls { public: + vector fiboCallsMade(int n) + { + vector z(n+2), o(n+2); + z[0] = 1; + o[1] = 1; + for(int i=2; i<=n; ++i) { + z[i] = z[i-2] + z[i-1]; + o[i] = o[i-2] + o[i-1]; + } + vector ans(2); + ans[0] = z[n]; + ans[1] = o[n]; + return ans; + } +}; + +// 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 vector & Expected, const vector & 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(_, NumberofFiboCalls().fiboCallsMade(n));} +int main(){ + +CASE(0) + int n = 0; + int __[] = {1, 0 }; + vector _(__, __+sizeof(__)/sizeof(*__)); +END +CASE(1) + int n = 3; + int __[] = {1, 2 }; + vector _(__, __+sizeof(__)/sizeof(*__)); +END +CASE(2) + int n = 6; + int __[] = {5, 8 }; + vector _(__, __+sizeof(__)/sizeof(*__)); +END +CASE(3) + int n = 22; + int __[] = {10946, 17711 }; + vector _(__, __+sizeof(__)/sizeof(*__)); +END + +} +// END CUT HERE ADDED SRM/352-U/1B-U.cpp Index: SRM/352-U/1B-U.cpp ================================================================== --- SRM/352-U/1B-U.cpp +++ SRM/352-U/1B-U.cpp @@ -0,0 +1,122 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +using namespace std; +typedef long long LL; +typedef complex CMP; + +class FibonacciKnapsack { public: + long long maximalCost(vector items, string C_) + { + LL C; + stringstream(C_) >> C; + + vector< pair > wp; + for(int i=0; i> W >> P; + wp.push_back( make_pair(W,P) ); + } + sort( wp.begin(), wp.end() ); + + LL lW = 0, lP = 0; + for(int i=0; i >& wp, int i, LL w, LL cP ) + { + if( lW <= w ) + best = max(best, lP+cP); + if( lP+cP <= best ) + return; + + for(int k=i; k>=0 && wp[k].first==wp[i].first; --k) + { + lW -= wp[k].first; + lP -= wp[k].second; + if( w >= 0 ) + rec(lW, lP, wp, k-1, w, cP); + w -= wp[k].first; + cP += wp[k].second; + } + } +}; + +// 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 long long& Expected, const long long& 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(_, FibonacciKnapsack().maximalCost(items, C));} +int main(){ + +CASE(0) + string items_[] = {"5 555", "8 195", "13 651"}; + vector items(items_, items_+sizeof(items_)/sizeof(*items_)); + string C = "15"; + long long _ = 750LL; +END +CASE(1) + string items_[] = {"5 555", "8 195", "13 751"}; + vector items(items_, items_+sizeof(items_)/sizeof(*items_)); + string C = "15"; + long long _ = 751LL; +END +CASE(2) + string items_[] = {"55 1562", "5 814", "55 1962", "8 996", "2 716", "34 1792"}; + vector items(items_, items_+sizeof(items_)/sizeof(*items_)); + string C = "94"; + long long _ = 4568LL; +END +CASE(3) + string items_[] = {"13 89"}; + vector items(items_, items_+sizeof(items_)/sizeof(*items_)); + string C = "1"; + long long _ = 0LL; +END +CASE(4) + string items_[] = {"27777890035288 9419696870097445", + "53316291173 6312623457097563", + "165580141 8848283653257131"}; + vector items(items_, items_+sizeof(items_)/sizeof(*items_)); + string C = "27777900000000"; + long long _ = 15160907110354694LL; +END +CASE(5) + string items_[] = {"89 90", "55 54", "55 54", "34 35", "34 35", "34 35", "34 35", "21 23", "21 23", "21 23", "21 23", "21 23", "21 23", "21 23", "21 23", "13 14", "13 14", "13 14", "13 14", "13 14", "13 14", "13 14", "13 14", "13 14", "13 14", "13 14", "13 14", "13 14", "13 14", "13 14", "13 14", "8 10", "8 10", "8 10", "8 10", "8 10", "8 10", "8 10", "8 10", "8 10", "8 10", "8 10", "8 10", "8 10", "8 10", "8 10", "8 10", "8 10", "8 10", "8 10"}; + vector items(items_, items_+sizeof(items_)/sizeof(*items_)); + string C = "200"; + long long _ = 241LL; +END + + +} +// END CUT HERE ADDED SRM/387-U/2U.cpp Index: SRM/387-U/2U.cpp ================================================================== --- SRM/387-U/2U.cpp +++ SRM/387-U/2U.cpp @@ -0,0 +1,230 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +using namespace std; +typedef long long LL; +typedef complex CMP; + +static const int NV = 256; +typedef int flow; +typedef int cost; +typedef int vert; +typedef vert edge; +typedef vector edges; +typedef vector graph; +typedef flow flow_graph[NV][NV]; +typedef cost cost_graph[NV][NV]; +static const flow FLOW_INF = 0x7FFFFFFF; +static const cost COST_INF = 0x7FFFFFFF; + +// edges(bidi), capacity, cost(s.t. C[x][y]=-C[y][x]) per 1 flow, src, dst +pair mincostFlow(graph& G, flow_graph F, cost_graph C, vert S, vert D) +{ + int N = G.size(); + cost total_cost = 0; + flow total_flow = 0; + + vector h(N, 0); // potential + for(cost RF=FLOW_INF; RF>0; ) // residual flow + { + // Dijkstra -- find the min-cost path + vector d(N, COST_INF); d[S] = 0; + vector prev(N, -1); + + typedef pair > cedge; + priority_queue< cedge, vector, greater > Q; + Q.push( cedge(0, make_pair(S,S)) ); + while( !Q.empty() ) { + cedge e = Q.top(); Q.pop(); + if( prev[e.second.second] >= 0 ) + continue; + prev[e.second.second] = e.second.first; + + vert u = e.second.second; + for(int i=0; i 0 && d[v] > d[u]+r_cost ) + Q.push( cedge(d[v]=d[u]+r_cost, make_pair(u,v)) ); + } + } + + if( prev[D] < 0 ) + break; // Finished + + // As much as possible + flow f = RF; + for(vert u=D; u!=S; u=prev[u]) + f = min(f, F[prev[u]][u]); + RF -= f; + total_flow += f; + + for(vert u=D; u!=S; u=prev[u]) + { + total_cost += f * C[prev[u]][u]; + F[prev[u]][u] -= f; + F[u][prev[u]] += f; + } + + // Update the potential + for(vert u=0; u boxes) + { + const int NB = boxes.size(); + vector< vector > bb(NB); + for(int i=0; i>v; ) + bb[i].push_back(v); + } + const int NC = bb[0].size(); + + graph G(NB+NC+3); + flow_graph F = {}; + cost_graph C = {}; + for(int i=0; i ff = mincostFlow(G, F, C, 0, 1); + return ff.first; + } +}; + +// 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 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(_, MarblesRegroupingHard().minMoves(boxes));} +int main(){ + +CASE(0) + string boxes_[] = {"0"}; + vector boxes(boxes_, boxes_+sizeof(boxes_)/sizeof(*boxes_)); + int _ = 0; +END +CASE(1) + string boxes_[] = {"77 97","8 0"}; + vector boxes(boxes_, boxes_+sizeof(boxes_)/sizeof(*boxes_)); + int _ = 77; +END +CASE(2) + string boxes_[] = {"6 97 7","73 45 0","67 45 63"}; + vector boxes(boxes_, boxes_+sizeof(boxes_)/sizeof(*boxes_)); + int _ = 170; +END +CASE(3) + string boxes_[] = {"97 94 0 99","1 72 46 45","0 10 47 75","0 92 76 20","2 25 98 22"}; + vector boxes(boxes_, boxes_+sizeof(boxes_)/sizeof(*boxes_)); + int _ = 559; +END +CASE(4) + string boxes_[] = {"38 0 0 45 77 61 0 0 8 87 65 53 0 2", + "0 97 79 37 70 0 69 35 95 11 75 96 77 29", + "39 32 0 24 63 22 91 71 0 63 92 59 12 0", + "34 0 0 71 88 13 60 56 0 22 29 89 81 53", + "69 95 65 0 94 98 84 37 0 87 16 0 58 18", + "82 0 36 88 0 54 82 40 6 0 34 44 80 2", + "33 58 7 95 83 87 0 0 79 35 0 51 22 84", + "7 0 30 57 33 4 0 26 44 55 67 31 88 42", + "62 58 62 93 91 0 1 0 44 23 95 0 55 57", + "35 8 22 26 76 0 78 54 0 46 42 0 0 64", + "93 54 58 0 89 89 0 0 57 0 98 3 24 0", + "9 0 0 23 82 18 0 46 0 0 94 84 19 18", + "78 12 6 45 0 80 16 69 59 76 35 0 66 0", + "0 68 77 13 15 0 52 72 0 0 18 65 23 0", + "0 0 73 53 0 95 91 44 27 96 85 0 99 85", + "93 29 4 89 27 44 27 17 21 6 0 8 3 91", + "0 46 73 94 60 59 15 50 18 75 74 88 46 93", + "0 0 0 94 93 26 21 83 54 62 0 89 59 77", + "32 98 0 44 34 38 56 20 0 61 29 94 52 57", + "52 60 0 22 0 5 38 0 50 98 12 75 0 81", + "23 0 41 18 36 87 49 32 62 43 22 74 0 97", + "0 0 30 79 16 73 61 0 75 51 64 13 45 0", + "11 0 14 2 0 1 2 16 84 37 95 45 48 33", + "10 0 0 35 0 85 28 76 99 74 76 32 52 8", + "60 0 96 0 95 26 59 13 0 0 44 42 97 48", + "34 7 77 25 91 85 35 78 32 99 7 29 18 15", + "61 50 43 22 42 63 64 50 9 94 42 22 21 33", + "58 0 41 10 16 0 27 67 83 27 14 37 98 47", + "37 60 60 76 71 2 84 32 27 39 82 84 0 94", + "15 98 69 82 36 66 0 97 62 39 0 65 62 67", + "0 41 0 43 0 0 94 0 0 58 0 0 27 33", + "53 90 71 91 85 0 32 86 40 60 11 0 99 28", + "79 62 0 0 79 0 14 62 87 76 35 0 70 0", + "0 40 73 48 0 63 0 0 63 5 30 18 47 51", + "75 6 58 69 33 57 66 0 12 0 46 0 65 10"}; + vector boxes(boxes_, boxes_+sizeof(boxes_)/sizeof(*boxes_)); + int _ = 18618; +END +/* +CASE(5) + string boxes_[] = ; + vector boxes(boxes_, boxes_+sizeof(boxes_)/sizeof(*boxes_)); + int _ = ; +END +CASE(6) + string boxes_[] = ; + vector boxes(boxes_, boxes_+sizeof(boxes_)/sizeof(*boxes_)); + int _ = ; +END +*/ +} +// END CUT HERE ADDED SRM/394-U/2C.cpp Index: SRM/394-U/2C.cpp ================================================================== --- SRM/394-U/2C.cpp +++ SRM/394-U/2C.cpp @@ -0,0 +1,90 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +using namespace std; +typedef long long LL; +typedef complex CMP; + +class ProperDivisors { public: + int analyzeInterval(int a, int b, int n) + { + int c = 0; + for(int k=2; k+k<=a+b; ++k) + c += cd(a+b, k, n) - cd(a-1, k, n); + return c; + } + + int cd(int x, int k, int n) + { + LL kn = 1; + for(int i=0; i x ) + return x/k - (x>=k ? 1 : 0); + return x/k - x/kn - (x>=k ? 1 : 0); + } +}; + +// 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 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(_, ProperDivisors().analyzeInterval(a, b, n));} +int main(){ + +CASE(0) + int a = 32; + int b = 1; + int n = 3; + int _ = 5; +END +CASE(1) + int a = 1; + int b = 12; + int n = 2; + int _ = 8; +END +CASE(2) + int a = 1000000; + int b = 10000000; + int n = 10; + int _ = 146066338; +END +/* +CASE(3) + int a = ; + int b = ; + int n = ; + int _ = ; +END +CASE(4) + int a = ; + int b = ; + int n = ; + int _ = ; +END +*/ +} +// END CUT HERE ADDED SRM/396-U/2C.cpp Index: SRM/396-U/2C.cpp ================================================================== --- SRM/396-U/2C.cpp +++ SRM/396-U/2C.cpp @@ -0,0 +1,120 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +using namespace std; +typedef long long LL; +typedef complex CMP; + +class RemovingDigits { public: + string maxNumber(string number, string digits) + { + vector d(9); + for(int i=0; i n(9); + for(int i=0; i >, string> memo; + + string maximize(const string& number, int s, vector& n, vector& d) + { + if( s == number.size() ) + return ""; + + pair< int, vector > key(s, d); + if( memo.count(key) ) + return memo[key]; + + char ch = number[s]-'1'; + string cand = ""; + + n[ch]--; + if( n[ch] >= d[ch] ) + { + string c1 = number[s] + maximize(number, s+1, n, d); + cand = max(cand, c1); + } + if( d[ch] > 0 ) + { + d[ch]--; + string c2 = maximize(number, s+1, n, d); + cand = max(cand, c2); + d[ch]++; + } + n[ch]++; + return memo[key] = cand; + } +}; + +// 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(_, RemovingDigits().maxNumber(number, digits));} +int main(){ + +CASE(0) + string number = "12345"; + string digits = "513"; + string _ = "24"; +END +CASE(1) + string number = "112352"; + string digits = "1123"; + string _ = "52"; +END +CASE(2) + string number = "123456654321"; + string digits = "612534"; + string _ = "654321"; +END +CASE(3) + string number = "654321123456"; + string digits = "612534"; + string _ = "654321"; +END +CASE(4) + string number = "2654982765982365"; + string digits = "2345978"; + string _ = "698265265"; +END +/* +CASE(5) + string number = ; + string digits = ; + string _ = ; +END +CASE(6) + string number = ; + string digits = ; + string _ = ; +END +*/ +} +// END CUT HERE ADDED SRM/397/1A.cpp Index: SRM/397/1A.cpp ================================================================== --- SRM/397/1A.cpp +++ SRM/397/1A.cpp @@ -0,0 +1,68 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +using namespace std; +typedef long long LL; + +class SortingGame +{ +public: + int fewestMoves(vector board, int k) + { + vector sorted(board); + sort(sorted.begin(), sorted.end()); + + vector< vector > Q; + set< vector > V; + Q.push_back(board), V.insert(board); + for(int N=0; !Q.empty(); ++N) + { + vector< vector > Q2; + for(int i=0; i& b = Q[i]; + if( b == sorted ) + return N; + for(int i=0; i+k<=b.size(); ++i) + { + reverse(b.begin()+i, b.begin()+i+k); + if( !V.count(b) ) + Q2.push_back(b), V.insert(b); + reverse(b.begin()+i, b.begin()+i+k); + } + } + Q.swap(Q2); + } + return -1; + } + +// BEGIN CUT HERE + public: + void run_test(int Case) { if ((Case == -1) || (Case == 0)) test_case_0(); if ((Case == -1) || (Case == 1)) test_case_1(); if ((Case == -1) || (Case == 2)) test_case_2(); if ((Case == -1) || (Case == 3)) test_case_3(); if ((Case == -1) || (Case == 4)) test_case_4(); } + private: + template string print_array(const vector &V) { ostringstream os; os << "{ "; for (typename vector::const_iterator iter = V.begin(); iter != V.end(); ++iter) os << '\"' << *iter << "\","; os << " }"; return os.str(); } + void verify_case(int Case, const int &Expected, const int &Received) { cerr << "Test Case #" << Case << "..."; if (Expected == Received) cerr << "PASSED" << endl; else { cerr << "FAILED" << endl; cerr << "\tExpected: \"" << Expected << '\"' << endl; cerr << "\tReceived: \"" << Received << '\"' << endl; } } + void test_case_0() { int Arr0[] = {1,2,3}; vector Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); int Arg1 = 3; int Arg2 = 0; verify_case(0, Arg2, fewestMoves(Arg0, Arg1)); } + void test_case_1() { int Arr0[] = {3,2,1}; vector Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); int Arg1 = 3; int Arg2 = 1; verify_case(1, Arg2, fewestMoves(Arg0, Arg1)); } + void test_case_2() { int Arr0[] = {5,4,3,2,1}; vector Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); int Arg1 = 2; int Arg2 = 10; verify_case(2, Arg2, fewestMoves(Arg0, Arg1)); } + void test_case_3() { int Arr0[] = {3,2,4,1,5}; vector Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); int Arg1 = 4; int Arg2 = -1; verify_case(3, Arg2, fewestMoves(Arg0, Arg1)); } + void test_case_4() { int Arr0[] = {7,2,1,6,8,4,3,5}; vector Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); int Arg1 = 4; int Arg2 = 7; verify_case(4, Arg2, fewestMoves(Arg0, Arg1)); } + +// END CUT HERE +}; +// BEGIN CUT HERE +int main() { + SortingGame().run_test(-1); +} +// END CUT HERE ADDED SRM/397/1B.cpp Index: SRM/397/1B.cpp ================================================================== --- SRM/397/1B.cpp +++ SRM/397/1B.cpp @@ -0,0 +1,90 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +using namespace std; +typedef long long LL; + +class SumOfPowers +{ +public: + int value(int n, int k) + { + // 2^m - 1^m = (1+1)^m - 1^m = mC1 1^(m-1) + ... + mCi 1^(m-i) + ... + mCm 1^0 + // 3^m - 2^m = (2+1)^m - 1^m = mC1 2^(m-1) + ... + mCi 2^(m-i) + ... + mCm 2^0 + //+ ... + //-------------------------------------------------------------------------------- + // (n+1)^m - 1 = mC1 \Sigma n^(m-1) + ... + mCi \Sigma n^(m-i) + ... + mCm \Sigma n^0 + + vector vals; + for(int m=1; m<=k+1; ++m) + { + LL x = SUB(POW(n+1, m), 1); + for(int i=2; i<=m; ++i) + x = SUB( x, MUL(C(m, i), vals[m-i]) ); + x = DIV(x, m); + vals.push_back(x); + } + return (int)vals.back(); + } + LL ADD(LL x, LL y) { return (x+y)%1000000007; } + LL SUB(LL x, LL y) { return (x-y+1000000007)%1000000007; } + LL MUL(LL x, LL y) { return (x*y)%1000000007; } + LL C(LL n, LL k) { + LL v = 1; + for(LL i=1; i<=k; ++i) + v = DIV(MUL(v, n-i+1), i); + return v; + } + LL POW(LL x, LL e) { + LL v = 1; + for(;e;x=MUL(x,x),e>>=1) + if(e&1) + v = MUL(v, x); + return v; + } + LL DIV(LL x, LL y) { + LL iy, _; + xgcd(y, 1000000007, &iy, &_); + return MUL(x, (iy+1000000007)%1000000007); + } + LL xgcd(LL a, LL b, LL* x, LL* y) { // ax+by=g + if(b) { + LL yy, g = xgcd(b,a%b,&yy,x); + *y = yy - a/b**x; + return g; + } + else { + *x=1, *y=0; + return a; + } + } + +// BEGIN CUT HERE + public: + void run_test(int Case) { if ((Case == -1) || (Case == 0)) test_case_0(); if ((Case == -1) || (Case == 1)) test_case_1(); if ((Case == -1) || (Case == 2)) test_case_2(); if ((Case == -1) || (Case == 3)) test_case_3(); } + private: + template string print_array(const vector &V) { ostringstream os; os << "{ "; for (typename vector::const_iterator iter = V.begin(); iter != V.end(); ++iter) os << '\"' << *iter << "\","; os << " }"; return os.str(); } + void verify_case(int Case, const int &Expected, const int &Received) { cerr << "Test Case #" << Case << "..."; if (Expected == Received) cerr << "PASSED" << endl; else { cerr << "FAILED" << endl; cerr << "\tExpected: \"" << Expected << '\"' << endl; cerr << "\tReceived: \"" << Received << '\"' << endl; } } + void test_case_0() { int Arg0 = 5; int Arg1 = 1; int Arg2 = 15; verify_case(0, Arg2, value(Arg0, Arg1)); } + void test_case_1() { int Arg0 = 4; int Arg1 = 2; int Arg2 = 30; verify_case(1, Arg2, value(Arg0, Arg1)); } + void test_case_2() { int Arg0 = 13; int Arg1 = 5; int Arg2 = 1002001; verify_case(2, Arg2, value(Arg0, Arg1)); } + void test_case_3() { int Arg0 = 123456789; int Arg1 = 1; int Arg2 = 383478132; verify_case(3, Arg2, value(Arg0, Arg1)); } + +// END CUT HERE +}; +// BEGIN CUT HERE +int main() { + SumOfPowers().run_test(-1); +} +// END CUT HERE ADDED SRM/397/1C.cpp Index: SRM/397/1C.cpp ================================================================== --- SRM/397/1C.cpp +++ SRM/397/1C.cpp @@ -0,0 +1,96 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +using namespace std; +typedef long long LL; + +typedef int vert; +typedef vert edge; +typedef vector edges; +typedef vector graph; + +bool augment( graph& G, int v, vector& matchTo, bool visited[] ) +{ + for(int i=0; i matchTo(G.size(), -1); + int ans = 0; + for(vert v=0; v bx, vector by, vector rx, vector ry, int R) + { + double r[50][50]; + set rs; + for(int i=0; i(r[i][j], R) ); + } + rs.insert(R); + + double result = 0; + for(set::iterator it=rs.begin(); it!=rs.end(); ++it) + { + double curR = *it; + graph G(bx.size()+rx.size()); + for(int i=0; i string print_array(const vector &V) { ostringstream os; os << "{ "; for (typename vector::const_iterator iter = V.begin(); iter != V.end(); ++iter) os << '\"' << *iter << "\","; os << " }"; return os.str(); } + void verify_case(int Case, const double &Expected, const double &Received) { cerr << "Test Case #" << Case << "..."; if (Expected == Received) cerr << "PASSED" << endl; else { cerr << "FAILED" << endl; cerr << "\tExpected: \"" << Expected << '\"' << endl; cerr << "\tReceived: \"" << Received << '\"' << endl; } } + void test_case_0() { int Arr0[] = { 0, 4 }; vector Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); int Arr1[] = { 0, 0 }; vector Arg1(Arr1, Arr1 + (sizeof(Arr1) / sizeof(Arr1[0]))); int Arr2[] = { 2, 1 }; vector Arg2(Arr2, Arr2 + (sizeof(Arr2) / sizeof(Arr2[0]))); int Arr3[] = { 2, -1 }; vector Arg3(Arr3, Arr3 + (sizeof(Arr3) / sizeof(Arr3[0]))); int Arg4 = 1; double Arg5 = 9.42477796076938; verify_case(0, Arg5, safetyFactor(Arg0, Arg1, Arg2, Arg3, Arg4)); } + void test_case_1() { int Arr0[] = { 1 }; vector Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); int Arr1[] = { 1 }; vector Arg1(Arr1, Arr1 + (sizeof(Arr1) / sizeof(Arr1[0]))); int Arr2[] = { 1 }; vector Arg2(Arr2, Arr2 + (sizeof(Arr2) / sizeof(Arr2[0]))); int Arr3[] = { 1 }; vector Arg3(Arr3, Arr3 + (sizeof(Arr3) / sizeof(Arr3[0]))); int Arg4 = 5; double Arg5 = 78.53981633974483; verify_case(1, Arg5, safetyFactor(Arg0, Arg1, Arg2, Arg3, Arg4)); } + void test_case_2() { int Arr0[] = { 0 }; vector Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); int Arr1[] = { 0 }; vector Arg1(Arr1, Arr1 + (sizeof(Arr1) / sizeof(Arr1[0]))); int Arr2[] = { 100 }; vector Arg2(Arr2, Arr2 + (sizeof(Arr2) / sizeof(Arr2[0]))); int Arr3[] = { 0 }; vector Arg3(Arr3, Arr3 + (sizeof(Arr3) / sizeof(Arr3[0]))); int Arg4 = 51; double Arg5 = 15707.963267948966; verify_case(2, Arg5, safetyFactor(Arg0, Arg1, Arg2, Arg3, Arg4)); } + void test_case_3() { int Arr0[] = { 23, 29, 29, 35 }; vector Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); int Arr1[] = { 77, 79, 75, 77 }; vector Arg1(Arr1, Arr1 + (sizeof(Arr1) / sizeof(Arr1[0]))); int Arr2[] = { 26, 26, 32 }; vector Arg2(Arr2, Arr2 + (sizeof(Arr2) / sizeof(Arr2[0]))); int Arr3[] = { 78, 76, 76 }; vector Arg3(Arr3, Arr3 + (sizeof(Arr3) / sizeof(Arr3[0]))); int Arg4 = 3; double Arg5 = 113.09733552923255; verify_case(3, Arg5, safetyFactor(Arg0, Arg1, Arg2, Arg3, Arg4)); } + +// END CUT HERE +}; +// BEGIN CUT HERE +int main() { + HouseProtection().run_test(-1); +} +// END CUT HERE ADDED SRM/398/1A.cpp Index: SRM/398/1A.cpp ================================================================== --- SRM/398/1A.cpp +++ SRM/398/1A.cpp @@ -0,0 +1,28 @@ +struct CountExpressions +{ + static int add(int x, int y) { return x+y; } + static int sub(int x, int y) { return x-y; } + static int mul(int x, int y) { return x*y; } + + int calcExpressions(int x, int y, int val) + { + int num[][4] = { + {x,x,y,y}, + {x,y,x,y}, + {x,y,y,x}, + {y,x,x,y}, + {y,x,y,x}, + {y,y,x,x}, + }; + int (*op[])(int,int) = { &add, &sub, &mul }; + + int cnt = 0; + for(int a=0; a<6; ++a) + for(int i=0; i<3; ++i) + for(int j=0; j<3; ++j) + for(int k=0; k<3; ++k) + if( val == op[k]( op[j]( op[i](num[a][0], num[a][1]), num[a][2] ), num[a][3] ) ) + ++cnt; + return cnt; + } +}; ADDED SRM/398/1B.cpp Index: SRM/398/1B.cpp ================================================================== --- SRM/398/1B.cpp +++ SRM/398/1B.cpp @@ -0,0 +1,62 @@ +#include +#include +#include +using namespace std; + + +long long dp[51][51][51][51]; // y,x,n,i + +struct CountPaths +{ + vector difPaths(int r, int c, vector fieldrow, vector fieldcol) + { + int k = fieldrow.size(); + map< pair, int > f; + for(int i=0; i=2 ) + sum += dp[y-1][x][n-1][i]; + if( x>=2 ) + sum += dp[y][x-1][n-1][i]; + } + dp[y][x][n][I+1] = sum % 1000007; + } + } else { + for(int n=0; n<=k; ++n) { + for(int i=0; i<=k; ++i) { + int sum = 0; + if( y>=2 ) + sum += dp[y-1][x][n][i]; + if( x>=2 ) + sum += dp[y][x-1][n][i]; + dp[y][x][n][i] = sum % 1000007; + } + } + } + } + + vector ans; + for(int n=0; n<=k; ++n) { + int sum = 0; + for(int i=0; i<=k; ++i) + sum += dp[r][c][n][i]; + ans.push_back( sum % 1000007 ); + } + return ans; + } +}; ADDED SRM/398/1C.cpp Index: SRM/398/1C.cpp ================================================================== --- SRM/398/1C.cpp +++ SRM/398/1C.cpp @@ -0,0 +1,103 @@ +#include +#include +#include +#include +using namespace std; + +vector solve_linear_eq( int n, vector< vector > M, const vector& V ) +{ + vector A(V); + for(int i=0; i G ) +{ + sort( G.begin(), G.end() ); + + vector::iterator b = lower_bound( G.begin(), G.end(), 1 ); + vector::iterator e = G.end(); + + while( b < e ) + { + int n = *(--e); + if( e-b < n ) + return false; + for(vector::iterator i=e-n; i!=e; ++i) + --*i; + inplace_merge( b, e-n, e ); + b = lower_bound( G.begin(), G.end(), 1 ); + } + return true; +} + +struct MyFriends +{ + string calcFriends(vector sumFriends, int k) + { + int n = sumFriends.size(); + + vector< vector > M(n, vector(n)); + for(int i=0; i V( sumFriends.begin(), sumFriends.end() ); + vector Ad = solve_linear_eq( n, M, V ); + vector A; + for(int i=0; i +#include +#include +#include +#include +using namespace std; + + +struct AvoidingProduct +{ + vector getTriple(vector a, int n) + { + int best = 1000000000; + vector ans(3); + + set as(a.begin(), a.end()); + for(int x=1;; ++x) if( !as.count(x) ) { + for(int y=x;; ++y) if( !as.count(y) ) { + for(int z=y;; ++z) if( !as.count(z) ) { + int m = x*y*z; + if( abs(m-n) < best ) { + best = abs(m-n); + ans[0] = x; + ans[1] = y; + ans[2] = z; + } + + if( x*y*z>n ) break; + } + if( x*y*y>n ) break; + } + if( x*x*x>n ) break; + } + + return ans; + } +}; ADDED SRM/399/1B.cpp Index: SRM/399/1B.cpp ================================================================== --- SRM/399/1B.cpp +++ SRM/399/1B.cpp @@ -0,0 +1,66 @@ +#include +#include +#include +using namespace std; + +string err = "222222222222222222222222222222222222222222222222"; + +void operator&=( string& x, const string& r ) +{ + if( x.size() > r.size() ) + x = r; + else if( x.size()==r.size() && x > r ) + x = r; +} + +struct BinarySum +{ + map memo; + + int rearrange(int a, int b, int c) + { + int an=0,bn=0,cn=0,n=0; + for(unsigned int i=1,t=1; i<=a; i<<=1,++t) {if(a&i) an++; n=max(n,t);} + for(unsigned int i=1,t=1; i<=b; i<<=1,++t) {if(b&i) bn++; n=max(n,t);} + for(unsigned int i=1,t=1; i<=c; i<<=1,++t) {if(c&i) cn++; n=max(n,t);} + string ans = err; + ans &= mini( 0, cn-1, an-1, bn ); // 1+0(+0)=01 + ans &= mini( 0, cn-1, an, bn-1 ); // 0+1(+0)=01 + ans &= mini( 1, cn-1, an, bn ); // 0+0(+1)=01 + if( ans.find('2') != string::npos ) + return -1; + ans = "1"+ans; + if( ans.size() > n ) + return -1; + int v = 0; + for(int i=0; i +#include +#include +#include +using namespace std; + + + + +static const int NV = 512; +typedef int flow; +typedef int vert; +typedef vert edge; +typedef vector edges; +typedef vector graph; +typedef flow flow_graph[NV][NV]; + +flow dinic_dfs( graph& G, flow_graph F, vert v, vert D, + int LV[], flow flow_in, int blocked[] ) +{ + flow flow_out = 0; + for(int i=0; i!=G[v].size(); ++i) + { + int u = G[v][i]; + if( LV[v]+1==LV[u] && F[v][u] ) + { + flow f = min(flow_in-flow_out, F[v][u]); + if( u==D || !blocked[u] && (f=dinic_dfs(G,F,u,D,LV,f,blocked)) ) + { + F[v][u] -= f; + F[u][v] += f; + flow_out += f; + if( flow_in == flow_out ) return flow_out; + } + } + } + blocked[v] = (flow_out==0); + return flow_out; +} + +flow dinic( graph& G, flow_graph F, vert S, vert D ) +{ + for( flow total=0 ;; ) { + int LV[NV] = {0}; + vector Q(1, S); + for(int lv=1; !Q.empty(); ++lv) + { + vector Q2; + for(int i=0; i!=Q.size(); ++i) + { + edges& ne = G[Q[i]]; + for(int j=0; j!=ne.size(); ++j) + if( F[Q[i]][ne[j]] && !LV[ne[j]] && ne[j]!=S ) + LV[ne[j]]=lv, Q2.push_back(ne[j]); + } + Q.swap(Q2); + } + + if( !LV[D] ) + return total; + + int blocked[NV] = {}; + total += dinic_dfs( G, F, S, D, LV, 0x7fffffff, blocked ); + } +} + + + +struct DancingParty +{ + int maxDances(vector likes, int k) + { + int n = likes.size(); + #define SRC 0 + #define DST 1 + #define L(i) 2+n*0+i + #define R(i) 2+n*1+i + #define YL(i) 2+n*2+i + #define NL(i) 2+n*3+i + #define YR(i) 2+n*4+i + #define NR(i) 2+n*5+i + + graph G(2+n*6); + + // Graph + for(int i=0; i +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +using namespace std; +typedef long long LL; + +class StrongPrimePower +{ +public: + vector baseAndExponent(string n) + { + LL N; + stringstream(n) >> N; + + LL sN = (LL) sqrt( (double)N ); + if( sN*sN == N && isPrime(sN) ) { + vector ans; + ans.push_back(sN); + ans.push_back(2); + return ans; + } + if( (sN+1)*(sN+1) == N && isPrime(sN+1) ) { + vector ans; + ans.push_back(sN+1); + ans.push_back(2); + return ans; + } + + for(LL p=2; p*p*p<=N; ++p) + if( N%p==0 ) { + LL q=0; + while(N%p==0) + N /= p, ++q; + if( N==1 ) { + vector ans; + ans.push_back(p); + ans.push_back(q); + return ans; + } + break; + } + return vector(); + } + + bool isPrime(LL N) { + for(LL p=2; p*p<=N; ++p) + if(N%p==0) + return false; + return true; + } +}; + +// BEGIN CUT HERE +template string print_array(const vector &V) { ostringstream os; os << "{ "; for (typename vector::const_iterator iter = V.begin(); iter != V.end(); ++iter) os << '\"' << *iter << "\","; os << " }"; return os.str(); } +int verify_case(const vector &Expected, const vector &Received) { if (Expected == Received) cerr << "PASSED" << endl; else { cerr << "FAILED" << endl; cerr << "\tExpected: " << print_array(Expected) << endl; cerr << "\tReceived: " << print_array(Received) << endl; } return 0;} + +template struct Case_ {}; +char Test_(...); +int Test_(Case_<0>) { + string n = "27"; + int RetVal_[] = {3, 3 }; + vector RetVal(RetVal_, RetVal_+sizeof(RetVal_)/sizeof(*RetVal_)); + return verify_case(RetVal, StrongPrimePower().baseAndExponent(n)); } +int Test_(Case_<1>) { + string n = "10"; + vector RetVal; + return verify_case(RetVal, StrongPrimePower().baseAndExponent(n)); } +int Test_(Case_<2>) { + string n = "7"; + vector RetVal; + return verify_case(RetVal, StrongPrimePower().baseAndExponent(n)); } +int Test_(Case_<3>) { + string n = "1296"; + vector RetVal; + return verify_case(RetVal, StrongPrimePower().baseAndExponent(n)); } +int Test_(Case_<4>) { + string n = "576460752303423488"; + int RetVal_[] = {2, 59 }; + vector RetVal(RetVal_, RetVal_+sizeof(RetVal_)/sizeof(*RetVal_)); + return verify_case(RetVal, StrongPrimePower().baseAndExponent(n)); } +int Test_(Case_<5>) { + string n = "999999874000003969"; + int RetVal_[] = {999999937, 2 }; + vector RetVal(RetVal_, RetVal_+sizeof(RetVal_)/sizeof(*RetVal_)); + return verify_case(RetVal, StrongPrimePower().baseAndExponent(n)); } + +template void Run_() { cerr << "Test Case #" << N << "..." << flush; Test_(Case_()); Run_()))==1 ? -1 : N+1>(); } +template<> void Run_<-1>() {} +int main() { Run_<0>(); } +// END CUT HERE + ADDED SRM/400/1B.cpp Index: SRM/400/1B.cpp ================================================================== --- SRM/400/1B.cpp +++ SRM/400/1B.cpp @@ -0,0 +1,109 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +using namespace std; +typedef long long LL; +#include + +static const int INF = 3000; +class ReversalChain +{ +public: + map,int>,int> memo; + int minReversal(string init, string goal) + { + LL s=0, sb=0; for(int i=0; i=INF ? -1 : a; + } + int mrev(LL s, LL g, int m) + { + #define mask(x) ((x) & (1LL<<(m))-1) + #define ith(x,i) (((x)>>(i))&1) + + if( mask(s) == mask(g) ) + return 0; + + pair,int> key(make_pair(mask(s),mask(g)),m); + if( memo.count(key) ) + return memo[key]; + + int a = INF; + if( ith(s,m-1)==ith(g,m-1) ) a = min(a, mrev(s, g, m-1)); + if( ith(s,0)==ith(g,0) ) a = min(a, mrev(s>>1, g>>1, m-1)); + + s = bitrev(s,m); + if( ith(s,m-1)==ith(g,m-1) ) a = min(a, 1+mrev(s, g, m-1)); + if( ith(s,0)==ith(g,0) ) a = min(a, 1+mrev(s>>1, g>>1, m-1)); + + return memo[key] = a; + } + LL bitrev(LL x, LL m) + { + LL y = 0; + for(int i=0; i>i)&1) << (m-1-i)); + return y; + } +}; + +// 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 string print_array(const vector &V) { ostringstream os; os << "{ "; for (typename vector::const_iterator iter = V.begin(); iter != V.end(); ++iter) os << '\"' << *iter << "\","; os << " }"; return os.str(); } +int verify_case(const int &Expected, const int &Received) { if (Expected == Received) cerr << "PASSED" << timer() << endl; else { cerr << "FAILED" << timer() << endl; cerr << "\tExpected: \"" << Expected << '\"' << endl; cerr << "\tReceived: \"" << Received << '\"' << endl; } return 0;} + +template struct Case_ { Case_(){start_time=clock();} }; +char Test_(...); +int Test_(Case_<0>) { + string init = "1100"; + string goal = "0110"; + int RetVal = 1; + return verify_case(RetVal, ReversalChain().minReversal(init, goal)); } +int Test_(Case_<1>) { + string init = "111000"; + string goal = "101010"; + int RetVal = 2; + return verify_case(RetVal, ReversalChain().minReversal(init, goal)); } +int Test_(Case_<2>) { + string init = "0"; + string goal = "1"; + int RetVal = -1; + return verify_case(RetVal, ReversalChain().minReversal(init, goal)); } +int Test_(Case_<3>) { + string init = "10101"; + string goal = "10101"; + int RetVal = 0; + return verify_case(RetVal, ReversalChain().minReversal(init, goal)); } +int Test_(Case_<4>) { + string init = "111000111000"; + string goal = "001100110011"; + int RetVal = 4; + return verify_case(RetVal, ReversalChain().minReversal(init, goal)); } +int Test_(Case_<5>) { + string init = "001000011000000111000000001111"; + string goal = "100100100100100100100100100100"; + int RetVal = 9; + return verify_case(RetVal, ReversalChain().minReversal(init, goal)); } + +template void Run_() { cerr << "Test Case #" << N << "..." << flush; Test_(Case_()); Run_()))==1 ? -1 : N+1>(); } +template<> void Run_<-1>() {} +int main() { Run_<0>(); } +// END CUT HERE + ADDED SRM/400/1C.cpp Index: SRM/400/1C.cpp ================================================================== --- SRM/400/1C.cpp +++ SRM/400/1C.cpp @@ -0,0 +1,108 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +using namespace std; +typedef long long LL; +typedef complex CMP; + +class CollectingBonuses { public: + double expectedBuy(string n, string k) + { + LL N; {stringstream ss; ss<>N;} + LL K; {stringstream ss; ss<>K;} + return N * sigma(K,N); + } + double f(LL b) + { + return 0.5772156649015313 + log(b+0.5); + } + double sigma(LL k, LL n) + { + // compute (1/n-k+1 + ... + 1/n) + + if(k<=10000000) { + double p=0.0; + for(LL i=0; 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 double& Expected, const double& Received) { + bool ok = (abs(Expected - Received) < 1e-9); + 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(_, CollectingBonuses().expectedBuy(n, k));} +int main(){ + +CASE(0) + string n = "1"; + string k = "1"; + double _ = 1.0; +END +CASE(1) + string n = "2"; + string k = "1"; + double _ = 1.0; +END +CASE(2) + string n = "2"; + string k = "2"; + double _ = 3.0; +END +CASE(3) + string n = "4"; + string k = "3"; + double _ = 4.333333333333333; +END +CASE(4) + string n = "999999999999999999"; + string k = "999999999999999999"; + double _ = 4.202374733879435E19; +END +/* +CASE(5) + string n = ; + string k = ; + double _ = ; +END +*/ +} +// END CUT HERE ADDED SRM/424/1A-2B.cpp Index: SRM/424/1A-2B.cpp ================================================================== --- SRM/424/1A-2B.cpp +++ SRM/424/1A-2B.cpp @@ -0,0 +1,13 @@ +struct ProductOfDigits +{ + int smallestNumber( int N ) + { + if( N < 10 ) + return 1; + int ans = 0; + for(int d=9; d>=2; --d) + while( N%d == 0 ) + N/=d, ans++; + return N==1 ? ans : -1; + } +}; ADDED SRM/424/1B.cpp Index: SRM/424/1B.cpp ================================================================== --- SRM/424/1B.cpp +++ SRM/424/1B.cpp @@ -0,0 +1,64 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +using namespace std; +typedef long long LL; + +int memo[1001][1001]; + +class StrengthOrIntellect +{ +public: + int numberOfMissions(vector strength, vector intellect, vector points) + { + memset(memo, 0xff, sizeof(memo)); + return rec(1, 1, strength, intellect, points); + } + + int rec(int s, int i, vector& strength, vector& intellect, vector& points) + { + if( memo[s][i] >= 0 ) + return memo[s][i]; + + int pt = 2-s-i, cnt=0; + for(int j=0; js || ii>i ) + cnt = max(cnt, rec(ss, ii, strength, intellect, points)); + } + return memo[s][i] = cnt; + } + +// BEGIN CUT HERE + public: + void run_test(int Case) { if ((Case == -1) || (Case == 0)) test_case_0(); if ((Case == -1) || (Case == 1)) test_case_1(); if ((Case == -1) || (Case == 2)) test_case_2(); if ((Case == -1) || (Case == 3)) test_case_3(); if ((Case == -1) || (Case == 4)) test_case_4(); } + private: + template string print_array(const vector &V) { ostringstream os; os << "{ "; for (typename vector::const_iterator iter = V.begin(); iter != V.end(); ++iter) os << '\"' << *iter << "\","; os << " }"; return os.str(); } + void verify_case(int Case, const int &Expected, const int &Received) { cerr << "Test Case #" << Case << "..."; if (Expected == Received) cerr << "PASSED" << endl; else { cerr << "FAILED" << endl; cerr << "\tExpected: \"" << Expected << '\"' << endl; cerr << "\tReceived: \"" << Received << '\"' << endl; } } + void test_case_0() { int Arr0[] = {1, 2}; vector Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); int Arr1[] = {1, 2}; vector Arg1(Arr1, Arr1 + (sizeof(Arr1) / sizeof(Arr1[0]))); int Arr2[] = {1, 2}; vector Arg2(Arr2, Arr2 + (sizeof(Arr2) / sizeof(Arr2[0]))); int Arg3 = 2; verify_case(0, Arg3, numberOfMissions(Arg0, Arg1, Arg2)); } + void test_case_1() { int Arr0[] = {3}; vector Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); int Arr1[] = {2}; vector Arg1(Arr1, Arr1 + (sizeof(Arr1) / sizeof(Arr1[0]))); int Arr2[] = {1}; vector Arg2(Arr2, Arr2 + (sizeof(Arr2) / sizeof(Arr2[0]))); int Arg3 = 0; verify_case(1, Arg3, numberOfMissions(Arg0, Arg1, Arg2)); } + void test_case_2() { int Arr0[] = {1, 3, 1, 10, 3}; vector Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); int Arr1[] = {1, 1, 3, 20, 3}; vector Arg1(Arr1, Arr1 + (sizeof(Arr1) / sizeof(Arr1[0]))); int Arr2[] = {2, 1, 1, 5, 1}; vector Arg2(Arr2, Arr2 + (sizeof(Arr2) / sizeof(Arr2[0]))); int Arg3 = 4; verify_case(2, Arg3, numberOfMissions(Arg0, Arg1, Arg2)); } + void test_case_3() { int Arr0[] = {1, 2, 100, 5, 100, 10, 100, 17, 100}; vector Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); int Arr1[] = {1, 100, 3, 100, 7, 100, 13, 100, 21}; vector Arg1(Arr1, Arr1 + (sizeof(Arr1) / sizeof(Arr1[0]))); int Arr2[] = {1, 2, 3, 4, 5, 6, 7, 8, 1}; vector Arg2(Arr2, Arr2 + (sizeof(Arr2) / sizeof(Arr2[0]))); int Arg3 = 9; verify_case(3, Arg3, numberOfMissions(Arg0, Arg1, Arg2)); } + void test_case_4() { int Arr0[] = {1, 10, 1, 2, 16, 12, 13, 19, 12, 8}; vector Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); int Arr1[] = {1, 5, 1, 8, 3, 5, 3, 16, 19, 20}; vector Arg1(Arr1, Arr1 + (sizeof(Arr1) / sizeof(Arr1[0]))); int Arr2[] = {1, 1, 1, 2, 1, 1, 2, 3, 5, 1}; vector Arg2(Arr2, Arr2 + (sizeof(Arr2) / sizeof(Arr2[0]))); int Arg3 = 7; verify_case(4, Arg3, numberOfMissions(Arg0, Arg1, Arg2)); } + +// END CUT HERE +}; +// BEGIN CUT HERE +int main() { StrengthOrIntellect().run_test(-1); } +// END CUT HERE ADDED SRM/424/1C.cpp Index: SRM/424/1C.cpp ================================================================== --- SRM/424/1C.cpp +++ SRM/424/1C.cpp @@ -0,0 +1,89 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +using namespace std; +typedef long long LL; + +template +struct FenwickTree +{ + vector x; + FenwickTree(size_t n, const T& v = T()) : x(n, v) {} + + void incr( int k, const T& a ) { // z[k] += a; + for(; k < x.size(); k|=k+1) + x[k] += a; + } + + T sum(int i, int j) { // z[i]+...+z[j] : inclusive + if(i) + return sum(0, j) - sum(0, i-1); + else { + T v = T(); + for(; j>=0; j=(j&(j+1))-1) + v += x[j]; + return v; + } + } +}; + +static const int MODVAL = 1000000007; + +class ProductOfPrices +{ +public: + int product(int N, int L, int X0, int A, int B) + { + LL result = 1; + + FenwickTree<> fw(L), nm(L); + + LL X = X0 % L; + fw.incr(X, X); + nm.incr(X, 1); + for(int i=1; i string print_array(const vector &V) { ostringstream os; os << "{ "; for (typename vector::const_iterator iter = V.begin(); iter != V.end(); ++iter) os << '\"' << *iter << "\","; os << " }"; return os.str(); } + void verify_case(int Case, const int &Expected, const int &Received) { cerr << "Test Case #" << Case << "..."; if (Expected == Received) cerr << "PASSED" << endl; else { cerr << "FAILED" << endl; cerr << "\tExpected: \"" << Expected << '\"' << endl; cerr << "\tReceived: \"" << Received << '\"' << endl; } } + void test_case_0() { int Arg0 = 5; int Arg1 = 10; int Arg2 = 3; int Arg3 = 1; int Arg4 = 1; int Arg5 = 180; verify_case(0, Arg5, product(Arg0, Arg1, Arg2, Arg3, Arg4)); } + void test_case_1() { int Arg0 = 3; int Arg1 = 20; int Arg2 = 5; int Arg3 = 2; int Arg4 = 3; int Arg5 = 64; verify_case(1, Arg5, product(Arg0, Arg1, Arg2, Arg3, Arg4)); } + void test_case_2() { int Arg0 = 4; int Arg1 = 21; int Arg2 = 1; int Arg3 = 7; int Arg4 = 1; int Arg5 = 3087; verify_case(2, Arg5, product(Arg0, Arg1, Arg2, Arg3, Arg4)); } + void test_case_3() { int Arg0 = 10; int Arg1 = 100; int Arg2 = 4; int Arg3 = 37; int Arg4 = 11; int Arg5 = 591860767; verify_case(3, Arg5, product(Arg0, Arg1, Arg2, Arg3, Arg4)); } + void test_case_4() { int Arg0 = 5; int Arg1 = 200000; int Arg2 = 999999999; int Arg3 = 123456789; int Arg4 = 987654321; int Arg5 = 499739175; verify_case(4, Arg5, product(Arg0, Arg1, Arg2, Arg3, Arg4)); } + +// END CUT HERE +}; +// BEGIN CUT HERE +int main() { ProductOfPrices().run_test(-1); } +// END CUT HERE ADDED SRM/424/2A.cpp Index: SRM/424/2A.cpp ================================================================== --- SRM/424/2A.cpp +++ SRM/424/2A.cpp @@ -0,0 +1,18 @@ +#include +#include +using namespace std; + +struct MagicSpell +{ + string fixTheSpell( string spell ) + { + stack s; + for(int i=0; i +#include +#include +using namespace std; + +struct BestRoads +{ + vector numberOfRoads(vector roads, int M) + { + int N = roads.size(); + vector ans(N); + + // Kruskal + typedef pair road; + set Q; + for(int i=0; i p(N,-1), s(N, 1); // Union-Find + int div=N, red=M-N+1; + + for(set::iterator it=Q.begin(); it!=Q.end(); ++it) + { + road r = *it; + int a=r.first; while(p[a]!=-1) a=p[a]; + int b=r.second; while(p[b]!=-1) b=p[b]; + if( a != b ) + { + ans[r.first]++, ans[r.second]++, div--; + if(s[a](); + } +}; ADDED SRM/425/1A.cpp Index: SRM/425/1A.cpp ================================================================== --- SRM/425/1A.cpp +++ SRM/425/1A.cpp @@ -0,0 +1,41 @@ +#include +#include +#include +#include +#include +using namespace std; + +typedef pair point; + +class CrazyBot +{ +public: + double getProbability(int n, int east, int west, int south, int north) + { + set vis; + vis.insert( point(0,0) ); + vis.insert( point(0,1) ); + return rec(vis, point(0,1), n-1, east, west, south, north); + } + + double rec( set& vis, point p, int N, int e, int w, int s, int n) + { + if( N == 0 ) + return 1; + int dx[] = {0,0,1,-1}; + int dy[] = {1,-1,0,0}; + int pr[] = {e,w,s,n}; + double ans = 0; + for(int i=0; i<4; ++i) + { + point q( p.first+dx[i], p.second+dy[i] ); + if( !vis.count(q) ) + { + vis.insert(q); + ans += double(pr[i])/100 * rec(vis, q, N-1, e, w, s, n); + vis.erase(q); + } + } + return ans; + } +}; ADDED SRM/425/1B.cpp Index: SRM/425/1B.cpp ================================================================== --- SRM/425/1B.cpp +++ SRM/425/1B.cpp @@ -0,0 +1,85 @@ +#include +#include +#include +#include +#include +using namespace std; + +class PiecesMover +{ +public: + static bool is_connected(int cur) + { + int py[5], px[5], pn=0; + for(int j=0; j<25; ++j) + if( cur & (1< board) + { + int start = 0; + for(int i=0; i<25; ++i) + if( board[i/5][i%5]=='*' ) + start |= (1< visited(1<<25); visited[start] = false; + vector Q(1, start); + int step = 0; + for(;; ++step) + { + vector Q2; + for(int i=0; i!=Q.size(); ++i) + { + int cur = Q[i]; + if( is_connected(cur) ) + return step; + + // next + int dx[] = {1,-1,0,0}; + int dy[] = {0,0,1,-1}; + for(int j=0; j<25; ++j) + if( cur & (1< x; +x.push_back("....."); +x.push_back("..**."); +x.push_back("....."); +x.push_back("...*."); +x.push_back("....."); + cout << PiecesMover().getMinimumMoves(x) << endl;; +} ADDED SRM/425/1C.cpp Index: SRM/425/1C.cpp ================================================================== --- SRM/425/1C.cpp +++ SRM/425/1C.cpp @@ -0,0 +1,175 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +using namespace std; +typedef long long LL; +typedef complex CMP; + +class RoadsOfKingdom { public: + vector< vector > P; + + double getProbability(vector roads) + { + P = read_input(roads); + memo = vector(1<(P.size()<(P.size()< > read_input(const vector& R) + { + int N = R.size(); + vector< vector > P(N, vector(N)); + for(int i=0; i memo; + double prob_tree(int S) + { + if( memo[S] >= 0 ) + return memo[S]; + + // the first and the second smallest IDs in S + int v, w; + for(v=0; (1< S ) + return memo[S] = 1.0; + + // Let's consider v as the "root node" of S, and try all possible "subtrees" T containing w. + // The situation is (other nodes)=v-(T where w in T) + double p = 0.0; + for(int T=S; T; T=(T-1)&S) + if( (T & 1< memo0; + double prob_zero(int v, int S) // 0 connection between v and S + { + const int key = v<= 0 ) return memo0[key]; + + double p = 1.0; + for(int u=0; (1< memo1; + double prob_one(int v, int S) // exactly 1 connection between v and S + { + const int key = v<= 0 ) return memo1[key]; + + double p = 0.0; + for(int c=0; (1< +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 double& Expected, const double& Received) { + bool ok = (abs(Expected - Received) < 1e-9); + 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(_, RoadsOfKingdom().getProbability(roads));} +int main(){ + +CASE(0) + string roads_[] = {"04", + "40"}; + vector roads(roads_, roads_+sizeof(roads_)/sizeof(*roads_)); + double _ = 0.5; +END +CASE(1) + string roads_[] = {"08", + "80"}; + vector roads(roads_, roads_+sizeof(roads_)/sizeof(*roads_)); + double _ = 1.0; +END +CASE(2) + string roads_[] = {"00", + "00"}; + vector roads(roads_, roads_+sizeof(roads_)/sizeof(*roads_)); + double _ = 0.0; +END +CASE(3) + string roads_[] = {"088", + "808", + "880"}; + vector roads(roads_, roads_+sizeof(roads_)/sizeof(*roads_)); + double _ = 0.0; +END +CASE(4) + string roads_[] = {"044", + "404", + "440"}; + vector roads(roads_, roads_+sizeof(roads_)/sizeof(*roads_)); + double _ = 0.375; +END +CASE(5) + string roads_[] = {"0701", + "7071", + "0708", + "1180"}; + vector roads(roads_, roads_+sizeof(roads_)/sizeof(*roads_)); + double _ = 0.622314453125; +END +CASE(6) + string roads_[] = {"0622100102300020", "6070000107208008", "2700207010110410", "2000188001010820", "1021032008000021", "0008300013001000", "0078200200000201", "1100002007000011", "0010010000600000", "2701830700518172", "3210000065000200", "0011000001002081", "0800010008020100", "0048002001201000", "2012200107080000", "0800101102010000"}; + vector roads(roads_, roads_+sizeof(roads_)/sizeof(*roads_)); + double _ = 5.519032471358341E-5; +END +/* +CASE(7) + string roads_[] = ; + vector roads(roads_, roads_+sizeof(roads_)/sizeof(*roads_)); + double _ = ; +END +*/ +} +// END CUT HERE ADDED SRM/425/2C.cpp Index: SRM/425/2C.cpp ================================================================== --- SRM/425/2C.cpp +++ SRM/425/2C.cpp @@ -0,0 +1,164 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +using namespace std; +typedef long long LL; +typedef complex CMP; + +class PiecesMover { public: + typedef vector< pair > state; + int getMinimumMoves(vector board) + { + set V; + vector Q(1, to_state(board)); + V.insert( Q.front() ); + for(int step=0; !Q.empty(); ++step) + { + vector Q_next; + for(int i=0; i& next = next_states(b); + for(int j=0; j& b ) + { + state s; + for(int y=0; y<5; ++y) + for(int x=0; x<5; ++x) + if( b[y][x] == '*' ) + s.push_back( make_pair(y,x) ); + sort( s.begin(), s.end() ); + return s; + } + + bool is_connected( const state& s ) + { + int N = s.size(); + vector< vector > G(N, vector(N)); + for(int i=0; i N ) + return false; + return true; + } + + vector next_states( const state& s ) + { + vector result; + for(int i=0; i > x; + 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(_, PiecesMover().getMinimumMoves(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 _ = 0; +END +CASE(2) + string board_[] = {"*...*", + ".....", + ".....", + ".....", + "*...*"} +; + vector board(board_, board_+sizeof(board_)/sizeof(*board_)); + int _ = 12; +END +/* +CASE(3) + string board_[] = ; + vector board(board_, board_+sizeof(board_)/sizeof(*board_)); + int _ = ; +END +CASE(4) + string board_[] = ; + vector board(board_, board_+sizeof(board_)/sizeof(*board_)); + int _ = ; +END +*/ +} +// END CUT HERE ADDED SRM/426-U/1A.cpp Index: SRM/426-U/1A.cpp ================================================================== --- SRM/426-U/1A.cpp +++ SRM/426-U/1A.cpp @@ -0,0 +1,62 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +using namespace std; +typedef long long LL; + +class ShufflingMachine +{ +public: + double stackDeck(vector shuffle, int maxShuffles, vector cardsReceived, int K) + { + set my( cardsReceived.begin(), cardsReceived.end() ); + + vector cur(shuffle.size()); + for(int i=0; i prob(shuffle.size()); + for(int N=1; N<=maxShuffles; ++N) + { + for(int i=0; i string print_array(const vector &V) { ostringstream os; os << "{ "; for (typename vector::const_iterator iter = V.begin(); iter != V.end(); ++iter) os << '\"' << *iter << "\","; os << " }"; return os.str(); } + void verify_case(int Case, const double &Expected, const double &Received) { cerr << "Test Case #" << Case << "..."; if (Expected == Received) cerr << "PASSED" << endl; else { cerr << "FAILED" << endl; cerr << "\tExpected: \"" << Expected << '\"' << endl; cerr << "\tReceived: \"" << Received << '\"' << endl; } } + void test_case_0() { int Arr0[] = {1,0}; vector Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); int Arg1 = 3; int Arr2[] = {0}; vector Arg2(Arr2, Arr2 + (sizeof(Arr2) / sizeof(Arr2[0]))); int Arg3 = 1; double Arg4 = 0.6666666666666666; verify_case(0, Arg4, stackDeck(Arg0, Arg1, Arg2, Arg3)); } + void test_case_1() { int Arr0[] = {1,2,0}; vector Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); int Arg1 = 5; int Arr2[] = {0}; vector Arg2(Arr2, Arr2 + (sizeof(Arr2) / sizeof(Arr2[0]))); int Arg3 = 2; double Arg4 = 0.8; verify_case(1, Arg4, stackDeck(Arg0, Arg1, Arg2, Arg3)); } + void test_case_2() { int Arr0[] = {1,2,0,4,3}; vector Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); int Arg1 = 7; int Arr2[] = {0,3}; vector Arg2(Arr2, Arr2 + (sizeof(Arr2) / sizeof(Arr2[0]))); int Arg3 = 2; double Arg4 = 1.0; verify_case(2, Arg4, stackDeck(Arg0, Arg1, Arg2, Arg3)); } + void test_case_3() { int Arr0[] = {0,4,3,5,2,6,1}; vector Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); int Arg1 = 19; int Arr2[] = {1,3,5}; vector Arg2(Arr2, Arr2 + (sizeof(Arr2) / sizeof(Arr2[0]))); int Arg3 = 2; double Arg4 = 1.0526315789473684; verify_case(3, Arg4, stackDeck(Arg0, Arg1, Arg2, Arg3)); } + void test_case_4() { int Arr0[] = {3,4,7,2,8,5,6,1,0,9}; vector Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); int Arg1 = 47; int Arr2[] = {6,3,5,2,8,7,4}; vector Arg2(Arr2, Arr2 + (sizeof(Arr2) / sizeof(Arr2[0]))); int Arg3 = 8; double Arg4 = 6.297872340425532; verify_case(4, Arg4, stackDeck(Arg0, Arg1, Arg2, Arg3)); } + +// END CUT HERE +}; +// BEGIN CUT HERE +int main() { + ShufflingMachine().run_test(-1); +} +// END CUT HERE ADDED SRM/426-U/1B.cpp Index: SRM/426-U/1B.cpp ================================================================== --- SRM/426-U/1B.cpp +++ SRM/426-U/1B.cpp @@ -0,0 +1,100 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +using namespace std; +typedef long long LL; + +class CatchTheMice +{ +public: + double intersect(double b1, double a1, double b2, double a2) + { + // a1x + b1 == a2x + b2 + if( a1 == a2 ) return -1; + return (b2-b1) / (a1-a2); + } + + double largestCage(vector xp, vector yp, vector xv, vector yv) + { + vector t; // collision time + t.push_back(0); + + int n = xp.size(); + for(int i=0; i0) t.push_back(ct); + ct = intersect(yp[i], yv[i], yp[j], yv[j]); + if(ct>0) t.push_back(ct); + } + + sort(t.begin(), t.end()); + t.erase(unique(t.begin(), t.end()), t.end()); + t.push_back( t.back()+1.0 ); + + double Lmin = 1e+300; + for(int i=0; i+1 xp[j]+xv[j]*ti ) j_xm=j, xm=xp[j]+xv[j]*ti; + if( xM < xp[j]+xv[j]*ti ) j_xM=j, xM=xp[j]+xv[j]*ti; + if( ym > yp[j]+yv[j]*ti ) j_ym=j, ym=yp[j]+yv[j]*ti; + if( yM < yp[j]+yv[j]*ti ) j_yM=j, yM=yp[j]+yv[j]*ti; + } + + int Xp = xp[j_xM]-xp[j_xm]; + int Xv = xv[j_xM]-xv[j_xm]; + int Yp = yp[j_yM]-yp[j_ym]; + int Yv = yv[j_yM]-yv[j_ym]; + + // min of max(x,y) + double ct = intersect(Xp,Xv,Yp,Yv); + if( t[i] <= ct && ct <= t[i+1] ) + Lmin = min(Lmin, Xp + Xv*ct); + else { + Lmin = min(Lmin, max(Xp+Xv*t[i+0], Yp+Yv*t[i+0])); + Lmin = min(Lmin, max(Xp+Xv*t[i+1], Yp+Yv*t[i+1])); + } + } + return Lmin; + } + +// BEGIN CUT HERE + public: + void run_test(int Case) { if ((Case == -1) || (Case == 0)) test_case_0(); if ((Case == -1) || (Case == 1)) test_case_1(); if ((Case == -1) || (Case == 2)) test_case_2(); if ((Case == -1) || (Case == 3)) test_case_3(); if ((Case == -1) || (Case == 4)) test_case_4(); if ((Case == -1) || (Case == 5)) test_case_5(); if ((Case == -1) || (Case == 6)) test_case_6(); } + private: + template string print_array(const vector &V) { ostringstream os; os << "{ "; for (typename vector::const_iterator iter = V.begin(); iter != V.end(); ++iter) os << '\"' << *iter << "\","; os << " }"; return os.str(); } + void verify_case(int Case, const double &Expected, const double &Received) { cerr << "Test Case #" << Case << "..."; if (Expected == Received) cerr << "PASSED" << endl; else { cerr << "FAILED" << endl; cerr << "\tExpected: \"" << Expected << '\"' << endl; cerr << "\tReceived: \"" << Received << '\"' << endl; } } + void test_case_0() { int Arr0[] = {0,10}; vector Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); int Arr1[] = {0,10}; vector Arg1(Arr1, Arr1 + (sizeof(Arr1) / sizeof(Arr1[0]))); int Arr2[] = {10,-10}; vector Arg2(Arr2, Arr2 + (sizeof(Arr2) / sizeof(Arr2[0]))); int Arr3[] = {0,0}; vector Arg3(Arr3, Arr3 + (sizeof(Arr3) / sizeof(Arr3[0]))); double Arg4 = 10.0; verify_case(0, Arg4, largestCage(Arg0, Arg1, Arg2, Arg3)); } + void test_case_1() { int Arr0[] = {0,10,0}; vector Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); int Arr1[] = {0,0,10}; vector Arg1(Arr1, Arr1 + (sizeof(Arr1) / sizeof(Arr1[0]))); int Arr2[] = {1,-6,4}; vector Arg2(Arr2, Arr2 + (sizeof(Arr2) / sizeof(Arr2[0]))); int Arr3[] = {4,5,-4}; vector Arg3(Arr3, Arr3 + (sizeof(Arr3) / sizeof(Arr3[0]))); double Arg4 = 3.0; verify_case(1, Arg4, largestCage(Arg0, Arg1, Arg2, Arg3)); } + void test_case_2() { int Arr0[] = {50,10,30,15}; vector Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); int Arr1[] = {-10,30,20,40}; vector Arg1(Arr1, Arr1 + (sizeof(Arr1) / sizeof(Arr1[0]))); int Arr2[] = {-5,-10,-15,-5}; vector Arg2(Arr2, Arr2 + (sizeof(Arr2) / sizeof(Arr2[0]))); int Arr3[] = {40,-10,-1,-50}; vector Arg3(Arr3, Arr3 + (sizeof(Arr3) / sizeof(Arr3[0]))); double Arg4 = 40.526315789473685; verify_case(2, Arg4, largestCage(Arg0, Arg1, Arg2, Arg3)); } + void test_case_3() { int Arr0[] = {0,10,10,0}; vector Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); int Arr1[] = {0,0,10,10}; vector Arg1(Arr1, Arr1 + (sizeof(Arr1) / sizeof(Arr1[0]))); int Arr2[] = {1,0,-1,0}; vector Arg2(Arr2, Arr2 + (sizeof(Arr2) / sizeof(Arr2[0]))); int Arr3[] = {0,1,0,-1}; vector Arg3(Arr3, Arr3 + (sizeof(Arr3) / sizeof(Arr3[0]))); double Arg4 = 10.0; verify_case(3, Arg4, largestCage(Arg0, Arg1, Arg2, Arg3)); } + void test_case_4() { int Arr0[] = {13,50,100,40,-100}; vector Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); int Arr1[] = {20,20,-150,-40,63}; vector Arg1(Arr1, Arr1 + (sizeof(Arr1) / sizeof(Arr1[0]))); int Arr2[] = {4,50,41,-41,-79}; vector Arg2(Arr2, Arr2 + (sizeof(Arr2) / sizeof(Arr2[0]))); int Arr3[] = {1,1,1,3,-1}; vector Arg3(Arr3, Arr3 + (sizeof(Arr3) / sizeof(Arr3[0]))); double Arg4 = 212.78688524590163; verify_case(4, Arg4, largestCage(Arg0, Arg1, Arg2, Arg3)); } + void test_case_5() { int Arr0[] = {0,10}; vector Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); int Arr1[] = {0,0}; vector Arg1(Arr1, Arr1 + (sizeof(Arr1) / sizeof(Arr1[0]))); int Arr2[] = {5,5}; vector Arg2(Arr2, Arr2 + (sizeof(Arr2) / sizeof(Arr2[0]))); int Arr3[] = {3,3}; vector Arg3(Arr3, Arr3 + (sizeof(Arr3) / sizeof(Arr3[0]))); double Arg4 = 10.0; verify_case(5, Arg4, largestCage(Arg0, Arg1, Arg2, Arg3)); } + void test_case_6() { int Arr0[] = {-49,-463,-212,-204,-557,-67,-374,-335,-590,-4}; vector Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); int Arr1[] = {352,491,280,355,129,78,404,597,553,445}; vector Arg1(Arr1, Arr1 + (sizeof(Arr1) / sizeof(Arr1[0]))); int Arr2[] = {-82,57,-23,-32,89,-72,27,17,100,-94}; vector Arg2(Arr2, Arr2 + (sizeof(Arr2) / sizeof(Arr2[0]))); int Arr3[] = {-9,-58,9,-14,56,75,-32,-98,-81,-43}; vector Arg3(Arr3, Arr3 + (sizeof(Arr3) / sizeof(Arr3[0]))); double Arg4 = 25.467532467532468; verify_case(6, Arg4, largestCage(Arg0, Arg1, Arg2, Arg3)); } + +// END CUT HERE +}; +// BEGIN CUT HERE +int main() { + CatchTheMice().run_test(-1); +} +// END CUT HERE ADDED SRM/426-U/1C-take1.cpp Index: SRM/426-U/1C-take1.cpp ================================================================== --- SRM/426-U/1C-take1.cpp +++ SRM/426-U/1C-take1.cpp @@ -0,0 +1,150 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +using namespace std; +typedef long long LL; + +vector solve_linear_eq( int n, vector< vector > M, const vector& V ) +{ + vector A(V); + for(int i=0; i 1e-4 ) + throw "no answer"; + } + return A; +} + + +class LongStraightRoad +{ +public: + int distanceToDestination(vector signs, string destination) + { + typedef map< string, vector< pair > > MSV; + MSV data; + for(int i=0; i>n>>d; + char c; sin>>c; // ';' + data[n].push_back( make_pair(double(d), i) ); + } + } + if( data[destination].empty() ) + return -1; + + vector< vector > M; + vector V; + for(MSV::iterator it=data.begin(); it!=data.end(); ++it) + { + vector< pair >& d = it->second; + for(int i=0; i+1 Mk(signs.size()); + Mk[d[i].second] = 1; + Mk[d[i+1].second] = -1; + M.push_back(Mk); + V.push_back( d[i+1].first - d[i].first ); + } + } + // S[last] = 0.0 + vector Mk(signs.size()); + Mk[signs.size()-1] = 1; + M.push_back(Mk); + V.push_back(0); + + if( signs.size() > M.size() ) + return -1; + try { + vector S = solve_linear_eq( signs.size(), M, V ); + for(int i=0; i+1 S[i+1] ) + return -1; + double result = + S[ data[destination][0].second ] + data[destination][0].first; + int rI = (int)floor(result+0.5); + if( abs(result-rI) > 1e-4 ) + return -1; + return ( rI < 0 ? -1 : rI ); + } catch(const char*) { + return -1; + } + } + +// BEGIN CUT HERE + public: + void run_test(int Case) { if ((Case == -1) || (Case == 0)) test_case_0(); if ((Case == -1) || (Case == 1)) test_case_1(); if ((Case == -1) || (Case == 2)) test_case_2(); if ((Case == -1) || (Case == 3)) test_case_3(); if ((Case == -1) || (Case == 4)) test_case_4(); } + private: + template string print_array(const vector &V) { ostringstream os; os << "{ "; for (typename vector::const_iterator iter = V.begin(); iter != V.end(); ++iter) os << '\"' << *iter << "\","; os << " }"; return os.str(); } + void verify_case(int Case, const int &Expected, const int &Received) { cerr << "Test Case #" << Case << "..."; if (Expected == Received) cerr << "PASSED" << endl; else { cerr << "FAILED" << endl; cerr << "\tExpected: \"" << Expected << '\"' << endl; cerr << "\tReceived: \"" << Received << '\"' << endl; } } + void test_case_0() { string Arr0[] = {"COLCHESTER 5;GLASTONBURY 25;MARLBOROUGH 13" +,"MARLBOROUGH 2"}; vector Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); string Arg1 = "GLASTONBURY"; int Arg2 = 14; verify_case(0, Arg2, distanceToDestination(Arg0, Arg1)); } + void test_case_1() { string Arr0[] = {"COLCHESTER 5;GLASTONBURY 25;MARLBOROUGH 13" +,"GLASTONBURY 13;MARLBOROUGH 2"}; vector Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); string Arg1 = "GLASTONBURY"; int Arg2 = -1; verify_case(1, Arg2, distanceToDestination(Arg0, Arg1)); } + void test_case_2() { string Arr0[] = {"A 25;B 15" +,"A 2"}; vector Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); string Arg1 = "B"; int Arg2 = -1; verify_case(2, Arg2, distanceToDestination(Arg0, Arg1)); } + void test_case_3() { string Arr0[] = {"YVO 60;J 62" +,"K 45" +,"K 40;MV 17" +,"K 37;YVO 44;HY 48;CC 69;D 77;YXF 80" +,"YVO 30;B 37;RB 59"}; vector Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); string Arg1 = "MV"; int Arg2 = 0; verify_case(3, Arg2, distanceToDestination(Arg0, Arg1)); } + void test_case_4() { string Arr0[] = {"A 200;B 150" +,"C 45;D 100;E 150" +,"C 25;E 130" +,"F 80;G 65" +,"G 35;H 160" +,"A 160" +,"H 130"}; vector Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); string Arg1 = "F"; int Arg2 = -1; verify_case(4, Arg2, distanceToDestination(Arg0, Arg1)); } + +// END CUT HERE +}; +// BEGIN CUT HERE +int main() { + LongStraightRoad().run_test(-1); +} +// END CUT HERE ADDED SRM/426-U/1C-take2.cpp Index: SRM/426-U/1C-take2.cpp ================================================================== --- SRM/426-U/1C-take2.cpp +++ SRM/426-U/1C-take2.cpp @@ -0,0 +1,156 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +using namespace std; +typedef long long LL; + + +class LongStraightRoad +{ +public: + typedef pair edge; // (index,dist) + int distanceToDestination(vector signs, string destination) + { + int n = signs.size(); + + // parse + vector< map > data(n); + map > n2d; // name -> (index, dist) + for(int i=0; i>n>>d; + char c; sin>>c; // ';' + data[i][n] = d; + n2d[n].push_back( edge(i,d) ); + } + } + + // construct the graph + vector< vector > G(n); + for(int i=0; i::iterator it=data[i].begin(); it!=data[i].end(); ++it) + { + vector& es = n2d[it->first]; + for(int j=0; jsecond - es[j].second) ); + } + } + + try { + vector done(n, false); + vector p(n); + { + vector visited(n, false); + dfs(n-1, 0, G, visited, p); + + vector kn; + for(int i=0; i& es = n2d[destination]; + int result = p[es[0].first] + es[0].second; + if( result < 0 ) + return -1; + + for(int v=0; v visited(n, false); + dfs(v, 0, G, visited, p); + + vector kn; + for(int i=0; i& kn ) + { + for(int i=0; i+1= kn[i+1] ) + return false; + return true; + } + + void dfs(int v, int d, vector< vector >& G, vector& visited, vector& p) + { + if( visited[v] ) { + if( d != p[v] ) { +cerr << d << " " << v << " " << p[v] << endl; + throw "bad"; +} + return; + } + visited[v] = true; + p[v] = d; + for(int i=0; i" << G[v][i].first << " : " << G[v][i].second << endl; + dfs( G[v][i].first, d+G[v][i].second, G, visited, p ); +} + } + +// BEGIN CUT HERE + public: + void run_test(int Case) { if ((Case == -1) || (Case == 0)) test_case_0(); if ((Case == -1) || (Case == 1)) test_case_1(); if ((Case == -1) || (Case == 2)) test_case_2(); if ((Case == -1) || (Case == 3)) test_case_3(); if ((Case == -1) || (Case == 4)) test_case_4(); } + private: + template string print_array(const vector &V) { ostringstream os; os << "{ "; for (typename vector::const_iterator iter = V.begin(); iter != V.end(); ++iter) os << '\"' << *iter << "\","; os << " }"; return os.str(); } + void verify_case(int Case, const int &Expected, const int &Received) { cerr << "Test Case #" << Case << "..."; if (Expected == Received) cerr << "PASSED" << endl; else { cerr << "FAILED" << endl; cerr << "\tExpected: \"" << Expected << '\"' << endl; cerr << "\tReceived: \"" << Received << '\"' << endl; } } + void test_case_0() { string Arr0[] = {"COLCHESTER 5;GLASTONBURY 25;MARLBOROUGH 13" +,"MARLBOROUGH 2"}; vector Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); string Arg1 = "GLASTONBURY"; int Arg2 = 14; verify_case(0, Arg2, distanceToDestination(Arg0, Arg1)); } + void test_case_1() { string Arr0[] = {"COLCHESTER 5;GLASTONBURY 25;MARLBOROUGH 13" +,"GLASTONBURY 13;MARLBOROUGH 2"}; vector Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); string Arg1 = "GLASTONBURY"; int Arg2 = -1; verify_case(1, Arg2, distanceToDestination(Arg0, Arg1)); } + void test_case_2() { string Arr0[] = {"A 25;B 15" +,"A 2"}; vector Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); string Arg1 = "B"; int Arg2 = -1; verify_case(2, Arg2, distanceToDestination(Arg0, Arg1)); } + void test_case_3() { string Arr0[] = {"YVO 60;J 62" +,"K 45" +,"K 40;MV 17" +,"K 37;YVO 44;HY 48;CC 69;D 77;YXF 80" +,"YVO 30;B 37;RB 59"}; vector Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); string Arg1 = "MV"; int Arg2 = 0; verify_case(3, Arg2, distanceToDestination(Arg0, Arg1)); } + void test_case_4() { string Arr0[] = {"A 200;B 150" +,"C 45;D 100;E 150" +,"C 25;E 130" +,"F 80;G 65" +,"G 35;H 160" +,"A 160" +,"H 130"}; vector Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); string Arg1 = "F"; int Arg2 = -1; verify_case(4, Arg2, distanceToDestination(Arg0, Arg1)); } + +// END CUT HERE +}; +// BEGIN CUT HERE +int main() { + LongStraightRoad().run_test(-1); +} +// END CUT HERE ADDED SRM/427/1A.cpp Index: SRM/427/1A.cpp ================================================================== --- SRM/427/1A.cpp +++ SRM/427/1A.cpp @@ -0,0 +1,53 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +using namespace std; +typedef long long LL; + +int gcd(int a, int b) +{ + while(a) { + int t = a; + a = b%a; + b = t; + } + return b; +} + +class DesignCalendar +{ +public: + int shortestPeriod(int dayLength, int yearLength) + { + return dayLength / gcd(yearLength, dayLength); + } + +// BEGIN CUT HERE + public: + void run_test(int Case) { if ((Case == -1) || (Case == 0)) test_case_0(); if ((Case == -1) || (Case == 1)) test_case_1(); if ((Case == -1) || (Case == 2)) test_case_2(); if ((Case == -1) || (Case == 3)) test_case_3(); if ((Case == -1) || (Case == 4)) test_case_4(); if ((Case == -1) || (Case == 5)) test_case_5(); } + private: + template string print_array(const vector &V) { ostringstream os; os << "{ "; for (typename vector::const_iterator iter = V.begin(); iter != V.end(); ++iter) os << '\"' << *iter << "\","; os << " }"; return os.str(); } + void verify_case(int Case, const int &Expected, const int &Received) { cerr << "Test Case #" << Case << "..."; if (Expected == Received) cerr << "PASSED" << endl; else { cerr << "FAILED" << endl; cerr << "\tExpected: \"" << Expected << '\"' << endl; cerr << "\tReceived: \"" << Received << '\"' << endl; } } + void test_case_0() { int Arg0 = 4; int Arg1 = 1461; int Arg2 = 4; verify_case(0, Arg2, shortestPeriod(Arg0, Arg1)); } + void test_case_1() { int Arg0 = 86400; int Arg1 = 31558150; int Arg2 = 1728; verify_case(1, Arg2, shortestPeriod(Arg0, Arg1)); } + void test_case_2() { int Arg0 = 98; int Arg1 = 100; int Arg2 = 49; verify_case(2, Arg2, shortestPeriod(Arg0, Arg1)); } + void test_case_3() { int Arg0 = 1000 ; int Arg1 = 50000100; int Arg2 = 10; verify_case(3, Arg2, shortestPeriod(Arg0, Arg1)); } + void test_case_4() { int Arg0 = 5673; int Arg1 = 28565335; int Arg2 = 5673; verify_case(4, Arg2, shortestPeriod(Arg0, Arg1)); } + void test_case_5() { int Arg0 = 5555; int Arg1 = 846555; int Arg2 = 1111; verify_case(5, Arg2, shortestPeriod(Arg0, Arg1)); } + +// END CUT HERE +}; +// BEGIN CUT HERE +int main() { DesignCalendar().run_test(-1); } +// END CUT HERE ADDED SRM/427/1B.cpp Index: SRM/427/1B.cpp ================================================================== --- SRM/427/1B.cpp +++ SRM/427/1B.cpp @@ -0,0 +1,117 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +using namespace std; +typedef long long LL; + +LL dig(LL x) +{ + if(x<=9) return x; + LL s = 0; + while(x) + s += x%10, x/=10; + return dig(s); +} +class LocateTreasure +{ +public: + string location(int K, int m) + { + vector ds; + ds.push_back(1); + LL found[10] = {-1, 0, -1, -1, -1, -1, -1, -1, -1, -1}; + LL loop_beg, loop_end; + for(LL i=1;; ++i) { + LL d = dig( ds.back()*m ); + if( found[d] < 0 ) + { + ds.push_back(d); + found[d] = i; + } + else + { + loop_beg = found[d]; + loop_end = i; + break; + } + } + + vector ds2 = ds; + ds2.insert(ds2.end(), ds.begin()+loop_beg, ds.begin()+loop_end); + ds2.insert(ds2.end(), ds.begin()+loop_beg, ds.begin()+loop_end); + ds2.insert(ds2.end(), ds.begin()+loop_beg, ds.begin()+loop_end); + ds2.insert(ds2.end(), ds.begin()+loop_beg, ds.begin()+loop_end); + ds = ds2; + + LL x=0, y=0; + + if( K <= loop_beg ) + { + for(LL i=0; i string print_array(const vector &V) { ostringstream os; os << "{ "; for (typename vector::const_iterator iter = V.begin(); iter != V.end(); ++iter) os << '\"' << *iter << "\","; os << " }"; return os.str(); } + void verify_case(int Case, const string &Expected, const string &Received) { cerr << "Test Case #" << Case << "..."; if (Expected == Received) cerr << "PASSED" << endl; else { cerr << "FAILED" << endl; cerr << "\tExpected: \"" << Expected << '\"' << endl; cerr << "\tReceived: \"" << Received << '\"' << endl; } } + void test_case_0() { int Arg0 = 5; int Arg1 = 2; string Arg2 = "-6 4"; verify_case(0, Arg2, location(Arg0, Arg1)); } + void test_case_1() { int Arg0 = 99; int Arg1 = 1; string Arg2 = "1 0"; verify_case(1, Arg2, location(Arg0, Arg1)); } + void test_case_2() { int Arg0 = 6; int Arg1 = 9; string Arg2 = "9 1"; verify_case(2, Arg2, location(Arg0, Arg1)); } + +// END CUT HERE +}; +// BEGIN CUT HERE +int main() { LocateTreasure().run_test(-1); } +// END CUT HERE ADDED SRM/427/1C.cpp Index: SRM/427/1C.cpp ================================================================== --- SRM/427/1C.cpp +++ SRM/427/1C.cpp @@ -0,0 +1,96 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +using namespace std; +typedef long long LL; + +static const LL MODVAL = 1234567891; + +LL ADD(LL x, LL y) { return (x+y)%MODVAL; } +LL MUL(LL x, LL y) { return (x*y)%MODVAL; } +LL FACT(LL x) { return x<=1 ? 1 : MUL(x, FACT(x-1)); } + +class PSequence +{ +public: + map,int>, LL> memo; + + int count(vector S, int p) + { + memo.clear(); + + vector cs; // congruences mod p + + bool used[30] = {}; + for(int i=0; i& cs, int forbidden, int n ) + { + if( n == 0 ) + return 1; + + int v = (forbidden >= 0 ? cs[forbidden] : -1); + pair,int> key(cs, forbidden); + sort(key.first.begin(), key.first.end()); + key.second = find(key.first.begin(), key.first.end(), v) - key.first.begin(); + + if( memo.count(key) ) + return memo[key]; + + LL np = 0; + for(int i=0; i string print_array(const vector &V) { ostringstream os; os << "{ "; for (typename vector::const_iterator iter = V.begin(); iter != V.end(); ++iter) os << '\"' << *iter << "\","; os << " }"; return os.str(); } + void verify_case(int Case, const int &Expected, const int &Received) { cerr << "Test Case #" << Case << "..."; if (Expected == Received) cerr << "PASSED" << endl; else { cerr << "FAILED" << endl; cerr << "\tExpected: \"" << Expected << '\"' << endl; cerr << "\tReceived: \"" << Received << '\"' << endl; } } + void test_case_0() { int Arr0[] = {-1,0,1,2,3}; vector Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); int Arg1 = 10; int Arg2 = 120; verify_case(0, Arg2, count(Arg0, Arg1)); } + void test_case_1() { int Arr0[] = {6,2}; vector Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); int Arg1 = 4; int Arg2 = 0; verify_case(1, Arg2, count(Arg0, Arg1)); } + void test_case_2() { int Arr0[] = {1,2,3,4}; vector Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); int Arg1 = 3; int Arg2 = 12; verify_case(2, Arg2, count(Arg0, Arg1)); } + void test_case_3() { int Arr0[] = {4,6,8,-3,7}; vector Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); int Arg1 = 2; int Arg2 = 12; verify_case(3, Arg2, count(Arg0, Arg1)); } + void test_case_4() { + int Arr0[] = {0,1,2,3,4,5,6,7,8,9, +10,11,12,13,14,15,16,17,18,19, +20,21,22,23,24,25,26,27,28,29}; +vector Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); + int Arg1 = 15; int Arg2 = 12; verify_case(4, Arg2, count(Arg0, Arg1)); } + +// END CUT HERE +}; +// BEGIN CUT HERE +int main() { PSequence().run_test(-1); } +// END CUT HERE ADDED SRM/428/1A.cpp Index: SRM/428/1A.cpp ================================================================== --- SRM/428/1A.cpp +++ SRM/428/1A.cpp @@ -0,0 +1,49 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +using namespace std; +typedef long long LL; + +class TheLuckyString +{ +public: + int count(string s) + { + int cnt = 0; + + sort(s.begin(), s.end()); + do + if( adjacent_find(s.begin(), s.end()) == s.end() ) + ++cnt; + while( next_permutation(s.begin(), s.end()) ); + + return cnt; + } + +// BEGIN CUT HERE + public: + void run_test(int Case) { if ((Case == -1) || (Case == 0)) test_case_0(); if ((Case == -1) || (Case == 1)) test_case_1(); if ((Case == -1) || (Case == 2)) test_case_2(); if ((Case == -1) || (Case == 3)) test_case_3(); } + private: + template string print_array(const vector &V) { ostringstream os; os << "{ "; for (typename vector::const_iterator iter = V.begin(); iter != V.end(); ++iter) os << '\"' << *iter << "\","; os << " }"; return os.str(); } + void verify_case(int Case, const int &Expected, const int &Received) { cerr << "Test Case #" << Case << "..."; if (Expected == Received) cerr << "PASSED" << endl; else { cerr << "FAILED" << endl; cerr << "\tExpected: \"" << Expected << '\"' << endl; cerr << "\tReceived: \"" << Received << '\"' << endl; } } + void test_case_0() { string Arg0 = "ab"; int Arg1 = 2; verify_case(0, Arg1, count(Arg0)); } + void test_case_1() { string Arg0 = "aaab"; int Arg1 = 0; verify_case(1, Arg1, count(Arg0)); } + void test_case_2() { string Arg0 = "aabbbaa"; int Arg1 = 1; verify_case(2, Arg1, count(Arg0)); } + void test_case_3() { string Arg0 = "abcdefghij"; int Arg1 = 3628800; verify_case(3, Arg1, count(Arg0)); } + +// END CUT HERE +}; +// BEGIN CUT HERE +int main() { TheLuckyString().run_test(-1); } +// END CUT HERE ADDED SRM/428/1B.cpp Index: SRM/428/1B.cpp ================================================================== --- SRM/428/1B.cpp +++ SRM/428/1B.cpp @@ -0,0 +1,85 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +using namespace std; +typedef long long LL; + +static const LL MODVAL = 1234567891; + +LL ADD(LL x, LL y) { return (x+y)%MODVAL; } +LL SUB(LL x, LL y) { return (x-y+MODVAL)%MODVAL; } +LL MUL(LL x, LL y) { return (x*y)%MODVAL; } +LL POW(LL x, LL e) { + LL v = 1; + for(;e;x=MUL(x,x),e>>=1) + if(e&1) + v = MUL(v, x); + return v; +} +LL DIV(LL x, LL y) { return MUL(x, POW(y, MODVAL-2)); } +LL C(LL n, LL k) { + LL v = 1; + for(LL i=1; i<=k; ++i) + v = DIV(MUL(v, n-i+1), i); + return v; +} +LL GSS(LL k, LL b, LL e) // k^b + k^b+1 + ... + k^e +{ + if( b > e ) return 0; + if( k <= 1 ) return k*(e-b+1); + return DIV(SUB(POW(k, e+1), POW(k,b)), k-1); +} + +class TheLongPalindrome +{ +public: + int count(int n, int k) + { + LL ans = 0; + for(int i=1; i<=k; ++i) + { + LL aa = f(i,i,n); + for(int j=i-1; j>=1; --j) + aa = (i-j)%2 + ? SUB(aa, MUL(f(j,i,n), C(i,j))) + : ADD(aa, MUL(f(j,i,n), C(i,j))); + ans = ADD(ans, MUL(aa, C(26,i))); + } + return ans; + } + + LL f(LL i, LL b, LL n) // i^b + i^b + i^b+1 + i^b+1 + ... + (nth) + { + LL v = MUL(GSS(i, b, n/2), 2); + if( n%2 == 1 ) + v = ADD(v, POW(i, n/2+1)); + return v; + } + +// BEGIN CUT HERE + public: + void run_test(int Case) { if ((Case == -1) || (Case == 0)) test_case_0(); if ((Case == -1) || (Case == 1)) test_case_1(); if ((Case == -1) || (Case == 2)) test_case_2(); if ((Case == -1) || (Case == 3)) test_case_3(); } + private: + template string print_array(const vector &V) { ostringstream os; os << "{ "; for (typename vector::const_iterator iter = V.begin(); iter != V.end(); ++iter) os << '\"' << *iter << "\","; os << " }"; return os.str(); } + void verify_case(int Case, const int &Expected, const int &Received) { cerr << "Test Case #" << Case << "..."; if (Expected == Received) cerr << "PASSED" << endl; else { cerr << "FAILED" << endl; cerr << "\tExpected: \"" << Expected << '\"' << endl; cerr << "\tReceived: \"" << Received << '\"' << endl; } } + void test_case_0() { int Arg0 = 1; int Arg1 = 1; int Arg2 = 26; verify_case(0, Arg2, count(Arg0, Arg1)); } + void test_case_1() { int Arg0 = 2; int Arg1 = 10; int Arg2 = 52; verify_case(1, Arg2, count(Arg0, Arg1)); } + void test_case_2() { int Arg0 = 3; int Arg1 = 2; int Arg2 = 728; verify_case(2, Arg2, count(Arg0, Arg1)); } + void test_case_3() { int Arg0 = 44; int Arg1 = 7; int Arg2 = 240249781; verify_case(3, Arg2, count(Arg0, Arg1)); } + +// END CUT HERE +}; +// BEGIN CUT HERE +int main() { TheLongPalindrome().run_test(-1); } +// END CUT HERE ADDED SRM/428/1C.cpp Index: SRM/428/1C.cpp ================================================================== --- SRM/428/1C.cpp +++ SRM/428/1C.cpp @@ -0,0 +1,129 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +using namespace std; +typedef long long LL; + +char memo[81*81*81*81]; +int n, X[16]; +static const char NIL = 99; + +bool win[243]; + +class TheStringGame +{ +public: + string winner(string s) + { + // construct the winning condition table + for(int mm=0; mm<243; ++mm) + win[mm] = (mm/ 9%3==0 && + ( mm/81%3==2 && mm/27%3==1 // LOX + || mm/27%3==2 && mm/ 3%3==2 // LXL + || mm/ 3%3==1 && mm/ 1%3==2 )); // XOL + + // read the input + int m=0, numX=0; + { + memset(memo, NIL, sizeof(memo)); + n = s.size(); + for(int i=0; i0 ? "John" : "Brus") << " " << numX-abs(r)+1; + return sout.str(); + } + return "Draw"; + } + + static int rev(int m) + { + int z = 0; + for(int i=0; i +double start_time;string timer() { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) << " msec)"; return os.str(); } + +template string print_array(const vector &V) { ostringstream os; os << "{ "; for (typename vector::const_iterator iter = V.begin(); iter != V.end(); ++iter) os << '\"' << *iter << "\","; os << " }"; return os.str(); } +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;} + +template struct Case_ { Case_(){start_time=clock();} }; +char Test_(...); +int Test_(Case_<0>) { + string s = "XXOXXXLXLX"; + string RetVal = "John 1"; + return verify_case(RetVal, TheStringGame().winner(s)); } +int Test_(Case_<1>) { + string s = "LXXLXXL"; + string RetVal = "Brus 2"; + return verify_case(RetVal, TheStringGame().winner(s)); } +int Test_(Case_<2>) { + string s = "LLOOLLOOLLOOLLOO"; + string RetVal = "Draw"; + return verify_case(RetVal, TheStringGame().winner(s)); } +int Test_(Case_<3>) { + string s = "XXXXXXXXXXXXXXXX"; + string RetVal = "Brus 16"; + return verify_case(RetVal, TheStringGame().winner(s)); } +int Test_(Case_<4>) { + string s = "XXXXXXXXXXXXXXXO"; + string RetVal = "John 15"; + return verify_case(RetVal, TheStringGame().winner(s)); } + +template void Run_() { cerr << "Test Case #" << N << "..." << flush; Test_(Case_()); Run_()))==1 ? -1 : N+1>(); } +template<> void Run_<-1>() {} +int main() { Run_<0>(); } +// END CUT HERE ADDED SRM/429/1A.cpp Index: SRM/429/1A.cpp ================================================================== --- SRM/429/1A.cpp +++ SRM/429/1A.cpp @@ -0,0 +1,72 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +using namespace std; +typedef long long LL; + +class SubrectanglesOfTable +{ +public: + vector getQuantity(vector table) + { + for(int i=0,n=table.size(); i ans(26); + + LL Y = table.size(), X = table[0].size(); + for(LL y=0; y +double start_time;string timer() { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) << " msec)"; return os.str(); } + +template string print_array(const vector &V) { ostringstream os; os << "{ "; for (typename vector::const_iterator iter = V.begin(); iter != V.end(); ++iter) os << '\"' << *iter << "\","; os << " }"; return os.str(); } +int verify_case(const vector &Expected, const vector &Received) { if (Expected == Received) cerr << "PASSED" << timer() << endl; else { cerr << "FAILED" << timer() << endl; cerr << "\tExpected: " << print_array(Expected) << endl; cerr << "\tReceived: " << print_array(Received) << endl; } return 0;} + +template struct Case_ { Case_(){start_time=clock();} }; +char Test_(...); +int Test_(Case_<0>) { + string table_[] = {"OK"}; + vector table(table_, table_+sizeof(table_)/sizeof(*table_)); + long RetVal_[] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 40, 0, 0, 0, 40, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }; + vector RetVal(RetVal_, RetVal_+sizeof(RetVal_)/sizeof(*RetVal_)); + return verify_case(RetVal, SubrectanglesOfTable().getQuantity(table)); } +int Test_(Case_<1>) { + string table_[] = {"GOOD", "LUCK"}; + vector table(table_, table_+sizeof(table_)/sizeof(*table_)); + long RetVal_[] = {0, 0, 320, 280, 0, 0, 280, 0, 0, 0, 280, 280, 0, 0, 640, 0, 0, 0, 0, 0, 320, 0, 0, 0, 0, 0 }; + vector RetVal(RetVal_, RetVal_+sizeof(RetVal_)/sizeof(*RetVal_)); + return verify_case(RetVal, SubrectanglesOfTable().getQuantity(table)); } +int Test_(Case_<2>) { + string table_[] = {"TANYA", + "HAPPY", + "BIRTH", + "DAYYY"}; + vector table(table_, table_+sizeof(table_)/sizeof(*table_)); + long RetVal_[] = {5168, 1280, 0, 1120, 0, 0, 0, 2560, 1472, 0, 0, 0, 0, 1344, 0, 3008, 0, 1536, 0, 2592, 0, 0, 0, 0, 6320, 0 }; + vector RetVal(RetVal_, RetVal_+sizeof(RetVal_)/sizeof(*RetVal_)); + return verify_case(RetVal, SubrectanglesOfTable().getQuantity(table)); } + +template void Run_() { cerr << "Test Case #" << N << "..." << flush; Test_(Case_()); Run_()))==1 ? -1 : N+1>(); } +template<> void Run_<-1>() {} +int main() { Run_<0>(); } +// END CUT HERE + ADDED SRM/429/1B.cpp Index: SRM/429/1B.cpp ================================================================== --- SRM/429/1B.cpp +++ SRM/429/1B.cpp @@ -0,0 +1,112 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +using namespace std; +typedef long long LL; + +LL gcd(LL a, LL b) +{ + while(a) swap(a, b%=a); return b; +} +LL lcm(LL a, LL b) +{ + return a/gcd(a,b)*b; +} + +class IngredientProportions +{ +public: + vector getMasses(vector proportions) + { + int N = proportions.size() + 1; + vector a(N-1),b(N-1),p(N-1),q(N-1); + + for(int i=0; i>a[i]; + sin.get();sin.get();sin.get();sin.get();sin.get();sin.get();//" and #" + sin>>b[i]; + sin.get();sin.get();sin.get();sin.get();//" as " + sin>>p[i]; + sin.get(); //: + sin>>q[i]; + } + + vector g(N, -1); g[0]=1; + for(int _=0; _ ans(N); + for(int i=0; i +double start_time;string timer() { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) << " msec)"; return os.str(); } + +template string print_array(const vector &V) { ostringstream os; os << "{ "; for (typename vector::const_iterator iter = V.begin(); iter != V.end(); ++iter) os << '\"' << *iter << "\","; os << " }"; return os.str(); } +int verify_case(const vector &Expected, const vector &Received) { if (Expected == Received) cerr << "PASSED" << timer() << endl; else { cerr << "FAILED" << timer() << endl; cerr << "\tExpected: " << print_array(Expected) << endl; cerr << "\tReceived: " << print_array(Received) << endl; } return 0;} + +template struct Case_ { Case_(){start_time=clock();} }; +char Test_(...); +int Test_(Case_<0>) { + string proportions_[] = {"#0 and #1 as 6:4"}; + vector proportions(proportions_, proportions_+sizeof(proportions_)/sizeof(*proportions_)); + int RetVal_[] = {3, 2 }; + vector RetVal(RetVal_, RetVal_+sizeof(RetVal_)/sizeof(*RetVal_)); + return verify_case(RetVal, IngredientProportions().getMasses(proportions)); } +int Test_(Case_<1>) { + string proportions_[] = {"#0 and #1 as 9:8","#1 and #2 as 9:8"} +; + vector proportions(proportions_, proportions_+sizeof(proportions_)/sizeof(*proportions_)); + int RetVal_[] = {81, 72, 64 }; + vector RetVal(RetVal_, RetVal_+sizeof(RetVal_)/sizeof(*RetVal_)); + return verify_case(RetVal, IngredientProportions().getMasses(proportions)); } +int Test_(Case_<2>) { + string proportions_[] = {"#4 and #0 as 1:1", "#4 and #1 as 3:1", "#4 and #2 as 5:1", "#4 and #3 as 7:1"}; + vector proportions(proportions_, proportions_+sizeof(proportions_)/sizeof(*proportions_)); + int RetVal_[] = {105, 35, 21, 15, 105 }; + vector RetVal(RetVal_, RetVal_+sizeof(RetVal_)/sizeof(*RetVal_)); + return verify_case(RetVal, IngredientProportions().getMasses(proportions)); } +int Test_(Case_<3>) { + string proportions_[] = {"#2 and #3 as 6:8", "#0 and #1 as 9:3", "#3 and #0 as 7:5"}; + vector proportions(proportions_, proportions_+sizeof(proportions_)/sizeof(*proportions_)); + int RetVal_[] = {60, 20, 63, 84 }; + vector RetVal(RetVal_, RetVal_+sizeof(RetVal_)/sizeof(*RetVal_)); + return verify_case(RetVal, IngredientProportions().getMasses(proportions)); } + +template void Run_() { cerr << "Test Case #" << N << "..." << flush; Test_(Case_()); Run_()))==1 ? -1 : N+1>(); } +template<> void Run_<-1>() {} +int main() { Run_<0>(); } +// END CUT HERE + ADDED SRM/429/1C.cpp Index: SRM/429/1C.cpp ================================================================== --- SRM/429/1C.cpp +++ SRM/429/1C.cpp @@ -0,0 +1,173 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +using namespace std; +typedef long long LL; + +class SpecificPolyominoCovering +{ +public: + vector findCovering(vector region) + { + try { + rec(0, 0, region); + } catch( vector& r ) { + return r; + } + return vector(); + } + + bool nextX(int &y, int &x, vector& r) + { + while( r[y][x] != 'X' ) { + ++x; + if( x == r[y].size() ) { + x=0, ++y; + if( y == r.size() ) + return false; + } + } + return true; + } + + bool canFill( vector r ) + { + return canFill_rec(0, 0, r); + } + + bool canFill_rec(int y, int x, vector& r) + { + if( !nextX(y, x, r) ) + return true; + + // if it was the pattern fillable both by B and A, + // XX?X + // XXXX + // then, if we can fill starting from A it implies ?=X + // In this case, we must have been able to fill starting + // from B. Thus, we never need to try A if we've failed + // filling by B. The "lexicographically first" will by + // considered elsewhere. + + if( x+1 < r[y].size() && r[y][x+1]=='X' ) + { + r[y][x] = r[y][x+1] = 'B'; + return canFill_rec(y, x, r); + } + + if( x+3 < r[y].size() && y+1 < r.size() ) + { + int py[] = {0,0,1,1,1,1}; + int px[] = {0,3,0,1,2,3}; + for(int i=0; i<6; ++i) + if( r[y+py[i]][x+px[i]]!='X' ) + return false; + else + r[y+py[i]][x+px[i]] = 'A'; + return canFill_rec(y, x, r); + } + + return false; + } + + void rec(int y, int x, vector& r) + { + if( !nextX(y, x, r) ) + throw r; + + // try to fill by A + if( x+3 < r[y].size() && y+1 < r.size() ) + { + int py[] = {0,0,1,1,1,1}; + int px[] = {0,3,0,1,2,3}; + bool bad = false; + for(int i=0; i<6; ++i) + if( r[y+py[i]][x+px[i]]!='X' ) + bad = true; + if( !bad ) + { + for(int i=0; i<6; ++i) + r[y+py[i]][x+px[i]] = 'A'; + if( canFill(r) ) + rec(y, x, r); + for(int i=0; i<6; ++i) + r[y+py[i]][x+px[i]] = 'X'; + } + } + // try to fill by B + if( x+1 < r[y].size() && r[y][x+1]=='X' ) + { + r[y][x] = r[y][x+1] = 'B'; + rec(y, x, r); + r[y][x] = r[y][x+1] = 'X'; + } + } +}; + +// 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 string print_array(const vector &V) { ostringstream os; os << "{ "; for (typename vector::const_iterator iter = V.begin(); iter != V.end(); ++iter) os << '\"' << *iter << "\","; os << " }"; return os.str(); } +int verify_case(const vector &Expected, const vector &Received) { if (Expected == Received) cerr << "PASSED" << timer() << endl; else { cerr << "FAILED" << timer() << endl; cerr << "\tExpected: " << print_array(Expected) << endl; cerr << "\tReceived: " << print_array(Received) << endl; } return 0;} + +template struct Case_ { Case_(){start_time=clock();} }; +char Test_(...); +int Test_(Case_<0>) { + string region_[] = {"XXXX", + "XXXX"}; + vector region(region_, region_+sizeof(region_)/sizeof(*region_)); + string RetVal_[] = {"ABBA", "AAAA" }; + vector RetVal(RetVal_, RetVal_+sizeof(RetVal_)/sizeof(*RetVal_)); + return verify_case(RetVal, SpecificPolyominoCovering().findCovering(region)); } +int Test_(Case_<1>) { + string region_[] = {"X..XXXX..X", + "XXXX..XXXX"}; + vector region(region_, region_+sizeof(region_)/sizeof(*region_)); + string RetVal_[] = {"A..ABBA..A", "AAAA..AAAA" }; + vector RetVal(RetVal_, RetVal_+sizeof(RetVal_)/sizeof(*RetVal_)); + return verify_case(RetVal, SpecificPolyominoCovering().findCovering(region)); } +int Test_(Case_<2>) { + string region_[] = {"XXXXXX", + "XXXXXX", + "XXXXXX"}; + vector region(region_, region_+sizeof(region_)/sizeof(*region_)); + string RetVal_[] = {"ABBABB", "AAAABB", "BBBBBB" }; + vector RetVal(RetVal_, RetVal_+sizeof(RetVal_)/sizeof(*RetVal_)); + return verify_case(RetVal, SpecificPolyominoCovering().findCovering(region)); } +int Test_(Case_<3>) { + string region_[] = {"X..XX", + "XXXXX"}; + vector region(region_, region_+sizeof(region_)/sizeof(*region_)); + vector RetVal; + return verify_case(RetVal, SpecificPolyominoCovering().findCovering(region)); } +int Test_(Case_<4>) { + string region_[] = {"XXXXXXXXXX", + "XXXXXXXXXX", + "XXXXXXXXXX", + "XXXXX..XXX", + "XXXXXXXXXX", + "XXXXXXXXXX", + "XXXXXXXXXX"}; + vector region(region_, region_+sizeof(region_)/sizeof(*region_)); + string RetVal_[] = {"ABBAABBABB", "AAAAAAAABB", "ABBABBBBBB", "AAAAA..ABB", "ABBAAAAABB", "AAAAABBABB", "BBBBAAAABB" }; + vector RetVal(RetVal_, RetVal_+sizeof(RetVal_)/sizeof(*RetVal_)); + return verify_case(RetVal, SpecificPolyominoCovering().findCovering(region)); } + +template void Run_() { cerr << "Test Case #" << N << "..." << flush; Test_(Case_()); Run_()))==1 ? -1 : N+1>(); } +template<> void Run_<-1>() {} +int main() { Run_<0>(); } +// END CUT HERE + ADDED SRM/430/1A.cpp Index: SRM/430/1A.cpp ================================================================== --- SRM/430/1A.cpp +++ SRM/430/1A.cpp @@ -0,0 +1,66 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +using namespace std; +typedef long long LL; + +class BitwiseEquations +{ +public: + long long kthPlusOrSolution(int x, int k) + { + LL y = 0; + for(LL i=1; k; i<<=1) + if( !(x&i) ) + y |= (k&1)*i, k>>=1; + return y; + } +}; + +// 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 string print_array(const vector &V) { ostringstream os; os << "{ "; for (typename vector::const_iterator iter = V.begin(); iter != V.end(); ++iter) os << '\"' << *iter << "\","; os << " }"; return os.str(); } +int verify_case(const long long &Expected, const long long &Received) { if (Expected == Received) cerr << "PASSED" << timer() << endl; else { cerr << "FAILED" << timer() << endl; cerr << "\tExpected: \"" << Expected << '\"' << endl; cerr << "\tReceived: \"" << Received << '\"' << endl; } return 0;} + +template struct Case_ { Case_(){start_time=clock();} }; +char Test_(...); +int Test_(Case_<0>) { + int x = 5; + int k = 1; + long long RetVal = 2LL; + return verify_case(RetVal, BitwiseEquations().kthPlusOrSolution(x, k)); } +int Test_(Case_<1>) { + int x = 5; + int k = 5; + long long RetVal = 18LL; + return verify_case(RetVal, BitwiseEquations().kthPlusOrSolution(x, k)); } +int Test_(Case_<2>) { + int x = 10; + int k = 3; + long long RetVal = 5LL; + return verify_case(RetVal, BitwiseEquations().kthPlusOrSolution(x, k)); } +int Test_(Case_<3>) { + int x = 1; + int k = 1000000000; + long long RetVal = 2000000000LL; + return verify_case(RetVal, BitwiseEquations().kthPlusOrSolution(x, k)); } + +template void Run_() { cerr << "Test Case #" << N << "..." << flush; Test_(Case_()); Run_()))==1 ? -1 : N+1>(); } +template<> void Run_<-1>() {} +int main() { Run_<0>(); } +// END CUT HERE + ADDED SRM/430/1B.cpp Index: SRM/430/1B.cpp ================================================================== --- SRM/430/1B.cpp +++ SRM/430/1B.cpp @@ -0,0 +1,168 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +using namespace std; +typedef long long LL; + +int INF = 10000000; + +class TwinTowns +{ +public: + vector optimalTwinTowns(vector x, vector y, int maxPartners, int minDistance) + { + int N = x.size(); + int cost[10][10]; + for(int i=0; i v(N, maxPartners); + pair r = rec(0, N, v, cost); + vector ans(2); + ans[0] = -r.first; + ans[1] = r.second; + return ans; + } + + map< vector,pair > memo; + pair rec(int i, int N, vector& v, int cost[10][10]) + { + if( i==N ) + return make_pair(0, 0); + vector key(v.begin()+i, v.end()); + if( memo.count(key) ) + return memo[key]; + + // choose at most v[i] js s.t. cost[i][j]!=INF && v[j]!=0 + pair best = rec(i+1, N, v, cost); + if( v[i] >= 1 ) + { + for(int j1= i+1; j1 a = rec(i+1, N, v, cost); + a.first -= 1; + a.second += cost[i][j1]; + best = min(best, a); + v[j1]++; + } + } + if( v[i] >= 2 ) + { + for(int j1= i+1; j1 a = rec(i+1, N, v, cost); + a.first -= 2; + a.second += cost[i][j1] + cost[i][j2]; + best = min(best, a); + v[j1]++; + v[j2]++; + } + } + if( v[i] >= 3 ) + { + for(int j1= i+1; j1 a = rec(i+1, N, v, cost); + a.first -= 3; + a.second += cost[i][j1] + cost[i][j2] + cost[i][j3]; + best = min(best, a); + v[j1]++; + v[j2]++; + v[j3]++; + } + } + + return memo[key] = best; + } +}; + +// 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 string print_array(const vector &V) { ostringstream os; os << "{ "; for (typename vector::const_iterator iter = V.begin(); iter != V.end(); ++iter) os << '\"' << *iter << "\","; os << " }"; return os.str(); } +int verify_case(const vector &Expected, const vector &Received) { if (Expected == Received) cerr << "PASSED" << timer() << endl; else { cerr << "FAILED" << timer() << endl; cerr << "\tExpected: " << print_array(Expected) << endl; cerr << "\tReceived: " << print_array(Received) << endl; } return 0;} + +template struct Case_ { Case_(){start_time=clock();} }; +char Test_(...); +int Test_(Case_<0>) { + int x_[] = {0,0,10}; + vector x(x_, x_+sizeof(x_)/sizeof(*x_)); + int y_[] = {0,10,4}; + vector y(y_, y_+sizeof(y_)/sizeof(*y_)); + int maxPartners = 1; + int minDistance = 1; + int RetVal_[] = {1, 10 }; + vector RetVal(RetVal_, RetVal_+sizeof(RetVal_)/sizeof(*RetVal_)); + return verify_case(RetVal, TwinTowns().optimalTwinTowns(x, y, maxPartners, minDistance)); } +int Test_(Case_<1>) { + int x_[] = {0,0,10}; + vector x(x_, x_+sizeof(x_)/sizeof(*x_)); + int y_[] = {0,10,4}; + vector y(y_, y_+sizeof(y_)/sizeof(*y_)); + int maxPartners = 1; + int minDistance = 11; + int RetVal_[] = {1, 14 }; + vector RetVal(RetVal_, RetVal_+sizeof(RetVal_)/sizeof(*RetVal_)); + return verify_case(RetVal, TwinTowns().optimalTwinTowns(x, y, maxPartners, minDistance)); } +int Test_(Case_<2>) { + int x_[] = {0,10,0,10}; + vector x(x_, x_+sizeof(x_)/sizeof(*x_)); + int y_[] = {0,0,20,20}; + vector y(y_, y_+sizeof(y_)/sizeof(*y_)); + int maxPartners = 1; + int minDistance = 1; + int RetVal_[] = {2, 20 }; + vector RetVal(RetVal_, RetVal_+sizeof(RetVal_)/sizeof(*RetVal_)); + return verify_case(RetVal, TwinTowns().optimalTwinTowns(x, y, maxPartners, minDistance)); } +int Test_(Case_<3>) { + int x_[] = {0,10,0,10}; + vector x(x_, x_+sizeof(x_)/sizeof(*x_)); + int y_[] = {0,0,20,20}; + vector y(y_, y_+sizeof(y_)/sizeof(*y_)); + int maxPartners = 2; + int minDistance = 10; + int RetVal_[] = {4, 60 }; + vector RetVal(RetVal_, RetVal_+sizeof(RetVal_)/sizeof(*RetVal_)); + return verify_case(RetVal, TwinTowns().optimalTwinTowns(x, y, maxPartners, minDistance)); } +int Test_(Case_<4>) { + int x_[] = {0,0,0,0,0,0,0,0,0}; + vector x(x_, x_+sizeof(x_)/sizeof(*x_)); + int y_[] = {1,2,3,4,5,6,7,8,9}; + vector y(y_, y_+sizeof(y_)/sizeof(*y_)); + int maxPartners = 3; + int minDistance = 6; + int RetVal_[] = {6, 40 }; + vector RetVal(RetVal_, RetVal_+sizeof(RetVal_)/sizeof(*RetVal_)); + return verify_case(RetVal, TwinTowns().optimalTwinTowns(x, y, maxPartners, minDistance)); } + +template void Run_() { cerr << "Test Case #" << N << "..." << flush; Test_(Case_()); Run_()))==1 ? -1 : N+1>(); } +template<> void Run_<-1>() {} +int main() { Run_<0>(); } +// END CUT HERE + ADDED SRM/430/1C.cpp Index: SRM/430/1C.cpp ================================================================== --- SRM/430/1C.cpp +++ SRM/430/1C.cpp @@ -0,0 +1,143 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +using namespace std; +typedef long long LL; +typedef LL BITS; + +class PickingUp +{ +public: + vector fairPickingUp(vector score1, vector score2) + { + LL t = accumulate(score2.begin(), score2.end(), 0LL); + vector s; + transform(score1.begin(), score1.end(), score2.begin(), back_inserter(s), plus()); + + int N = s.size() / 2; + + vector< map > L = try_all( s.begin(), N ); + vector< map > R = try_all( s.begin()+N, N ); + + pair best(0x7fffffffffffffffLL, 0); + for(int i=0; i<=N; ++i) + best = min(best, best_possible( L[i], R[N-i], t, N )); + + vector ans; + for(int i=0; i!=s.size(); ++i) + ans.push_back( (-best.second & (1LL< best_possible( map& L, map& R, LL t, int N ) + { + typedef map::iterator mit; + + pair best(0x7fffffffffffffffLL, 0); + for(mit i=L.begin(); i!=L.end(); ++i) + { + LL as = i->first; + LL am = i->second; + mit j = R.lower_bound(t-as); + if( j!=R.end() ) { + LL bs = j->first; + LL bm = j->second; + best = min(best, make_pair(abs(as+bs-t), -((am<first; + LL bm = j->second; + best = min(best, make_pair(abs(as+bs-t), -((am< + vector< map > try_all( Ite s, int N ) + { + vector< map > a(N+1); + for(BITS m=(1<=0; --m) + { + int nb = 0; + LL sm = 0; + for(int i=0; i +double start_time;string timer() { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) << " msec)"; return os.str(); } + +template string print_array(const vector &V) { ostringstream os; os << "{ "; for (typename vector::const_iterator iter = V.begin(); iter != V.end(); ++iter) os << '\"' << *iter << "\","; os << " }"; return os.str(); } +int verify_case(const vector &Expected, const vector &Received) { if (Expected == Received) cerr << "PASSED" << timer() << endl; else { cerr << "FAILED" << timer() << endl; cerr << "\tExpected: " << print_array(Expected) << endl; cerr << "\tReceived: " << print_array(Received) << endl; } return 0;} + +template struct Case_ { Case_(){start_time=clock();} }; +char Test_(...); +int Test_(Case_<0>) { + long score1_[] = {5,9}; + vector score1(score1_, score1_+sizeof(score1_)/sizeof(*score1_)); + long score2_[] = {8,6}; + vector score2(score2_, score2_+sizeof(score2_)/sizeof(*score2_)); + int RetVal_[] = {1, 2 }; + vector RetVal(RetVal_, RetVal_+sizeof(RetVal_)/sizeof(*RetVal_)); + return verify_case(RetVal, PickingUp().fairPickingUp(score1, score2)); } +int Test_(Case_<1>) { + long score1_[] = {2,3,4,7}; + vector score1(score1_, score1_+sizeof(score1_)/sizeof(*score1_)); + long score2_[] = {2,4,5,8}; + vector score2(score2_, score2_+sizeof(score2_)/sizeof(*score2_)); + int RetVal_[] = {1, 2, 2, 1 }; + vector RetVal(RetVal_, RetVal_+sizeof(RetVal_)/sizeof(*RetVal_)); + return verify_case(RetVal, PickingUp().fairPickingUp(score1, score2)); } +int Test_(Case_<2>) { + long score1_[] = {1,5,6,8}; + vector score1(score1_, score1_+sizeof(score1_)/sizeof(*score1_)); + long score2_[] = {7,5,3,1}; + vector score2(score2_, score2_+sizeof(score2_)/sizeof(*score2_)); + int RetVal_[] = {1, 2, 1, 2 }; + vector RetVal(RetVal_, RetVal_+sizeof(RetVal_)/sizeof(*RetVal_)); + return verify_case(RetVal, PickingUp().fairPickingUp(score1, score2)); } +int Test_(Case_<3>) { + long score1_[] = {300,300,300,300}; + vector score1(score1_, score1_+sizeof(score1_)/sizeof(*score1_)); + long score2_[] = {600,10,10,10}; + vector score2(score2_, score2_+sizeof(score2_)/sizeof(*score2_)); + int RetVal_[] = {2, 1, 1, 2 }; + vector RetVal(RetVal_, RetVal_+sizeof(RetVal_)/sizeof(*RetVal_)); + return verify_case(RetVal, PickingUp().fairPickingUp(score1, score2)); } +int Test_(Case_<4>) { + long score1_[] = {50,50,50,1}; + vector score1(score1_, score1_+sizeof(score1_)/sizeof(*score1_)); + long score2_[] = {30,30,30,150}; + vector score2(score2_, score2_+sizeof(score2_)/sizeof(*score2_)); + int RetVal_[] = {1, 2, 2, 1 }; + vector RetVal(RetVal_, RetVal_+sizeof(RetVal_)/sizeof(*RetVal_)); + return verify_case(RetVal, PickingUp().fairPickingUp(score1, score2)); } + +template void Run_() { cerr << "Test Case #" << N << "..." << flush; Test_(Case_()); Run_()))==1 ? -1 : N+1>(); } +template<> void Run_<-1>() {} +int main() { Run_<0>(); } +// END CUT HERE + ADDED SRM/431/1A.cpp Index: SRM/431/1A.cpp ================================================================== --- SRM/431/1A.cpp +++ SRM/431/1A.cpp @@ -0,0 +1,85 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +using namespace std; +typedef long long LL; + +class LaserShooting +{ +public: + double numberOfHits(vector x, vector y1, vector y2) + { + double ans = 0.0; + for(int i=0; i +double start_time;string timer() { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) << " msec)"; return os.str(); } + +template string print_array(const vector &V) { ostringstream os; os << "{ "; for (typename vector::const_iterator iter = V.begin(); iter != V.end(); ++iter) os << '\"' << *iter << "\","; os << " }"; return os.str(); } +int verify_case(const double &Expected, const double &Received) { double diff = Expected - Received; if (diff < 0) diff = -diff; if (diff < 1e-9) cerr << "PASSED" << timer() << endl; else { cerr << "FAILED" << timer() << endl; cerr << "\tExpected: \"" << Expected << '\"' << endl; cerr << "\tReceived: \"" << Received << '\"' << endl; } return 0;} + +template struct Case_ { Case_(){start_time=clock();} }; +char Test_(...); +int Test_(Case_<0>) { + int x_[] = {1}; + vector x(x_, x_+sizeof(x_)/sizeof(*x_)); + int y1_[] = {-1}; + vector y1(y1_, y1_+sizeof(y1_)/sizeof(*y1_)); + int y2_[] = {1}; + vector y2(y2_, y2_+sizeof(y2_)/sizeof(*y2_)); + double RetVal = 0.5; + return verify_case(RetVal, LaserShooting().numberOfHits(x, y1, y2)); } +int Test_(Case_<1>) { + int x_[] = {1,2}; + vector x(x_, x_+sizeof(x_)/sizeof(*x_)); + int y1_[] = {-1,-2}; + vector y1(y1_, y1_+sizeof(y1_)/sizeof(*y1_)); + int y2_[] = {1,2}; + vector y2(y2_, y2_+sizeof(y2_)/sizeof(*y2_)); + double RetVal = 1.0; + return verify_case(RetVal, LaserShooting().numberOfHits(x, y1, y2)); } +int Test_(Case_<2>) { + int x_[] = {3,4,7,1}; + vector x(x_, x_+sizeof(x_)/sizeof(*x_)); + int y1_[] = {1,2,3,4}; + vector y1(y1_, y1_+sizeof(y1_)/sizeof(*y1_)); + int y2_[] = {4,3,2,1}; + vector y2(y2_, y2_+sizeof(y2_)/sizeof(*y2_)); + double RetVal = 0.4623163952488826; + return verify_case(RetVal, LaserShooting().numberOfHits(x, y1, y2)); } +int Test_(Case_<3>) { + int x_[] = {134,298,151,942}; + vector x(x_, x_+sizeof(x_)/sizeof(*x_)); + int y1_[] = {-753,-76,19,568}; + vector y1(y1_, y1_+sizeof(y1_)/sizeof(*y1_)); + int y2_[] = {440,689,-39,672}; + vector y2(y2_, y2_+sizeof(y2_)/sizeof(*y2_)); + double RetVal = 1.444210260641501; + return verify_case(RetVal, LaserShooting().numberOfHits(x, y1, y2)); } + +template void Run_() { cerr << "Test Case #" << N << "..." << flush; Test_(Case_()); Run_()))==1 ? -1 : N+1>(); } +template<> void Run_<-1>() {} +int main() { Run_<0>(); } +// END CUT HERE + ADDED SRM/431/1B.cpp Index: SRM/431/1B.cpp ================================================================== --- SRM/431/1B.cpp +++ SRM/431/1B.cpp @@ -0,0 +1,89 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +using namespace std; +typedef long long LL; + +static const LL MODVAL = 1000000007; +int memo[10][11][1001][9]; + +class MegaCoolNumbers +{ +public: + int count(int N, int A) + { + memset(memo, -1, sizeof(memo)); + return rec(1, 10, N, A); + } + + int rec(int D, int F, int N, int A) + { + if( A==0 ) return N==0 ? 1 : 0; + if( N < A ) return 0; + if( N==1 ) return 10-D - (F==10 ? 0 : 1); + if( 10-D < A ) return 0; + if( memo[D][F][N][A] != -1 ) + return memo[D][F][N][A]; + + LL ans = 0; + for(int d=D; d<=9; ++d) if(d!=F) + for(int k=2; k<=N; ++k) + for(int q=0; d+(k-1)*q<=9; ++q) + ans += rec(d+(k-1)*q, min(10, d+k*q), N-k, A-1); + return memo[D][F][N][A] = int(ans % MODVAL); + } +}; + +// 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 string print_array(const vector &V) { ostringstream os; os << "{ "; for (typename vector::const_iterator iter = V.begin(); iter != V.end(); ++iter) os << '\"' << *iter << "\","; os << " }"; return os.str(); } +int verify_case(const int &Expected, const int &Received) { if (Expected == Received) cerr << "PASSED" << timer() << endl; else { cerr << "FAILED" << timer() << endl; cerr << "\tExpected: \"" << Expected << '\"' << endl; cerr << "\tReceived: \"" << Received << '\"' << endl; } return 0;} + +template struct Case_ { Case_(){start_time=clock();} }; +char Test_(...); +int Test_(Case_<0>) { + int N = 1; + int A = 1; + int RetVal = 9; + return verify_case(RetVal, MegaCoolNumbers().count(N, A)); } +int Test_(Case_<1>) { + int N = 2; + int A = 1; + int RetVal = 45; + return verify_case(RetVal, MegaCoolNumbers().count(N, A)); } +int Test_(Case_<2>) { + int N = 2; + int A = 2; + int RetVal = 0; + return verify_case(RetVal, MegaCoolNumbers().count(N, A)); } +int Test_(Case_<3>) { + int N = 10; + int A = 3; + int RetVal = 7502; + return verify_case(RetVal, MegaCoolNumbers().count(N, A)); } +int Test_(Case_<4>) { + int N = 1000; + int A = 5; + int RetVal = 7502; + return verify_case(RetVal, MegaCoolNumbers().count(N, A)); } + +template void Run_() { cerr << "Test Case #" << N << "..." << flush; Test_(Case_()); Run_()))==1 ? -1 : N+1>(); } +template<> void Run_<-1>() {} +int main() { Run_<0>(); } +// END CUT HERE + ADDED SRM/431/1C.cpp Index: SRM/431/1C.cpp ================================================================== --- SRM/431/1C.cpp +++ SRM/431/1C.cpp @@ -0,0 +1,198 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +using namespace std; +typedef long long LL; + +template +struct RMQ +{ + vector< vector > rm; + T* d; + int n; + + RMQ( T* d, int n ) : d(d), n(n) { + rm.push_back( vector(n) ); + for(int x=0; x d[rm[k-1][x + (1< all(int L, int R) const { + vector ans; + int minValue = d[(*this)(L, R)]; + while( L <= R ) { + int C = (*this)(L, R); + if( minValue < d[C] ) + break; + ans.push_back(C); + L = C+1; + } + return ans; + } +}; + +int B[2048][2048]; +int U[2048][2048]; +int D[2048][2048]; + +class PerfectRectangles +{ +public: + int numberOfRectangles(int N, int M, int X0, int A, int _B, int Y0, int C, int _D) + { + // input + int x=X0%N, y=Y0%N; + memset(B, 0, sizeof(B)); + for(int i=0; i=0; --y) + for(int x=0; x rmU(U[y], N); + RMQ rmD(D[y], N); + + stack< pair > S; + S.push( make_pair(0, N-1) ); + while( !S.empty() ) { + int L = S.top().first; + int R = S.top().second; + S.pop(); + + int d = rmD(L,R); + if( D[y][d] == 1 ) + ++cnt; + if( D[y][d] <= 1 ) { + vector m = rmU.all(L, R); + m.push_back(R+1); + for(int i=0; i +double start_time;string timer() { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) << " msec)"; return os.str(); } + +template string print_array(const vector &V) { ostringstream os; os << "{ "; for (typename vector::const_iterator iter = V.begin(); iter != V.end(); ++iter) os << '\"' << *iter << "\","; os << " }"; return os.str(); } +int verify_case(const int &Expected, const int &Received) { if (Expected == Received) cerr << "PASSED" << timer() << endl; else { cerr << "FAILED" << timer() << endl; cerr << "\tExpected: \"" << Expected << '\"' << endl; cerr << "\tReceived: \"" << Received << '\"' << endl; } return 0;} + +template struct Case_ { Case_(){start_time=clock();} }; +char Test_(...); +int Test_(Case_<0>) { + int N = 5; + int M = 1; + int X0 = 2; + int A = 0; + int B = 0; + int Y0 = 2; + int C = 0; + int D = 0; + int RetVal = 4; + return verify_case(RetVal, PerfectRectangles().numberOfRectangles(N, M, X0, A, B, Y0, C, D)); } +int Test_(Case_<1>) { + int N = 4; + int M = 4; + int X0 = 0; + int A = 1; + int B = 1; + int Y0 = 0; + int C = 1; + int D = 1; + int RetVal = 6; + return verify_case(RetVal, PerfectRectangles().numberOfRectangles(N, M, X0, A, B, Y0, C, D)); } +int Test_(Case_<2>) { + int N = 1; + int M = 1000000; + int X0 = 1; + int A = 2; + int B = 3; + int Y0 = 1; + int C = 4; + int D = 7; + int RetVal = 0; + return verify_case(RetVal, PerfectRectangles().numberOfRectangles(N, M, X0, A, B, Y0, C, D)); } +int Test_(Case_<3>) { + int N = 10; + int M = 20; + int X0 = 4; + int A = 76; + int B = 2; + int Y0 = 6; + int C = 2; + int D = 43; + int RetVal = 12; + return verify_case(RetVal, PerfectRectangles().numberOfRectangles(N, M, X0, A, B, Y0, C, D)); } +int Test_(Case_<4>) { + int N = 1999; + int M = 4000000; + int X0 = 1000; + int A = 1; + int B = 1; + int Y0 = 0; + int C = 1; + int D = 1; + int RetVal = 1002995; + return verify_case(RetVal, PerfectRectangles().numberOfRectangles(N, M, X0, A, B, Y0, C, D)); } +int Test_(Case_<5>) { + int N = 2000; + int M = 4000000; + int X0 = 1500; + int A = 1; + int B = 1; + int Y0 = 500; + int C = 1; + int D = 1; + int RetVal = 1003997; + return verify_case(RetVal, PerfectRectangles().numberOfRectangles(N, M, X0, A, B, Y0, C, D)); } +template void Run_() { cerr << "Test Case #" << N << "..." << flush; Test_(Case_()); Run_()))==1 ? -1 : N+1>(); } +template<> void Run_<-1>() {} +int main() { Run_<0>(); } +// END CUT HERE + ADDED SRM/432/1A.cpp Index: SRM/432/1A.cpp ================================================================== --- SRM/432/1A.cpp +++ SRM/432/1A.cpp @@ -0,0 +1,121 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +using namespace std; +typedef long long LL; + +class LampsGrid { +public: + int curMax; + + int mostLit(vector initial, int K) + { + // atama-warui + // only meaningful way to flip lights are + // that: exists i {j | initial[i][j]=='0'} + // so at most initial.size() try is sufficient... + + vector s; + for(int j=0; j=0; K-=2) + if( K <= initial[0].size() ) + rec((1LL< s, int i, int K) // exact K flips + { + int c = 0; + for(LL j=1; j<=lit; j<<=1) + if( lit&j ) + ++c; + if( c<=curMax ) + return; + if( i == s.size() ) + { + curMax = c; + return; + } + + if( i+K < s.size() ) // noflip + rec( lit&s[i], s, i+1, K ); + if( K ) // flip + rec( lit&~s[i], s, i+1, K-1 ); + } +}; + +// 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 string print_array(const vector &V) { ostringstream os; os << "{ "; for (typename vector::const_iterator iter = V.begin(); iter != V.end(); ++iter) os << '\"' << *iter << "\","; os << " }"; return os.str(); } +int verify_case(const int &Expected, const int &Received) { if (Expected == Received) cerr << "PASSED" << timer() << endl; else { cerr << "FAILED" << timer() << endl; cerr << "\tExpected: \"" << Expected << '\"' << endl; cerr << "\tReceived: \"" << Received << '\"' << endl; } return 0;} + +template struct Case_ { Case_(){start_time=clock();} }; +char Test_(...); +int Test_(Case_<0>) { + string initial_[] = {"01", + "10", + "10"}; + vector initial(initial_, initial_+sizeof(initial_)/sizeof(*initial_)); + int K = 1; + int RetVal = 2; + return verify_case(RetVal, LampsGrid().mostLit(initial, K)); } +int Test_(Case_<1>) { + string initial_[] = {"101010"}; + vector initial(initial_, initial_+sizeof(initial_)/sizeof(*initial_)); + int K = 2; + int RetVal = 0; + return verify_case(RetVal, LampsGrid().mostLit(initial, K)); } +int Test_(Case_<2>) { + string initial_[] = {"00", "11"}; + vector initial(initial_, initial_+sizeof(initial_)/sizeof(*initial_)); + int K = 999; + int RetVal = 0; + return verify_case(RetVal, LampsGrid().mostLit(initial, K)); } +int Test_(Case_<3>) { + string initial_[] = {"0", "1", "0", "1", "0"} +; + vector initial(initial_, initial_+sizeof(initial_)/sizeof(*initial_)); + int K = 1000; + int RetVal = 2; + return verify_case(RetVal, LampsGrid().mostLit(initial, K)); } +int Test_(Case_<4>) { + string initial_[] = {"001", "101", "001", "000", "111", "001", "101", "111", "110", "000", "111", "010", "110", "001"}; + vector initial(initial_, initial_+sizeof(initial_)/sizeof(*initial_)); + int K = 6; + int RetVal = 4; + return verify_case(RetVal, LampsGrid().mostLit(initial, K)); } +int Test_(Case_<5>) { + string initial_[] = {"01", "10", "01", "01", "10"}; + vector initial(initial_, initial_+sizeof(initial_)/sizeof(*initial_)); + int K = 1; + int RetVal = 3; + return verify_case(RetVal, LampsGrid().mostLit(initial, K)); } + +template void Run_() { cerr << "Test Case #" << N << "..." << flush; Test_(Case_()); Run_()))==1 ? -1 : N+1>(); } +template<> void Run_<-1>() {} +int main() { Run_<0>(); } +// END CUT HERE + ADDED SRM/432/1B.cpp Index: SRM/432/1B.cpp ================================================================== --- SRM/432/1B.cpp +++ SRM/432/1B.cpp @@ -0,0 +1,118 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +using namespace std; +typedef long long LL; + +class GroupedWord { +public: + string restore(vector parts) + { + while( reduce(parts) ) {} + + if( parts.size() == 1 ) + return gd(parts[0]) ? parts[0] : "IMPOSSIBLE"; + + string s = accumulate(parts.begin(), parts.end(), string("")); + return gd(s) ? "MANY" : "IMPOSSIBLE"; + } + + bool reduce( vector& p ) + { + for(int j=0; j +double start_time;string timer() { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) << " msec)"; return os.str(); } + +template string print_array(const vector &V) { ostringstream os; os << "{ "; for (typename vector::const_iterator iter = V.begin(); iter != V.end(); ++iter) os << '\"' << *iter << "\","; os << " }"; return os.str(); } +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;} + +template struct Case_ { Case_(){start_time=clock();} }; +char Test_(...); +int Test_(Case_<0>) { + string parts_[] = {"aaa", "a", "aa"}; + vector parts(parts_, parts_+sizeof(parts_)/sizeof(*parts_)); + string RetVal = "aaaaaa"; + return verify_case(RetVal, GroupedWord().restore(parts)); } +int Test_(Case_<1>) { + string parts_[] = {"ab", "bba"}; + vector parts(parts_, parts_+sizeof(parts_)/sizeof(*parts_)); + string RetVal = "IMPOSSIBLE"; + return verify_case(RetVal, GroupedWord().restore(parts)); } +int Test_(Case_<2>) { + string parts_[] = {"te", "st"}; + vector parts(parts_, parts_+sizeof(parts_)/sizeof(*parts_)); + string RetVal = "stte"; + return verify_case(RetVal, GroupedWord().restore(parts)); } +int Test_(Case_<3>) { + string parts_[] = {"te", "s", "t"}; + vector parts(parts_, parts_+sizeof(parts_)/sizeof(*parts_)); + string RetVal = "MANY"; + return verify_case(RetVal, GroupedWord().restore(parts)); } +int Test_(Case_<4>) { + string parts_[] = {"orr", "rd", "woo", "www"}; + vector parts(parts_, parts_+sizeof(parts_)/sizeof(*parts_)); + string RetVal = "wwwwooorrrd"; + return verify_case(RetVal, GroupedWord().restore(parts)); } +int Test_(Case_<5>) { + string parts_[] = {"abcb"}; + vector parts(parts_, parts_+sizeof(parts_)/sizeof(*parts_)); + string RetVal = "IMPOSSIBLE"; + return verify_case(RetVal, GroupedWord().restore(parts)); } + +template void Run_() { cerr << "Test Case #" << N << "..." << flush; Test_(Case_()); Run_()))==1 ? -1 : N+1>(); } +template<> void Run_<-1>() {} +int main() { Run_<0>(); } +// END CUT HERE + ADDED SRM/432/1C.cpp Index: SRM/432/1C.cpp ================================================================== --- SRM/432/1C.cpp +++ SRM/432/1C.cpp @@ -0,0 +1,169 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +using namespace std; +typedef long long LL; + +struct union_find { + vector p, s; + union_find(int N) : p(N, -1), s(N, 1) {} + + int repr(int u) { + while( p[u]>=0 ) u=p[u]; + return u; + } + bool conn(int u, int v) { + u = repr(u); + v = repr(v); + if( u==v ) return false; + if( s[u] < s[v] ) { p[u]=v; s[v]+=s[u]; } + else { p[v]=u; s[u]+=s[v]; } + return true; + } +}; + +class BuildersCountry { +public: + long long minCost(vector before, vector after, vector houseCost, vector g, int roadCost) + { + int N = before.size(); + + LL baseCost = 0; + + vector< vector > cost(N, vector(N)); + for(int i=0; i!=N; ++i) + for(int j=i+1; j!=N; ++j) +{ LL cc = LL(roadCost)*(before[i]+before[j]); + + int mi = houseCost[i] >& c, vector& g, int N) + { + union_find uf(N); + for(int i=0; i!=N; ++i) + for(int j=i+1; j!=N; ++j) + if( g[i][j]=='Y' ) + uf.conn(i,j); + + typedef pair > cedge; + vector es; + for(int i=0; i!=N; ++i) + for(int j=i+1; j!=N; ++j) + es.push_back( make_pair( c[i][j], make_pair(i,j) ) ); + sort(es.begin(), es.end()); + + LL cc = 0; + for(int i=0; i!=es.size(); ++i) + { + int u = es[i].second.first; + int v = es[i].second.second; + LL c = es[i].first; + if( uf.conn(u,v) ) + cc += c; + } + return cc; + } +}; + +// 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 string print_array(const vector &V) { ostringstream os; os << "{ "; for (typename vector::const_iterator iter = V.begin(); iter != V.end(); ++iter) os << '\"' << *iter << "\","; os << " }"; return os.str(); } +int verify_case(const long long &Expected, const long long &Received) { if (Expected == Received) cerr << "PASSED" << timer() << endl; else { cerr << "FAILED" << timer() << endl; cerr << "\tExpected: \"" << Expected << '\"' << endl; cerr << "\tReceived: \"" << Received << '\"' << endl; } return 0;} + +template struct Case_ { Case_(){start_time=clock();} }; +char Test_(...); +int Test_(Case_<0>) { + int before_[] = {2, 1, 3, 5}; + vector before(before_, before_+sizeof(before_)/sizeof(*before_)); + int after_[] = {2, 1, 3, 5}; + vector after(after_, after_+sizeof(after_)/sizeof(*after_)); + int houseCost_[] = {4, 5, 3, 2}; + vector houseCost(houseCost_, houseCost_+sizeof(houseCost_)/sizeof(*houseCost_)); + string g_[] = {"NNNN", "NNNN", "NNNN", "NNNN"}; + vector g(g_, g_+sizeof(g_)/sizeof(*g_)); + int roadCost = 1000; + long long RetVal = 13000LL; + return verify_case(RetVal, BuildersCountry().minCost(before, after, houseCost, g, roadCost)); } +int Test_(Case_<1>) { + int before_[] = {1, 1, 1, 1}; + vector before(before_, before_+sizeof(before_)/sizeof(*before_)); + int after_[] = {1, 3, 1, 2}; + vector after(after_, after_+sizeof(after_)/sizeof(*after_)); + int houseCost_[] = {8, 5, 3, 2}; + vector houseCost(houseCost_, houseCost_+sizeof(houseCost_)/sizeof(*houseCost_)); + string g_[] = {"NYNN", "YNYN", "NYNY", "NNYN"}; + vector g(g_, g_+sizeof(g_)/sizeof(*g_)); + int roadCost = 100000; + long long RetVal = 39LL; + return verify_case(RetVal, BuildersCountry().minCost(before, after, houseCost, g, roadCost)); } +int Test_(Case_<2>) { + int before_[] = {9, 11}; + vector before(before_, before_+sizeof(before_)/sizeof(*before_)); + int after_[] = {10, 11}; + vector after(after_, after_+sizeof(after_)/sizeof(*after_)); + int houseCost_[] = {5, 1}; + vector houseCost(houseCost_, houseCost_+sizeof(houseCost_)/sizeof(*houseCost_)); + string g_[] = {"NN", "NN"}; + vector g(g_, g_+sizeof(g_)/sizeof(*g_)); + int roadCost = 15; + long long RetVal = 400LL; + return verify_case(RetVal, BuildersCountry().minCost(before, after, houseCost, g, roadCost)); } +int Test_(Case_<3>) { + int before_[] = {1}; + vector before(before_, before_+sizeof(before_)/sizeof(*before_)); + int after_[] = {1000}; + vector after(after_, after_+sizeof(after_)/sizeof(*after_)); + int houseCost_[] = {2}; + vector houseCost(houseCost_, houseCost_+sizeof(houseCost_)/sizeof(*houseCost_)); + string g_[] = {"N"}; + vector g(g_, g_+sizeof(g_)/sizeof(*g_)); + int roadCost = 888; + long long RetVal = 999000LL; + return verify_case(RetVal, BuildersCountry().minCost(before, after, houseCost, g, roadCost)); } +int Test_(Case_<4>) { + int before_[] = {99, 23, 44, 55, 32}; + vector before(before_, before_+sizeof(before_)/sizeof(*before_)); + int after_[] = {99, 23, 44, 55, 32}; + vector after(after_, after_+sizeof(after_)/sizeof(*after_)); + int houseCost_[] = {39, 32, 11, 23, 89}; + vector houseCost(houseCost_, houseCost_+sizeof(houseCost_)/sizeof(*houseCost_)); + string g_[] = {"NYNNN", "YNNNY", "NNNYY", "NNYNY", "NYYYN"}; + vector g(g_, g_+sizeof(g_)/sizeof(*g_)); + int roadCost = 54; + long long RetVal = 0LL; + return verify_case(RetVal, BuildersCountry().minCost(before, after, houseCost, g, roadCost)); } + +template void Run_() { cerr << "Test Case #" << N << "..." << flush; Test_(Case_()); Run_()))==1 ? -1 : N+1>(); } +template<> void Run_<-1>() {} +int main() { Run_<0>(); } +// END CUT HERE + ADDED SRM/433/1A.cpp Index: SRM/433/1A.cpp ================================================================== --- SRM/433/1A.cpp +++ SRM/433/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; + +class MagicWords { +public: + int count(vector S, int K) + { + int cnt = 0; + + int idx[] = {0,1,2,3,4,5,6,7}; + do { + string s; for(int i=0; i +double start_time;string timer() { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) << " msec)"; return os.str(); } + +template string print_array(const vector &V) { ostringstream os; os << "{ "; for (typename vector::const_iterator iter = V.begin(); iter != V.end(); ++iter) os << '\"' << *iter << "\","; os << " }"; return os.str(); } +int verify_case(const int &Expected, const int &Received) { if (Expected == Received) cerr << "PASSED" << timer() << endl; else { cerr << "FAILED" << timer() << endl; cerr << "\tExpected: \"" << Expected << '\"' << endl; cerr << "\tReceived: \"" << Received << '\"' << endl; } return 0;} + +template struct Case_ { Case_(){start_time=clock();} }; +char Test_(...); +int Test_(Case_<0>) { + string S_[] = {"CAD","ABRA","ABRA"}; + vector S(S_, S_+sizeof(S_)/sizeof(*S_)); + int K = 1; + int RetVal = 6; + return verify_case(RetVal, MagicWords().count(S, K)); } +int Test_(Case_<1>) { + string S_[] = {"AB","RAAB","RA"}; + vector S(S_, S_+sizeof(S_)/sizeof(*S_)); + int K = 2; + int RetVal = 3; + return verify_case(RetVal, MagicWords().count(S, K)); } +int Test_(Case_<2>) { + string S_[] = {"AA","AA","AAA","A"}; + vector S(S_, S_+sizeof(S_)/sizeof(*S_)); + int K = 1; + int RetVal = 0; + return verify_case(RetVal, MagicWords().count(S, K)); } +int Test_(Case_<3>) { + string S_[] = {"AA","AA","AAA","A","AAA","AAAA"}; + vector S(S_, S_+sizeof(S_)/sizeof(*S_)); + int K = 15; + int RetVal = 720; + return verify_case(RetVal, MagicWords().count(S, K)); } +int Test_(Case_<4>) { + string S_[] = {"ABC","AB","ABC","CA"}; + vector S(S_, S_+sizeof(S_)/sizeof(*S_)); + int K = 3; + int RetVal = 0; + return verify_case(RetVal, MagicWords().count(S, K)); } +int Test_(Case_<5>) { + string S_[] = {"A","B","C","A","B","C"}; + vector S(S_, S_+sizeof(S_)/sizeof(*S_)); + int K = 1; + int RetVal = 672; + return verify_case(RetVal, MagicWords().count(S, K)); } +int Test_(Case_<6>) { + string S_[] = {"AAAAAAAAAAAAAAAAAAAA", + "AAAAAAAAAAAAAAAAAAAA", + "AAAAAAAAAAAAAAAAAAAA", + "AAAAAAAAAAAAAAAAAAAA", + "AAAAAAAAAAAAAAAAAAAA", + "AAAAAAAAAAAAAAAAAAAA", + "AAAAAAAAAAAAAAAAAAAA", + "AAAAAAAAAAAAAAAAAAAB"}; + vector S(S_, S_+sizeof(S_)/sizeof(*S_)); + int K = 1; + int RetVal = 40320; + return verify_case(RetVal, MagicWords().count(S, K)); } + +template void Run_() { cerr << "Test Case #" << N << "..." << flush; Test_(Case_()); Run_()))==1 ? -1 : N+1>(); } +template<> void Run_<-1>() {} +int main() { Run_<0>(); } +// END CUT HERE + ADDED SRM/433/1B.cpp Index: SRM/433/1B.cpp ================================================================== --- SRM/433/1B.cpp +++ SRM/433/1B.cpp @@ -0,0 +1,88 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +using namespace std; +typedef long long LL; +typedef complex CMP; + +int gcd(int a, int b) { while(a) swap(a,b%=a); return b; } + +class SettingTents { +public: + int countSites(int N, int M) + { + int cnt = 0; + for(int w=1; w<=N; ++w) // for each + for(int h=1; h<=M; ++h) // bounding box... + { + int numWH = (N+1-w)*(M+1-h); + + // w/2 - k(h-2j) = x + // h/2 + kw = y + for(int j=1; j +double start_time;string timer() { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) << " msec)"; return os.str(); } + +template string print_array(const vector &V) { ostringstream os; os << "{ "; for (typename vector::const_iterator iter = V.begin(); iter != V.end(); ++iter) os << '\"' << *iter << "\","; os << " }"; return os.str(); } +int verify_case(const int &Expected, const int &Received) { if (Expected == Received) cerr << "PASSED" << timer() << endl; else { cerr << "FAILED" << timer() << endl; cerr << "\tExpected: \"" << Expected << '\"' << endl; cerr << "\tReceived: \"" << Received << '\"' << endl; } return 0;} + +template struct Case_ { Case_(){start_time=clock();} }; +char Test_(...); +int Test_(Case_<0>) { + int N = 2; + int M = 2; + int RetVal = 6; + return verify_case(RetVal, SettingTents().countSites(N, M)); } +int Test_(Case_<1>) { + int N = 1; + int M = 6; + int RetVal = 6; + return verify_case(RetVal, SettingTents().countSites(N, M)); } +int Test_(Case_<2>) { + int N = 6; + int M = 8; + int RetVal = 527; + return verify_case(RetVal, SettingTents().countSites(N, M)); } + +template void Run_() { cerr << "Test Case #" << N << "..." << flush; Test_(Case_()); Run_()))==1 ? -1 : N+1>(); } +template<> void Run_<-1>() {} +int main() { Run_<0>(); } +// END CUT HERE + ADDED SRM/433/1C.cpp Index: SRM/433/1C.cpp ================================================================== --- SRM/433/1C.cpp +++ SRM/433/1C.cpp @@ -0,0 +1,231 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +using namespace std; +typedef long long LL; +typedef complex CMP; + +static const LL SHIFT = 1000000000LL; +static const LL INF = 1000001LL; +static const LL MAX = 1000000LL; +static const int NV = 5002; +typedef LL flow; +typedef int vert; +typedef vert edge; +typedef vector edges; +typedef vector graph; +//typedef flow flow_graph[NV][NV]; +typedef map< vert,map > flow_graph; + +flow dinic_dfs( graph& G, flow_graph& F, vert v, vert D, + int LV[], flow flow_in, int blocked[] ) +{ + flow flow_out = 0; + for(int i=0; i!=G[v].size(); ++i) { + int u = G[v][i]; + if( LV[v]+1==LV[u] && F[v][u] ) { + flow f = min(flow_in-flow_out, F[v][u]); + if( u==D || !blocked[u] && (f=dinic_dfs(G,F,u,D,LV,f,blocked)) ) { + F[v][u] -= f; + F[u][v] += f; + flow_out += f; + if( flow_in == flow_out ) return flow_out; + } + } + } + blocked[v] = (flow_out==0); + return flow_out; +} + +flow maxFlow( graph& G, flow_graph& F, vert S, vert D ) +{ + for( flow total=0 ;; ) { + int LV[NV] = {0}; + vector Q(1, S); + for(int lv=1; !Q.empty(); ++lv) { + vector Q2; + for(int i=0; i!=Q.size(); ++i) { + edges& ne = G[Q[i]]; + for(int j=0; j!=ne.size(); ++j) + if( F[Q[i]][ne[j]] && !LV[ne[j]] && ne[j]!=S ) + LV[ne[j]]=lv, Q2.push_back(ne[j]); + } + Q.swap(Q2); + } + + if( !LV[D] ) + return total; + + int blocked[NV] = {}; + total += dinic_dfs( G, F, S, D, LV, 0x7fffffff, blocked ); + } +} + +flow_graph F; +class BarbarianInvasion { +public: + int minimalDetachment(vector countryMap, vector detachmentSize) + { + int H = countryMap.size(); + int W = countryMap[0].size(); + vert OUT = 0, CAP = 1; + + graph G(H*W*2+2); + flow_graph F; + + for(int y=0; yA->B==> + vert CUR_B = countryMap[y][x] == '*' ? CAP : (y*W+x)*2+3; + if( countryMap[y][x] != '*' && countryMap[y][x] != '-' ) { + G[CUR_A].push_back(CUR_B); + G[CUR_B].push_back(CUR_A); + F[CUR_A][CUR_B] = SHIFT + detachmentSize[countryMap[y][x]-'A']; + F[CUR_B][CUR_A] = 0; + } + + int DY[] = {-1,+1,0,0}, DX[]={0,0,-1,+1}; + for(int d=0; d<4; ++d) + { + int dy = DY[d], dx = DX[d]; + if( 0<=y+dy && y+dy +double start_time;string timer() { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) << " msec)"; return os.str(); } + +template string print_array(const vector &V) { ostringstream os; os << "{ "; for (typename vector::const_iterator iter = V.begin(); iter != V.end(); ++iter) os << '\"' << *iter << "\","; os << " }"; return os.str(); } +int verify_case(const int &Expected, const int &Received) { if (Expected == Received) cerr << "PASSED" << timer() << endl; else { cerr << "FAILED" << timer() << endl; cerr << "\tExpected: \"" << Expected << '\"' << endl; cerr << "\tReceived: \"" << Received << '\"' << endl; } return 0;} + +template struct Case_ { Case_(){start_time=clock();} }; +char Test_(...); +int Test_(Case_<0>) { + string countryMap_[] = {"ABA", + "A*A", + "AAA"}; + vector countryMap(countryMap_, countryMap_+sizeof(countryMap_)/sizeof(*countryMap_)); + int detachmentSize_[] = {1,2,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1}; + vector detachmentSize(detachmentSize_, detachmentSize_+sizeof(detachmentSize_)/sizeof(*detachmentSize_)); + int RetVal = 5; + return verify_case(RetVal, BarbarianInvasion().minimalDetachment(countryMap, detachmentSize)); } +int Test_(Case_<1>) { + string countryMap_[] = {"CCCC", + "-BAC", + "-*AC", + "--AC"}; + vector countryMap(countryMap_, countryMap_+sizeof(countryMap_)/sizeof(*countryMap_)); + int detachmentSize_[] = {5,20,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1}; + vector detachmentSize(detachmentSize_, detachmentSize_+sizeof(detachmentSize_)/sizeof(*detachmentSize_)); + int RetVal = 25; + return verify_case(RetVal, BarbarianInvasion().minimalDetachment(countryMap, detachmentSize)); } +int Test_(Case_<2>) { + string countryMap_[] = {"A----A", + "-AAAA-", + "-AA*A-", + "-AAAA-", + "A----A"}; + vector countryMap(countryMap_, countryMap_+sizeof(countryMap_)/sizeof(*countryMap_)); + int detachmentSize_[] = {9,8,2,5,3,2,1,2,6,10,4,6,7,1,7,8,8,8,2,9,7,6,5,1,5,10}; + vector detachmentSize(detachmentSize_, detachmentSize_+sizeof(detachmentSize_)/sizeof(*detachmentSize_)); + int RetVal = 0; + return verify_case(RetVal, BarbarianInvasion().minimalDetachment(countryMap, detachmentSize)); } +int Test_(Case_<3>) { + string countryMap_[] = {"-A-----", + "-BCCC*-", + "-A-----"}; + vector countryMap(countryMap_, countryMap_+sizeof(countryMap_)/sizeof(*countryMap_)); + int detachmentSize_[] = {1,5,10,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1}; + vector detachmentSize(detachmentSize_, detachmentSize_+sizeof(detachmentSize_)/sizeof(*detachmentSize_)); + int RetVal = 5; + return verify_case(RetVal, BarbarianInvasion().minimalDetachment(countryMap, detachmentSize)); } +int Test_(Case_<4>) { + string countryMap_[] = {"WANNABRUTEFORCEMEHUH", + "ASUDQWNHIOCASFIUQISA", + "UWQD-ASFFC-AJSQOOWE-", + "-----*Y--AVSSFIUQISA", + "UWQD-ASFFC-AJSQOOWE-", + "JUFDIFD-CHBVISBOOWE-", + "WANNABRUTEFORCEMEHUH"}; + vector countryMap(countryMap_, countryMap_+sizeof(countryMap_)/sizeof(*countryMap_)); + int detachmentSize_[] = {87,78,20,43,30,12,9,18,57,93,32,60,64,9,69,74,74,78,12,81,63,57,48,8,44,95}; + vector detachmentSize(detachmentSize_, detachmentSize_+sizeof(detachmentSize_)/sizeof(*detachmentSize_)); + int RetVal = 218; + return verify_case(RetVal, BarbarianInvasion().minimalDetachment(countryMap, detachmentSize)); } +int Test_(Case_<5>) { + string countryMap_[] = { + "WOMP-PVE-LV-NB-J---E", + "K--MKNWSOLFKK-JHP--W", + "BE-K--MPP-YGZI-DMVKG", + "JGWRXQ-UYEROYGC--SZR", + "KFYN--CRX*N--JS-M-S-", + "TBFGG-OL--D-DY--RR-A", + "AMJT-NV--P--XF-YUSBM", + "HJ-C--KHU-KDZHERUUHM", + "-FGQZOI-A--ZIPB--QOT", + "PNLI-RWYZG-MNV-SUNJD", + "YJ-EJLD-H---K-HSVZF-", + "LKPGZGF-IQ-ETYQJ-VAR", + "UGS--V-BOUGG-SMO--Q-", + "DFVWRHC-D-XES-MEQMCQ", + "L-PUXR-C-HL-WX-NFJJR", + "-A-JLUQX--VWPFENHJZ-", + "HNG-NQAF-VVCUU-WU-HL"}; + vector countryMap(countryMap_, countryMap_+sizeof(countryMap_)/sizeof(*countryMap_)); + int detachmentSize_[] = { +96882, 90320, 19358, 82732, 80637, //ABCDE +88638, 43236, 34294, 49732, 86156, //FGHIJ + 1186, 84308, 49312, 92789, 49312, //KLMNO +14324, 27420, 32881, 73865, 91069, //PQRST + 7266, 60052, 98451, 84652, 3991, 58948}; //UVWXYZ + vector detachmentSize(detachmentSize_, detachmentSize_+sizeof(detachmentSize_)/sizeof(*detachmentSize_)); + int RetVal = 69753; + return verify_case(RetVal, BarbarianInvasion().minimalDetachment(countryMap, detachmentSize)); } + +template void Run_() { cerr << "Test Case #" << N << "..." << flush; Test_(Case_()); Run_()))==1 ? -1 : N+1>(); } +template<> void Run_<-1>() {} +int main() { Run_<0>(); } +// END CUT HERE + ADDED SRM/434-U/1A.cpp Index: SRM/434-U/1A.cpp ================================================================== --- SRM/434-U/1A.cpp +++ SRM/434-U/1A.cpp @@ -0,0 +1,134 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +using namespace std; +typedef long long LL; + +bool is_ps( LL v ) +{ + LL sv = (LL)sqrt( double(v) ); + return (sv-1)*(sv-1) == v + || sv*sv == v + || (sv+1)*(sv+1) == v; +} + +class FindingSquareInTable { +public: + int findMaximalSquare(vector table) + { + LL ans = -1; + + int Y = table.size(); + int X = table[0].size(); + 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 string print_array(const vector &V) { ostringstream os; os << "{ "; for (typename vector::const_iterator iter = V.begin(); iter != V.end(); ++iter) os << '\"' << *iter << "\","; os << " }"; return os.str(); } +int verify_case(const int &Expected, const int &Received) { if (Expected == Received) cerr << "PASSED" << timer() << endl; else { cerr << "FAILED" << timer() << endl; cerr << "\tExpected: \"" << Expected << '\"' << endl; cerr << "\tReceived: \"" << Received << '\"' << endl; } return 0;} + +template struct Case_ { Case_(){start_time=clock();} }; +char Test_(...); +int Test_(Case_<0>) { + string table_[] = {"123", + "456"}; + vector table(table_, table_+sizeof(table_)/sizeof(*table_)); + int RetVal = 64; + return verify_case(RetVal, FindingSquareInTable().findMaximalSquare(table)); } +int Test_(Case_<1>) { + string table_[] = {"00000", + "00000", + "00200", + "00000", + "00000"}; + vector table(table_, table_+sizeof(table_)/sizeof(*table_)); + int RetVal = 0; + return verify_case(RetVal, FindingSquareInTable().findMaximalSquare(table)); } +int Test_(Case_<2>) { + string table_[] = {"3791178", + "1283252", + "4103617", + "8233494", + "8725572", + "2937261"}; + vector table(table_, table_+sizeof(table_)/sizeof(*table_)); + int RetVal = 320356; + return verify_case(RetVal, FindingSquareInTable().findMaximalSquare(table)); } +int Test_(Case_<3>) { + string table_[] = {"135791357", + "357913579", + "579135791", + "791357913", + "913579135"} +; + vector table(table_, table_+sizeof(table_)/sizeof(*table_)); + int RetVal = 9; + return verify_case(RetVal, FindingSquareInTable().findMaximalSquare(table)); } +int Test_(Case_<4>) { + string table_[] = {"553333733", + "775337775", + "777537775", + "777357333", + "755553557", + "355533335", + "373773573", + "337373777", + "775557777"}; + vector table(table_, table_+sizeof(table_)/sizeof(*table_)); + int RetVal = -1; + return verify_case(RetVal, FindingSquareInTable().findMaximalSquare(table)); } +int Test_(Case_<5>) { + string table_[] = {"257240281", + "197510846", + "014345401", + "035562575", + "974935632", + "865865933", + "684684987", + "768934659", + "287493867"}; + vector table(table_, table_+sizeof(table_)/sizeof(*table_)); + int RetVal = 95481; + return verify_case(RetVal, FindingSquareInTable().findMaximalSquare(table)); } + +template void Run_() { cerr << "Test Case #" << N << "..." << flush; Test_(Case_()); Run_()))==1 ? -1 : N+1>(); } +template<> void Run_<-1>() {} +int main() { Run_<0>(); } +// END CUT HERE + ADDED SRM/434-U/1B.cpp Index: SRM/434-U/1B.cpp ================================================================== --- SRM/434-U/1B.cpp +++ SRM/434-U/1B.cpp @@ -0,0 +1,135 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +using namespace std; +typedef long long LL; + +static const int N = 60; + +typedef vector Num; +Num conv( const string& s ) +{ + Num n(N); + for(int i=0; i numbers, int k) + { + vector ns; + transform( numbers.begin(), numbers.end(), back_inserter(ns), &conv ); + + vector gain(36); + for(int d=0; d<36; ++d) + { + vector g(N); + for(int i=0; i +double start_time;string timer() { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) << " msec)"; return os.str(); } + +template string print_array(const vector &V) { ostringstream os; os << "{ "; for (typename vector::const_iterator iter = V.begin(); iter != V.end(); ++iter) os << '\"' << *iter << "\","; os << " }"; return os.str(); } +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;} + +template struct Case_ { Case_(){start_time=clock();} }; +char Test_(...); +int Test_(Case_<0>) { + string numbers_[] = {"HELLO"}; + vector numbers(numbers_, numbers_+sizeof(numbers_)/sizeof(*numbers_)); + int k = 2; + string RetVal = "ZZLLO"; + return verify_case(RetVal, HexatridecimalSum().maximizeSum(numbers, k)); } +int Test_(Case_<1>) { + string numbers_[] = {"500", "POINTS", "FOR", "THIS", "PROBLEM"}; + vector numbers(numbers_, numbers_+sizeof(numbers_)/sizeof(*numbers_)); + int k = 5; + string RetVal = "1100TC85"; + return verify_case(RetVal, HexatridecimalSum().maximizeSum(numbers, k)); } +int Test_(Case_<2>) { + string numbers_[] = {"TO", "BE", "OR", "NOT", "TO", "BE"}; + vector numbers(numbers_, numbers_+sizeof(numbers_)/sizeof(*numbers_)); + int k = 0; + string RetVal = "QNO"; + return verify_case(RetVal, HexatridecimalSum().maximizeSum(numbers, k)); } +int Test_(Case_<3>) { + string numbers_[] = {"KEQUALS36"}; + vector numbers(numbers_, numbers_+sizeof(numbers_)/sizeof(*numbers_)); + int k = 36; + string RetVal = "ZZZZZZZZZ"; + return verify_case(RetVal, HexatridecimalSum().maximizeSum(numbers, k)); } +int Test_(Case_<4>) { + string numbers_[] = {"GOOD", "LUCK", "AND", "HAVE", "FUN"}; + vector numbers(numbers_, numbers_+sizeof(numbers_)/sizeof(*numbers_)); + int k = 7; + string RetVal = "31YUB"; + return verify_case(RetVal, HexatridecimalSum().maximizeSum(numbers, k)); } + +template void Run_() { cerr << "Test Case #" << N << "..." << flush; Test_(Case_()); Run_()))==1 ? -1 : N+1>(); } +template<> void Run_<-1>() {} +int main() { Run_<0>(); } +// END CUT HERE + ADDED SRM/434-U/1C-U.cpp Index: SRM/434-U/1C-U.cpp ================================================================== --- SRM/434-U/1C-U.cpp +++ SRM/434-U/1C-U.cpp @@ -0,0 +1,154 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +using namespace std; +typedef long long LL; + +class IncreasingLists { +public: + string ans; + typedef pair > key; + map memo; + + vector cm; + + string makeIncreasingList(string mask) + { + string buf(mask); + comma(mask, 0, 1, 0, buf); + + for(int i=0; i cur ) + return; + if( i==mask.size() ) { + cm.push_back(buf); + return; + } + if( mask[i]==',' ) { + buf[i] = ','; + comma( mask, i+1, cur, 0, buf ); + } + else if( mask[i]=='?' ) { + if( prev<=cur ) { + buf[i]=','; + comma( mask, i+1, cur, 0, buf ); + } + buf[i]='?'; + comma( mask, i+1, prev, cur+1, buf ); + } + else { + buf[i] = mask[i]; + comma( mask, i+1, prev, cur+1, buf ); + } + } + + bool rec(const string& mask, int i, const string& prev, const string& cur, string& buf) + { + key k(i, make_pair(prev,cur)); + if( memo.count(k) ) + return false; + if( i==mask.size() || mask[i]==',' ) + { + if( prev.size() > cur.size() + || prev.size() == cur.size() && prev>=cur ) + return false; + } + + if( i==mask.size() ) { + ans = buf; + return true; + } + + if( mask[i]==',' ) + return rec(mask, i+1, cur, "", buf); + + if( '0' <= mask[i] && mask[i] <='9' ) + return rec(mask, i+1, prev, cur+mask[i], buf); + + // try , + if( !(prev.size()>cur.size() || prev.size()==cur.size() && prev>=cur) ) { + buf[i] = ','; + if( rec(mask, i+1, cur, "", buf) ) + return true; + } + // try 0 + for(int d=0; d<9; ++d) + if(cur.size()>0 || d>0) { + buf[i] = char(d+'0'); + if( rec(mask, i+1, prev, cur+char(d+'0'), buf) ) + return true; + } + return memo[k] = false; + } +}; + +// 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 string print_array(const vector &V) { ostringstream os; os << "{ "; for (typename vector::const_iterator iter = V.begin(); iter != V.end(); ++iter) os << '\"' << *iter << "\","; os << " }"; return os.str(); } +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;} + +template struct Case_ { Case_(){start_time=clock();} }; +char Test_(...); +int Test_(Case_<0>) { + string mask = "??"; + string RetVal = "10"; + return verify_case(RetVal, IncreasingLists().makeIncreasingList(mask)); } +int Test_(Case_<1>) { + string mask = "???"; + string RetVal = "1,2"; + return verify_case(RetVal, IncreasingLists().makeIncreasingList(mask)); } +int Test_(Case_<2>) { + string mask = "?????????,9"; + string RetVal = "1,2,3,4,5,9"; + return verify_case(RetVal, IncreasingLists().makeIncreasingList(mask)); } +int Test_(Case_<3>) { + string mask = "??????????,9"; + string RetVal = "impossible"; + return verify_case(RetVal, IncreasingLists().makeIncreasingList(mask)); } +int Test_(Case_<4>) { + string mask = "?,10,?????????????????,16,??"; + string RetVal = "impossible"; + return verify_case(RetVal, IncreasingLists().makeIncreasingList(mask)); } +int Test_(Case_<5>) { + string mask = "?2?5??7?,??"; + string RetVal = "12,50,70,71"; + return verify_case(RetVal, IncreasingLists().makeIncreasingList(mask)); } +int Test_(Case_<6>) { + string mask = "???????????????????????????????,???"; + string RetVal = "1,10,11,100,101,102,103,104,105,106"; + return verify_case(RetVal, IncreasingLists().makeIncreasingList(mask)); } +int Test_(Case_<7>) { + string mask = "??????????????????????????????????????????????????"; + string RetVal = "1,10,100,1000,10000,100000,1000000,100000000000000"; + return verify_case(RetVal, IncreasingLists().makeIncreasingList(mask)); } + +template void Run_() { cerr << "Test Case #" << N << "..." << flush; Test_(Case_()); Run_()))==1 ? -1 : N+1>(); } +template<> void Run_<-1>() {} +int main() { Run_<0>(); } +// END CUT HERE + ADDED SRM/435/1A.cpp Index: SRM/435/1A.cpp ================================================================== --- SRM/435/1A.cpp +++ SRM/435/1A.cpp @@ -0,0 +1,85 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +using namespace std; +typedef long long LL; + +class CellRemoval { +public: + int cellsLeft(vector parent, int deletedCell) + { + vector dead(parent.size(), false); + + for(int i=0; i +double start_time;string timer() { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) << " msec)"; return os.str(); } + +template string print_array(const vector &V) { ostringstream os; os << "{ "; for (typename vector::const_iterator iter = V.begin(); iter != V.end(); ++iter) os << '\"' << *iter << "\","; os << " }"; return os.str(); } +int verify_case(const int &Expected, const int &Received) { if (Expected == Received) cerr << "PASSED" << timer() << endl; else { cerr << "FAILED" << timer() << endl; cerr << "\tExpected: \"" << Expected << '\"' << endl; cerr << "\tReceived: \"" << Received << '\"' << endl; } return 0;} + +template struct Case_ { Case_(){start_time=clock();} }; +char Test_(...); +int Test_(Case_<0>) { + int parent_[] = {-1,0,0,1,1}; + vector parent(parent_, parent_+sizeof(parent_)/sizeof(*parent_)); + int deletedCell = 2; + int RetVal = 2; + return verify_case(RetVal, CellRemoval().cellsLeft(parent, deletedCell)); } +int Test_(Case_<1>) { + int parent_[] = {-1,0,0,1,1}; + vector parent(parent_, parent_+sizeof(parent_)/sizeof(*parent_)); + int deletedCell = 1; + int RetVal = 1; + return verify_case(RetVal, CellRemoval().cellsLeft(parent, deletedCell)); } +int Test_(Case_<2>) { + int parent_[] = {-1,0,0,1,1}; + vector parent(parent_, parent_+sizeof(parent_)/sizeof(*parent_)); + int deletedCell = 0; + int RetVal = 0; + return verify_case(RetVal, CellRemoval().cellsLeft(parent, deletedCell)); } +int Test_(Case_<3>) { + int parent_[] = {-1,0,0,2,2,4,4,6,6}; + vector parent(parent_, parent_+sizeof(parent_)/sizeof(*parent_)); + int deletedCell = 4; + int RetVal = 2; + return verify_case(RetVal, CellRemoval().cellsLeft(parent, deletedCell)); } +int Test_(Case_<4>) { + int parent_[] = {26,2,32,36,40,19,43,24,30,13,21,14,24,21,19,4,30,10,44,12,7,32,17,43, + 35,18,7,36,10,16,5,38,35,4,13,-1,16,26,1,12,2,5,18,40,1,17,38,44,14}; + vector parent(parent_, parent_+sizeof(parent_)/sizeof(*parent_)); + int deletedCell = 24; + int RetVal = 14; + return verify_case(RetVal, CellRemoval().cellsLeft(parent, deletedCell)); } + +template void Run_() { cerr << "Test Case #" << N << "..." << flush; Test_(Case_()); Run_()))==1 ? -1 : N+1>(); } +template<> void Run_<-1>() {} +int main() { Run_<0>(); } +// END CUT HERE + ADDED SRM/435/1B.cpp Index: SRM/435/1B.cpp ================================================================== --- SRM/435/1B.cpp +++ SRM/435/1B.cpp @@ -0,0 +1,122 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +using namespace std; +typedef long long LL; + +static const LL MODVAL = 1000000007; + +class DNADeletion { +public: + int differentProteins(vector DNASequence, vector codonTable) + { + string dna; + for(int i=0; i cL, cR; + for(int i=0; i>s>>t; + cL.push_back(s); + cR.push_back(t); + } + int M = cL.size(); + + + vector dp(N+1); dp[0] = 1; + for(int i=0; i p(M); + + set done; + for(int j=i; j +double start_time;string timer() { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) << " msec)"; return os.str(); } + +template string print_array(const vector &V) { ostringstream os; os << "{ "; for (typename vector::const_iterator iter = V.begin(); iter != V.end(); ++iter) os << '\"' << *iter << "\","; os << " }"; return os.str(); } +int verify_case(const int &Expected, const int &Received) { if (Expected == Received) cerr << "PASSED" << timer() << endl; else { cerr << "FAILED" << timer() << endl; cerr << "\tExpected: \"" << Expected << '\"' << endl; cerr << "\tReceived: \"" << Received << '\"' << endl; } return 0;} + +template struct Case_ { Case_(){start_time=clock();} }; +char Test_(...); +int Test_(Case_<0>) { + string DNASequence_[] = {"ACTG"}; + vector DNASequence(DNASequence_, DNASequence_+sizeof(DNASequence_)/sizeof(*DNASequence_)); + string codonTable_[] = {"ACT gua", "ACG cys", "ATG leu", "CTG thr"}; + vector codonTable(codonTable_, codonTable_+sizeof(codonTable_)/sizeof(*codonTable_)); + int RetVal = 4; + return verify_case(RetVal, DNADeletion().differentProteins(DNASequence, codonTable)); } +int Test_(Case_<1>) { + string DNASequence_[] = {"AAACCC"}; + vector DNASequence(DNASequence_, DNASequence_+sizeof(DNASequence_)/sizeof(*DNASequence_)); + string codonTable_[] = {"AAA thr", "CCC cys"}; + vector codonTable(codonTable_, codonTable_+sizeof(codonTable_)/sizeof(*codonTable_)); + int RetVal = 3; + return verify_case(RetVal, DNADeletion().differentProteins(DNASequence, codonTable)); } +int Test_(Case_<2>) { + string DNASequence_[] = {"AAATCCC"}; + vector DNASequence(DNASequence_, DNASequence_+sizeof(DNASequence_)/sizeof(*DNASequence_)); + string codonTable_[] = {"AAA gua","TCC dop","AAT dop","CCC gua"}; + vector codonTable(codonTable_, codonTable_+sizeof(codonTable_)/sizeof(*codonTable_)); + int RetVal = 5; + return verify_case(RetVal, DNADeletion().differentProteins(DNASequence, codonTable)); } +int Test_(Case_<3>) { + string DNASequence_[] = {"ATGCGCATTAACCTCCTACCATGGAAGGGACGTAACCCGGCAATTTGATC", + "CTGATGACGGCATAAGCTACCCCTAGAGGTAAAAATGCATACTGCGTGCT", + "ATGCAG"}; + vector DNASequence(DNASequence_, DNASequence_+sizeof(DNASequence_)/sizeof(*DNASequence_)); + string codonTable_[] = {"AAC RpjZt","AAT ZeiC","GCA ChZwh","TCC RpjZt","GAA I", + "TAG ZeiC","CTG dVK","GAG ZeiC","GTG I","AAG q","ATT dVK", + "AGA cJEjM","GGG KONUd","GTC ZRV","GGC ZeiC","TTA KONUd", + "GAC q","CCA q","GCC ZRV","GCG RpjZt","CCT ZRV","ATG dVK", + "ATC ChZwh","CTC cJEjM","CCC q","ATA dWjz","TTG DkEG", + "CAG q","CAA ZRV","ACT dVK","TCG dVK","ACC I","CGC dVK"}; + vector codonTable(codonTable_, codonTable_+sizeof(codonTable_)/sizeof(*codonTable_)); + int RetVal = 455985264; + return verify_case(RetVal, DNADeletion().differentProteins(DNASequence, codonTable)); } +int Test_(Case_<4>) { + string DNASequence_[] = {"AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"}; + vector DNASequence(DNASequence_, DNASequence_+sizeof(DNASequence_)/sizeof(*DNASequence_)); + string codonTable_[] = {"AAA thr", "CCC cys", "CCC cys", "CCC cys", "CCC cys","AAA thr", "CCC cys", "CCC cys", "CCC cys", "CCC cys","AAA thr", "CCC cys", "CCC cys", "CCC cys", "CCC cys","AAA thr", "CCC cys", "CCC cys", "CCC cys", "CCC cys","AAA thr", "CCC cys", "CCC cys", "CCC cys", "CCC cys","AAA thr", "CCC cys", "CCC cys", "CCC cys", "CCC cys","AAA thr", "CCC cys", "CCC cys", "CCC cys", "CCC cys","AAA thr", "CCC cys", "CCC cys", "CCC cys", "CCC cys","AAA thr", "CCC cys", "CCC cys", "CCC cys", "CCC cys","AAA thr", "CCC cys", "CCC cys", "CCC cys", "CCC cys"}; + vector codonTable(codonTable_, codonTable_+sizeof(codonTable_)/sizeof(*codonTable_)); + int RetVal = 3; + return verify_case(RetVal, DNADeletion().differentProteins(DNASequence, codonTable)); } + +template void Run_() { cerr << "Test Case #" << N << "..." << flush; Test_(Case_()); Run_()))==1 ? -1 : N+1>(); } +template<> void Run_<-1>() {} +int main() { Run_<0>(); } +// END CUT HERE + ADDED SRM/435/1C.cpp Index: SRM/435/1C.cpp ================================================================== --- SRM/435/1C.cpp +++ SRM/435/1C.cpp @@ -0,0 +1,192 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +using namespace std; +typedef long long LL; +typedef complex CMP; + +typedef int vert; +typedef vert edge; +typedef vector edges; +typedef vector graph; + +bool augment( graph& G, int v, vector& matchTo, bool visited[] ) +{ + for(int i=0; i +int biMatch( graph& G, int L ) // [0,L):left, [L,?):right + // only left->right edges are used during computation +{ + vector matchTo(G.size(), -1); + int ans = 0; + for(vert v=0; v hasManaged) + { + int N = hasManaged.size(); + + bool hm[50][50], hmStar[50][50]; + for(int i=0; i cs; + vector pi; + + for(int c=0; c(g, N); + } +}; + +// 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 string print_array(const vector &V) { ostringstream os; os << "{ "; for (typename vector::const_iterator iter = V.begin(); iter != V.end(); ++iter) os << '\"' << *iter << "\","; os << " }"; return os.str(); } +int verify_case(const int &Expected, const int &Received) { if (Expected == Received) cerr << "PASSED" << timer() << endl; else { cerr << "FAILED" << timer() << endl; cerr << "\tExpected: \"" << Expected << '\"' << endl; cerr << "\tReceived: \"" << Received << '\"' << endl; } return 0;} + +template struct Case_ { Case_(){start_time=clock();} }; +char Test_(...); +int Test_(Case_<0>) { + string hasManaged_[] = {"NNNN","NNYN","NNNN","YYYN"}; + vector hasManaged(hasManaged_, hasManaged_+sizeof(hasManaged_)/sizeof(*hasManaged_)); + int RetVal = 1; + return verify_case(RetVal, CompanyRestructuring().fewestDivisions(hasManaged)); } +int Test_(Case_<1>) { + string hasManaged_[] = {"NNYN","NNYN","YNNN","YYYN"}; + vector hasManaged(hasManaged_, hasManaged_+sizeof(hasManaged_)/sizeof(*hasManaged_)); + int RetVal = 1; + return verify_case(RetVal, CompanyRestructuring().fewestDivisions(hasManaged)); } +int Test_(Case_<2>) { + string hasManaged_[] = {"NYNNN","NNYNN","NNNYN","NNNNY","YNNNN"}; + vector hasManaged(hasManaged_, hasManaged_+sizeof(hasManaged_)/sizeof(*hasManaged_)); + int RetVal = 5; + return verify_case(RetVal, CompanyRestructuring().fewestDivisions(hasManaged)); } +int Test_(Case_<3>) { + string hasManaged_[] = {"NYNYYYNN" +,"NNNNYYNN" +,"NYNNYYNN" +,"NNYNYYNN" +,"NNNNNNNN" +,"NYYNYNNN" +,"YYNYYYNN" +,"YYNYYYYN"}; + vector hasManaged(hasManaged_, hasManaged_+sizeof(hasManaged_)/sizeof(*hasManaged_)); + int RetVal = 1; + return verify_case(RetVal, CompanyRestructuring().fewestDivisions(hasManaged)); } +int Test_(Case_<4>) { + string hasManaged_[] = {"NYNYNNYYNYNNNYYNYNYY" +,"YNNNNNYYNNNYYNYNYNYY" +,"NNNNNNYYNNNYYNYNNNNY" +,"YNYNNNNYNNNYNNYNYNNY" +,"NYNNNNNYYYYNYNYYNNYN" +,"YYYYNNYNYNNNNNYYNYNY" +,"NNNNNNNNNNNYYNNNNNYY" +,"NNNNNNYNNNNYYNYNNNYN" +,"NYYYNNNYNNNNYYNYYNYY" +,"NNYNNNYYNNNNYNNNNNYY" +,"YYNNNYNNYNNNNYNNYNYY" +,"NNNNNNNYNNNNYNYNNNNY" +,"NNNNNNYNNNNYNNNNNNNN" +,"NNYNNNNNNNNYYNYNNYYN" +,"NNNNNNNNNNNYNNNNNNNY" +,"YNYYNYYNNNYYNNNNYNYY" +,"NYNNNNNYNYNYYYYNNNNY" +,"NNYNNNNYNYNYYYNNNNYY" +,"NNNNNNNYNNNYNNNNNNNY" +,"NNNNNNYYNNNYYNYNNNYN"}; + vector hasManaged(hasManaged_, hasManaged_+sizeof(hasManaged_)/sizeof(*hasManaged_)); + int RetVal = 4; + return verify_case(RetVal, CompanyRestructuring().fewestDivisions(hasManaged)); } +int Test_(Case_<5>) { + string hasManaged_[] = {"NNNYNNNNNNNYNNNNNNNN" +,"NNNNNNYNNYNNNNNYNYYN" +,"YNNNNNYNYNNNNNNNNNNY" +,"YNNNNNNNNNNNNNNNNNNN" +,"NNYNNNYNYNNYNNYNNNNY" +,"NNNYNNNNNNNYNNYYYNYY" +,"NNYYNNNNYNNNNNNNNNNY" +,"NYNNNYNNYNNNYNYNYNNN" +,"NNNNNNNNNNNNNNNNNNNN" +,"YNNNNNNNNNNYNNYYNNYN" +,"NNYYNYNNYYNNNNYYNYNN" +,"NNNNNNNNYNNNNNNNNNNN" +,"NNNYYNYNYYYYNNNNYNYY" +,"NNYYNYNNYYYYNNNNNYNY" +,"NNYYYNNNYNNNNNNNNNNN" +,"YNNYYYNNNNNNNNYNYNYY" +,"NNNNYNYNNNNYNNYNNNNN" +,"YNNYYYYNNYNNYNNNYNYN" +,"YNNYYYYNYNNYNNYYYNNY" +,"YNYNNNNNYNNYNNNNNNNN"}; + vector hasManaged(hasManaged_, hasManaged_+sizeof(hasManaged_)/sizeof(*hasManaged_)); + int RetVal = 2; + return verify_case(RetVal, CompanyRestructuring().fewestDivisions(hasManaged)); } +int Test_(Case_<6>) { + string hasManaged_[] = {"NNNNN","NNNNN","NNNNN","NNNNN","NNNNN"}; + vector hasManaged(hasManaged_, hasManaged_+sizeof(hasManaged_)/sizeof(*hasManaged_)); + int RetVal = 5; + return verify_case(RetVal, CompanyRestructuring().fewestDivisions(hasManaged)); } + +template void Run_() { cerr << "Test Case #" << N << "..." << flush; Test_(Case_()); Run_()))==1 ? -1 : N+1>(); } +template<> void Run_<-1>() {} +int main() { Run_<0>(); } +// END CUT HERE + ADDED SRM/436/1A.cpp Index: SRM/436/1A.cpp ================================================================== --- SRM/436/1A.cpp +++ SRM/436/1A.cpp @@ -0,0 +1,102 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +using namespace std; +typedef long long LL; + +class BestView { +public: + int numberOfBuildings(vector heights) + { + int maxCnt = -1; + + int N = heights.size(); + for(int from=0; from