Single Round Match 754 Editorials
Single Round Match 754
Saturday, March 30th, 2019
Used as: Division Two - Level One:
|
|
|
|
|
|
|
|
|
|
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
Used as: Division Two - Level Two:
|
|
|
|
|
|
|
|
|
|
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;
}
Used as: Division Two - Level Three:
|
|
|
|
|
|
|
|
|
|
Used as: Division One - Level One:
|
|
|
|
|
|
|
|
|
|
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
Used as: Division One - Level Two:
|
|
|
|
|
|
|
|
|
|
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:
[latex]A-B-C[/latex] and [latex]D-E-F[/latex] lie on same lines having pairwise direction [latex](0,1)[/latex].
[latex]DC,DB,DA[/latex] have directions [latex](-5,2),(-5,1),(-5,0)[/latex] respectively.
[latex]EA,EB,EC[/latex] have directions [latex](-5,-1),(-5,-2),(-5,-3)[/latex] respectively.
[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
Used as: Division One - Level Three:
|
|
|
|
|
|
|
|
|
|
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:
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
By adamant
Topcoder Member