blog default image
SRMSRM Editorials

Single Round Match 754 Editorials




Match Overview

Single Round Match 754


Saturday, March 30th, 2019


MissingDwarf


rate_it


Used as: Division Two - Level One:


Value


250


Submission Rate


141 / 155 (90.97%)


Success Rate


126 / 141 (89.36%)


High Score


winterflame for 248.62 points (2 mins 7 secs)


Average Score


214.04 (for 126 correct submissions)

Statement


You're given [latex]6[/latex] numbers [latex]h_1,\dots,h_6[/latex]. You need to find smallest number [latex]h_7[/latex] such that [latex]h_7 > h_k[/latex] for [latex]k=1..6[/latex] and mean value of all numbers [latex]\frac{h_1 + \dots + h_7}{7}[/latex] is integer.

Solution


Find maximum [latex]h_k[/latex], start with [latex]h_7[/latex] being equal this number plus one, increase it until mean value becomes integer. You will do at most [latex]6[/latex] increases, thus running time is [latex]O(1)[/latex].


int getHeight(vector


SeventhPowers


rate_it


Used as: Division Two - Level Two:


Value


500


Submission Rate


114 / 155 (73.55%)


Success Rate


100 / 114 (87.72%)


High Score


ongchuviettel for 486.67 points (4 mins 43 secs)


Average Score


339.35 (for 100 correct submissions)

Statement


Consider integer [latex]A=a_{n-1}\dots a_1a_0[/latex], You're given [latex]B=a_{0}^{7}+a_{1}^{7}+\dots+a_{n-1}^{7}[/latex]. Construct any [latex]A[/latex] which gives such [latex]B[/latex] having no leading zeroes and length at most [latex]500[/latex]. [latex]B[/latex] itself is at most [latex]10^7[/latex].

Solution


One of possible ways to solve the problem is to greedily pick the largest digit [latex]x[/latex] such that [latex]x^7[/latex] is less than [latex]B[/latex], then subtract [latex]x^7[/latex] from [latex]B[/latex] and append it to the answer. It may be directly checked that this will provide short enough output for all possible inputs


string reconstructA(int B) {
string ans;
int t = 9;
while(B > 0) {
while(pow(t, 7) > B) {
t--;
}
ans += '0' + t;
B -= pow(t, 7);
}
return ans;
}


MoreSquares


rate_it


Used as: Division Two - Level Three:


Value


1000


Submission Rate


20 / 155 (12.90%)


Success Rate


4 / 20 (20.00%)


High Score


kektak for 586.75 points (28 mins 28 secs)


Average Score


500.18 (for 4 correct submissions)


Used as: Division One - Level One:


Value


250


Submission Rate


104 / 120 (86.67%)


Success Rate


56 / 104 (53.85%)


High Score


Egor for 237.29 points (6 mins 38 secs)


Average Score


171.60 (for 56 correct submissions)

Statement


You're given set of [latex]N \leq 3000[/latex] points [latex](x_1, y_1), \dots, (x_N, y_N)[/latex]. You have to calculate the number of point [latex](x,y)[/latex] such that if you add them to the set, the number of quadruples of points which form square will increase.

Solution


If point [latex](x,y)[/latex] increases the amount of squares among points, there should be two points [latex](x_i,y_i)[/latex] and [latex](x_j,y_j)[/latex] among the set forming the diagonal of that square, we can iterate over all such pairs in [latex]O(N^2)[/latex].


For given pair we can determine that center of the square is at the point [latex]\frac{(x_i+x_j,y_i+y_j)}{2}[/latex] and two other corners are in positions [latex]\frac{(x_i+x_j,y_i+y_j) \pm (y_i-y_j,x_j-x_i)}{2}[/latex], thus if one of such points is present in the set and the other is not, you should add the one which is not present to the set of points constituting the answer. Note that you can't simply increment answer because single point may add several squares and it will be counted multiple times in such a case.


typedef int ftype;
typedef complex


OrthogonalProjections


rate_it


Used as: Division One - Level Two:


Value


600


Submission Rate


15 / 120 (12.50%)


Success Rate


6 / 15 (40.00%)


High Score


Stonefeang for 277.42 points (43 mins 9 secs)


Average Score


260.52 (for 6 correct submissions)

Statement


Consider set of [latex]N[/latex] distinct points [latex](x_1,y_1), \dots, (x_N,y_N)[/latex] and a line [latex]L[/latex]. If point [latex]X[/latex] lies on the line [latex]L[/latex], the orthogonal projection of [latex]X[/latex] onto [latex]L[/latex] is [latex]X[/latex] itself. Otherwise, the orthogonal projection of [latex]X[/latex] onto [latex]L[/latex] is the unique point [latex]Y[/latex] on [latex]L[/latex] such that [latex]XY[/latex] is orthogonal to [latex]L[/latex].


Suppose you are given a finite sequence [latex]S[/latex] of points in the plane. Two lines [latex]L_1[/latex] and [latex]L_2[/latex] are equivalent if the orthogonal projections of points of [latex]S[/latex] onto [latex]L_1[/latex] are in the same order as the projections of points of [latex]S[latex] onto [latex]L_2[/latex]. You have to construct the set of [latex]N \leq 500[/latex] points having exactly given number [latex]n \leq 10^5[/latex] of equivalence classes.

Solution


First of all we have to determine how to calculate the number of equivalence classes for given set. Let's look on two particular points [latex]X_i[/latex] and [latex]X_j[/latex]. What relative configuration can they have? If the line is orthogonal to [latex]X_i - X_j[/latex] then they have same projection on it, let's call this line [latex]L_0[/latex]. Otherwise they lie in one order if line goes counter-clockwise from [latex]L_0[/latex] and in the other order if it goes clockwise from [latex]L_0[/latex].


If we consider lines [latex]L_0[/latex] for all possible pairs of [latex](X_i, X_j)[/latex], they will split the unit circle in [latex]2k[/latex] segments in such a way that lines going through same segment have same configuration with respect to given set of [latex]N[/latex] points. Also each endpoint will have its own configuration different from configurations in segments. It would provide you with [latex]4k[/latex] configurations, but it also counts inverted configurations which should not be counted, thus the number of configurations will be exactly [latex]2k[/latex] where [latex]k[/latex] is the number of distinct [latex]X_i-X_j[/latex] directions up to [latex]-1[/latex] multiplier.


Some manual check also tells us that it's impossible to obtain [latex]4[/latex] configurations and it's possible to obtain exactly [latex]1[/latex] configuration if you use only one point. Otherwise making [latex]n[/latex] configurations is possible if and only if [latex]n[/latex] is even. Let's consider one of possible explicit constructions to obtain [latex]n[/latex] configurations. Let [latex]t = \lfloor \sqrt{n} \rfloor[/latex], begin with [latex]t[/latex] points [latex](0,0), (0,1), \dots, (0,t-1)[/latex]. It will initialize our number of directions with [latex]1[/latex]. Now if we add arbitrary point [latex](x,y)[/latex] it will give us another [latex]t[/latex] directions. To fairly control number of new directions we will add points [latex](1,0), (1,t),(1,2t),\dots,(1,kt)[/latex], so with each new point we will have exactly [latex]t[/latex] new directions. But for final point you will have to add some number [latex]x[/latex] which is less or equal to [latex]t[/latex]. It can be done by placing point [latex](1,(k-1)t+x)[/latex] instead of [latex]kt[/latex].


For example, look on the following diagram:

  1. [latex]A-B-C[/latex] and [latex]D-E-F[/latex] lie on same lines having pairwise direction [latex](0,1)[/latex].

  2. [latex]DC,DB,DA[/latex] have directions [latex](-5,2),(-5,1),(-5,0)[/latex] respectively.

  3. [latex]EA,EB,EC[/latex] have directions [latex](-5,-1),(-5,-2),(-5,-3)[/latex] respectively.

  4. [latex]FC,FB,FA[/latex] have directions [latex](-5,-3),(-5,-4),(-5,-5)[/latex] respectively.


Note that because we took [latex]F[/latex] to be [latex]2[/latex] points above [latex]E[/latex] instead of [latex]3[/latex], exactly one direction to set of points [latex]\{A,B,C\}[/latex] was repeated. This solution works in [latex]O(\sqrt n)[/latex].


vector


RestoreDrawing


rate_it


Used as: Division One - Level Three:


Value


900


Submission Rate


5 / 120 (4.17%)


Success Rate


1 / 5 (20.00%)


High Score


tourist for 521.02 points (29 mins 9 secs)


Average Score


521.02 (for 1 correct submission)

Statement


You have to construct [latex]N \times M[/latex] grid [latex]N, M \leq 100[/latex] such that sizes of its 4-connected components are [latex]a_1,\dots,a_n[/latex] and sizes of its 8-connected components are [latex]b_1,\dots,b_m[/latex]. Where for arrays holds [latex]n,m \leq 20[/latex] and [latex]a_1 + \dots + a_n \leq 1500[/latex].

Solution


Note that you may take arbitrary set of [latex]4[/latex]-connected components, join it via diagonals and obtain single [latex]8[/latex]-connected component. Thus the whole solution splits in two parts: splitting sizes of [latex]8[/latex]-connected components into sizes of [latex]4[/latex]-connected components and constructing the answer.


First part is an instance of bin packing problem. Author's approach to solve it is to fix some order on set [latex]b[/latex] and calculate [latex]dp[mask][/latex] which is equal to [latex]1[/latex] if it's possible to construct prefix of [latex]b[/latex] (maybe with some remainder) such that it splits on two distinct subsets on [latex]b_1,b_1+b_2,b_1+b_2+b_3[/latex] and so on, it may be calculated in [latex]2^n \times n[/latex].


Tester approach is to use recursive function split(L, R, mask) which tries to split segment [latex]b_L, b_{L+1},\dots, b_{R-1}[/latex] using only items presented in [latex]mask[/latex]. To do this you split [latex][L,R)[/latex] into [latex][L,M)[/latex] and [latex][M,R)[/latex], iterate over all submasks of [latex]mask[/latex] and try to split subsegment [latex][L,M)[/latex] with such submask. If it was possible, you also try to split [latex][M,R)[/latex] with its complement. It's hard to estimate working time of such solution but rough upper bound is something like [latex]3^n[/latex] which, obviously, always way less on practice.


After you calculated possible splitting, you should compose [latex]4[/latex]-connected components with such sizes and make them into [latex]8[/latex]-connected components. One of possible ways to do it is to split the whole table by vertical line in half and fill 4-connected components alternatively, aligning them to the center. Another possible way is by using following pattern alternatively:

Screen-Shot-2019-03-31-at-10.42.27


Thus each 4-connected component is either single `#` or it is preceded and succeeded by single `#`, so that those single `#`'s maintain connection between 4-components into 8-component. Please feel free to inspect the code for better understanding.


const int maxn = 1

adamant


By adamant


Topcoder Member