### chokudai's blog

By chokudai, history, 10 months ago,

We will hold AtCoder Beginner Contest 159.

We are looking forward to your participation!

• +52

 » 10 months ago, # |   +5 How to solve E?
•  » » 10 months ago, # ^ | ← Rev. 2 →   +15 brute force over all possible lines for the rows. Then solve greedily for columns.
•  » » » 10 months ago, # ^ |   +8 What is the greedy for columns?
•  » » » » 10 months ago, # ^ | ← Rev. 4 →   +16 For every "block" (rows between two horizontal cuts), maintain count of 1's for each column.So if you had something like: 1 1 1 1 1 0 1 0 0 0 1 0 0 1 1 1 Let's say currently you're making cuts under rows 1 and 3 (1-based), so it'd be something like: 1 1 1 1 first block -------------- 1 0 1 0 0 0 1 0 second block -------------- 0 1 1 1 third block Now maintain count of 1's for each block at each column: 1 1 1 1 -------------- 1 0 2 0 -------------- 0 1 1 1 You know that if in any block, any count is bigger than k, than this horizontal cut setup is impossible, so you can just try the next mask.But in this case, let's say $k=2$. Now iterate through each column and each block and maintain one horizontal sum for each block, if at any index $i$ $\text{horizontal sum of some block} > k$, than you have to make a cut, and sum in each block becomes the i-th element in that block.So in the first block and second block, sum of the first three elements will exceed k, so you will make a cut there and sum will reset ($sum[1] = 1, sum[2] = 2, sum[3] = 1)$. 1 1 | 1 1 ----|---------- 1 0 | 2 0 ----|---------- 0 1 | 1 1 Welp, hope that explanation wasn't terrible haha.
•  » » » » » 10 months ago, # ^ |   +6 Thanks a lot, you explained far better than the editorial.
•  » » » » 10 months ago, # ^ |   0 Go through the array column by column. If the group size exceeds k for any of the groups in a column, we draw a line before the column and update the size of each group.
•  » » » » 10 months ago, # ^ |   0 Another way to understand greedy is to solve the 1-D problem; A = [1 1 0 0 1 ... etc]. In this case you will obviously keep going as far as possible until you need to make a cut. "As far as possible" means until your current sum exceeds K. Then extending this idea to the columns is easy.
•  » » 10 months ago, # ^ |   0 For splits across rows, try out all 2^r (actually 2^(r-1)) splits, for e.g using bit masks. Then greedily perform splits across the columns.
•  » » 10 months ago, # ^ |   0 Why should I go over 0 to 2^(h-1). Can anyone explain this in detail please :(
•  » » » 10 months ago, # ^ | ← Rev. 3 →   0 Coz you are checking all possible combination. Suppose you have 3 rows, then row-wise you have 2 possible lines to cut the bar ( one line between 1st and 2nd row, and another between 2nd and 3rd row ), generally speaking in a N row bar you have (N-1) lines to possibly cut the bar. Now you don't know which lines you should cut through to have MINIMUM value of cuts and meet the condition, so you check all possibilities. In our example, [ 0 , 2^(3-1) — 1 ] == [ 0, 3 ]. If you observe the binary representation of the numbers, decimal --> binary --> interpretation 0 --> 00 --> No cut is needed row-wise 1 --> 01 --> Cut only through the 2nd line 2 --> 10 --> Cut only through the 1st line 3 --> 11 --> Cut through all the lines If you observe carefully the binary representation of numbers from [ 0, 2^(n) — 1] actually contains all possible combination for n items as described above. It is a simple trick we use in cp.
•  » » » » 10 months ago, # ^ |   +2 Hey makes sense to me thank you for the reply :) care to explain what to do in each and every combination.
•  » » 10 months ago, # ^ |   0 If you want to see solution, You can see my submission, if you still have any doubt. i have used naming convention related to my approch such that someone can understand it !!
 » 10 months ago, # |   +30 How to solve F?
•  » » 10 months ago, # ^ |   +21 Let $\mathrm{dp}[i][j]$ be the sum of left endpoints of all sequences that end at $i$ and have weight $j$. The answer is $\sum_{i = 1}^n (n - i + 1) \mathrm{dp}[i][S]$.
•  » » » 10 months ago, # ^ |   +3 I understand that we basically add for all i,j of subsequences the pairs of indexes including i,j. That is all where left is less or eq i, and right bg j.But I do not get how we find all i,j of subseqs with sum==s? And even not the sum of the i?
•  » » » » 10 months ago, # ^ | ← Rev. 2 →   +76 I don't understand a word of what you just said, but I'll try to elaborate a bit. If we have a subsequence with sum $S$ that starts at $l$ and ends at $r$, its contribution to the answer is $l (n - r + 1)$. That's because it is contained in $f(L, R)$ exactly if $L \le l$ and $r \le R$.We can think of it like this. There are many different subsequences with left endpoint $l$ and right endpoint $r$, suppose the $i$-th of them has left endpoint $l_i$ and right endpoint $r_i$. Then the answer to the problem looks like $l_1 (n - r_1 + 1) + l_2 (n - r_2 + 1) + l_3 (n - r_3 + 1) + \cdots.$(Obviously, this sum is too large to calculate directly, it is just to help with thinking.)Let's look at one particular $r$ and all the summands of the form $l_i (n - r + 1)$. That part of the sum above looks like $l_1 (n - r + 1) + l_2 (n - r + 1) + l_3 (n - r + 1) + \cdots = (l_1 + l_2 + l_3 + \cdots) (n - r + 1).$So, we are interested in the sum of left endpoints of all subsequences with weight $S$ that end at $r$.This can be calculated through dynamic programming. As I stated above, define $\mathrm{dp}[i][j]$ as the sum of left endpoints of all subsequences with weight $j$ that end at $i$. Then we can calculate it for all $i$ like: \begin{align*} \mathrm{dp}[i][A_i] &= i \\ \mathrm{dp}[i][j] &= \sum_{k < i} \mathrm{dp}[k][j - A_i] \text{ for all } j > A_i. \end{align*}
•  » » » » » 10 months ago, # ^ |   +4 Isn't this O(N*N*S) ?
•  » » » » » » 10 months ago, # ^ |   +16 No, it is $\mathcal{O}(NS)$. We can calculate the last formula using standard prefix sums.
•  » » » » » 10 months ago, # ^ |   0 Will there be illegal transitions?As dp[k][j-A[i]](kA[i])->dp[i][j],but A[k]>A[i]. According to the problem statement, this is a illegal transition.
•  » » » » » » 10 months ago, # ^ |   0 Made a mistake reading the statement.233
•  » » 10 months ago, # ^ | ← Rev. 2 →   +12 Hint : SpoilerConsider a sequence Ai...Aj...Ak ，that ∑A = S.There are i*(n-k+1) pairs that contain this. Further HintFor each k(the last index in the sequence) , We must know ∑i(the first index) Dp solutionDp[i][j] means the sum of (the first index) of the sequences that the sum of that is j.You can update the Dp easily.Then consider the i-th element as the last one,the answer add (n-i+1)*(dp[i-1][s-a[i]])
 » 10 months ago, # |   +1 Just one optimization, but i believe E can also be solved considering (2^(H-1)) masks.
•  » » 10 months ago, # ^ |   +3 can you please explain your logic?
•  » » » 10 months ago, # ^ | ← Rev. 3 →   0 For reducing the mask value, you could do a recursion for each row based on the assumption if ith row remains together with (i-1)th row or not after all the cuts are made.So we only do this differently (2^(H-1)) times, because 1st row doesn't have any preceding row, so it is assigned any relation with a previous row. Or simply just select (H-1) bits for masking where ith bit denotes if (i+1)th row is together with ith row or not in the final cut. Edit : 0 based indexing used here
•  » » » » 10 months ago, # ^ |   0 Thank you:)
 » 10 months ago, # | ← Rev. 2 →   0 For problem E, does some fail in test6 and pass at last? I've been struggling for quite a while, my code status.Thanks in advance.
 » 10 months ago, # |   +12 Supplementary editorial for questions 2, 4, 5, 6 AtCoder ABC 159
 » 10 months ago, # |   +1 Hey guys, does any of you got to read the editorial for problem F and can help me understand the transitions for the DP? It seems quite interesting.I don't understand why at this point when we calculate dp[i+1][j+a[i]][2] we have to include dp[i][j][0], why should we add R without actually adding the L ? Thank you in advance for help.
•  » » 10 months ago, # ^ |   +2 Can you tell the meaning of Adding Only L , not adding L and R , adding both L and R ?? TheRedLegend
•  » » » 10 months ago, # ^ | ← Rev. 2 →   0 As far as I understand it, we can start a new interval at each i, where 1 <= i <= n , this means that we set L (t = 1) to mark the beginning, then we can stop and interval meaning that we set R (t = 2) (but when setting R we need to have set L before, I understand this as transition between t = 1 to t = 2). We can set R at any position from [i..n]. And being between L and R we can add elements to the partial sum.When you think about this for a while you can easily notice that we will have L at any position and then R after L so L <= R, this meaning that we will iterate through all the possible L and R. I think this is the logic but I am not 100% sure. Still I don't understand why when computing dp[i+1][j+a[i]][2] we have to include dp[i][j][0].
 » 10 months ago, # | ← Rev. 2 →   +1 Can someone explain tmwilliamlin168's solution for F? https://atcoder.jp/contests/abc159/submissions/11099994
•  » » 10 months ago, # ^ |   0 As far as I see, dp[i][j] is the answer for the first $i$th number if the required sum is $j$.When we are processing the new number $x$, obviously the previous sequence could be reused. Now consider how the new number could contribute to answer: First, itself could be a sequence so dp[i][x]=i. Then it could also form a sequence with previous sequences so dp[i][j]+=dp[i-1][j-x] for all $j\ge x$
 » 9 months ago, # |   0 where can I find solutions in English?
•  » » 7 months ago, # ^ |   0 There's actually some explanation in English at the bottom of the editorial.I couldn't see it at first.
 » 9 months ago, # |   0 what is the logic for D?
•  » » 6 weeks ago, # ^ | ← Rev. 3 →   0 Here you have to choose every $2$ elements from total count(except for the number itself) of a number.-First find the total $\sum\limits_{i = 0}^m\binom{cnt}{2}$;Here $m$ is total unique elements-Then exclude the extra element from the total $total-\binom{cnt}{2}+\binom{cnt-1}{2}$ $\binom{n}{r}=\frac{n(n-1)(n-2)...(n-r+1)}{r!}$My submission
 » 7 months ago, # |   0 This may seems weird but can anyone explain the idea of C?Sadly i couldn't understand what was written in the editorial..
•  » » 7 months ago, # ^ |   0 Have to find the maximum volume. As we know when l == b then area of rectangle is maximum . In the same way in three dimensions you should have all sides same to get max volume of cuboid i.e. cube so each side become L/3,L/3,L/3 volume L*L*L/27
•  » » » 7 months ago, # ^ |   0 Thank you :)
 » 5 weeks ago, # |   0 F can be solved with O(S) memory also. Submission