And one more time I enter the DIV-1 class with a good performance in this SRM. I was able to very quickly solve the easy one, implement a clear solution for the medium, and devised a correct solution to hard problem, though I couldn’t finish implementing it in time during the contest. Later in practice I confirmed that my solution was correct.

**DIV-2 Easy; PrimalUnlicensedCreatures:**

In order to defeat a Grez, it helps to have maximum level possible to achieve before fighting this new Grez. In order to do so, it is intuitive that one should fight all creatures with power less then the next Grez you are about to fight. Thus solution is to sort creatures by increasing level of power, and incrementally beat creatures, till you can’t beat anymore of them.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
class PrimalUnlicensedCreatures { public: int maxWins(int iL, vector <int> gP) { int s = 0; sort(gP.begin(), gP.end()); for(int i=0;i<gP.size();i++) { if(gP[i]<iL) { s++; iL += (gP[i]/2); } } return s; } }; |

This is an implementation problem. One needs to be careful and make sure to understand what the problem is actually asking for. Only tricky case is when you have to decide between replacing your existing buffer with a string from undo history vs typing a few more characters to complete what you could restore from undo history.

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 |
class UndoHistory { public: string commonPre(string s1, string s2) { string pre = ""; if(s2.size()<s1.size()) swap(s1, s2); for(int i=0;i<s1.size();i++) { if(s1[i]==s2[i]) pre += s1[i]; else break; } return pre; } int minPresses(vector <string> l) { int r = 0; for(int i=0;i<l.size();i++) { string pre = ""; for(int j=0;j<i;j++) { string npre = commonPre(l[i], l[j]); pre = npre.size()>pre.size()?npre:pre; } if(pre.size()==0) { r += l[i].size()+1; if(i>0) r+=2; } else if(i>0 && pre==l[i-1]) { r += l[i].size()-pre.size()+1; } else { int a = l[i].size()-pre.size()+2+1; int b = INT_MAX; if(i>0) { string npre = commonPre(l[i], l[i-1]); if(npre==l[i-1]) b = l[i].size()-npre.size()+1; } r += min(a, b); } } return r; } }; |

**DIV-2 Hard; MarblePositioning:**

You have to place marbles in such a way that they do not overlap, yet they are closely packed such that distance between first and last marble is the minimum. Since number of marbles can be at most 8, we can iterate over all possible permutations, and figure out what is the least possible distance between first and last marble in this arrangement. Minimum of all arrangements is your answer. In order to ensure minimum distance between first and last marble for a specific arrangement, we need to place each new marble at a position that is closest to already placed marbles yet do not overlap any one of them. To do this, check what position you will have to place this new marble against each existing marble, such that it only touches it. The maximum position of all of these is the one you should place this new marble at.

In order to figure at which position you should place a marble while comparing it to an already placed marble, notice that the new marble will have to touch existing marble at one point, such that the direct distance between their centers will be r1+r2 (radius of first, plus radius of second marble), while the distance between their points touching the ground will be sqrt((r1+r2)^2 – (abs(r1-r2))^2)). Reducing this equation gives you 2*sqrt(r1*r2).

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 |
class MarblePositioning { public: double solve(vector<int> r) { vector<long double> pos(r.size(), DBL_MIN); pos[0] = 0.0; for(int i=1;i<r.size();i++) { for(int j=0;j<i;j++) { long double r1 = r[i]; long double r2 = r[j]; long double c = pos[j]+(2*sqrt(r1*r2)); pos[i] = max(pos[i], c); } } return pos[pos.size()-1]; } double totalWidth(vector <int> rad) { double r = DBL_MAX; sort(rad.begin(), rad.end()); do { r = min(r, solve(rad)); }while(next_permutation(rad.begin(), rad.end())); return r; } }; |

**Conclusion:**

With this SRM, I advance to division 1, and I hope to stay their for the time to come.