awoo's blog

By awoo, history, 4 years ago, translation, In English

1389A - LCM Problem

Idea: BledDest

Tutorial
Solution (BledDest)

1389B - Array Walk

Idea: Roms

Tutorial
Solution 1 (pikmike)
Solution 2 (pikmike)

1389C - Good String

Idea: BledDest

Tutorial
Solution (Ne0n25)

1389D - Segment Intersections

Idea: adedalic

Tutorial
Solution (adedalic)

1389E - Calendar Ambiguity

Idea: BledDest

Tutorial
Solution (pikmike)

1389F - Bicolored Segments

Idea: Roms

Tutorial
Solution 1 (Ne0n25)
Solution 2 (Ne0n25)

1389G - Directing Edges

Idea: BledDest

Tutorial
Solution (BledDest)
  • Vote: I like it
  • +127
  • Vote: I do not like it

| Write comment?
»
4 years ago, # |
  Vote: I like it 0 Vote: I do not like it

Super Fast!!

  • »
    »
    4 years ago, # ^ |
    Rev. 2   Vote: I like it +45 Vote: I do not like it

    Day by day I feel like nobody is getting the humor anymore they are just downvoting every comment.

»
4 years ago, # |
Rev. 2   Vote: I like it 0 Vote: I do not like it

In the Tutorial of Problem E: I think you mean yd + x instead of xd + y as the index of the x-day of the y-month in the year.

  • »
    »
    4 years ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    xd+y means index y-th day of x-th month which is corresponding pair of x-th day of y-th month whose index is yd+x.

»
4 years ago, # |
  Vote: I like it 0 Vote: I do not like it

Can someone tell me why my solution for D fails? I simply decide whether to gain intersection by joining two new segments or expanding others which already cover themselves totally. Counter-examples are especially appreciated.

Here's the submission: https://codeforces.net/contest/1389/submission/88410212.

»
4 years ago, # |
  Vote: I like it 0 Vote: I do not like it

Waiting for Div 3 :) After so many difficult rounds to increase rating :(

»
4 years ago, # |
  Vote: I like it 0 Vote: I do not like it

What is Event Processing I see it here and there but never find some concise tutorial on it.

»
4 years ago, # |
  Vote: I like it 0 Vote: I do not like it

I think we can just go through 45 combinations for C instead of the 100 given in the tutorial.

TLDR:

0101010 has both a 101010 and a 010101 type strings in it.

DETAILED:

Let us say the string we get after removing all other numbers but two be some 011100001001010.....

Then for the above we want it to reduce to the form : 01010101... Note, that if the reduced string ends with 1 then the answer is: size(original string)-size(reduced string). Else, the answer is: size(original string)-{size(reduced string)-1}.

»
4 years ago, # |
  Vote: I like it +3 Vote: I do not like it

Can someone explain the dynamic programming approach to problem E?

  • »
    »
    4 years ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    Problem E doesn't use dynamic programming. It's just math and number theory.

»
4 years ago, # |
  Vote: I like it +5 Vote: I do not like it

Cool contest!Though I've lost some points :(

»
4 years ago, # |
Rev. 3   Vote: I like it 0 Vote: I do not like it

.

  • »
    »
    4 years ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    Firstly.

    What does these numbers mean? Test containing 4 numbers, but u give 6. It's not 1 test and not 2.

    Secondly

    X can't be negative.

    In both pairs {l, r} l <= r, so max(r1, r2) >= max(l1, l2) => max(r1, r2) >= min(l1, l2)

    I hope you understand

»
4 years ago, # |
Rev. 4   Vote: I like it 0 Vote: I do not like it

Why this code is giving TLE? https://ideone.com/wXdX3A

»
4 years ago, # |
Rev. 10   Vote: I like it 0 Vote: I do not like it

Hey @adedalic I have one doubt in D, You have said,

In the case of non-intersecting [l1,l2] and [r1,r2], we should at first "invest" some number of steps in each pair to make them intersect. So let's iterate over the number of segments to "invest" cntInv. We should make cntInv⋅(max(l1,l2)−min(r1,r2)) steps to make segments touch. Now, cntInv segments touch so we can use almost the same formulas for them as in the previous case.
But consider case where input is,
5 16
1 4
8 12 
In this case should we invest time in joining all pairs or just invest time in joining one pair 
and complete operations using only that pair.
So steps for joining = 4
Steps to make both equal size = 11 , remaining k = 5.
So instead of joining all pairs just invest in first joined pair.
Remaining steps = 5*2 = 10.
Ans = 4 + 11 + 10.

Why should we invest in joining all pairs ? Am I missing something ? Community, Please help !!!

EDIT: Now I get it, u have used a for loop (i to n), that checks by joining first i pairs and compares it with minimum each time . Thanks.

  • »
    »
    4 years ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    Just iterate over n and find mininum value.

    ans = INF
    for i in :n
        temp = cost of getting K intersection with i segments joined.
        ans = min(ans,temp)
    
»
4 years ago, # |
  Vote: I like it 0 Vote: I do not like it

I wrote a recursive solution using memoization where I checked for each I it's i-1 and i+1 and updated my answer with maximum sum Does my solution not violate the condition given in the question :. " you can't perform two or more moves to the left in a row" Can anyone please explain this that how my function goes for 2 or more left moves in a row if z is>=2 and does not violate this and gets accepted

__88418038

»
4 years ago, # |
  Vote: I like it +42 Vote: I do not like it

Could anyone please explain the dp with segment tree approach for problem F?

  • »
    »
    4 years ago, # ^ |
      Vote: I like it +13 Vote: I do not like it

    Here's how I understand it (I did not solve in contest), feel free to correct me if I'm wrong or there is an easier way to think about this.

    First, let us sort all segments in increasing order of their right endpoints in the list $$$segs$$$. Split the segments based on type and sort them in the same fashion in the lists $$$s[t]$$$. Let $$$dp[i][j][t]$$$ be the answer assuming that the problem is restricted to the first $$$j$$$ segments in $$$segs$$$, along with the condition that we can only select the first $$$i$$$ segments in $$$s[t]$$$, and that we have used segment $$$i$$$ in $$$s[t]$$$ in our solution.

    Now, to transition to $$$segs[j+1]$$$ , assuming $$$segs[j+1]$$$ is of type 1 and is the $$$k+1$$$'th segment in $$$s[1]$$$, we have the following:

    • $$$dp[i][j+1][2] = dp[i][j][2] + 1$$$ if there is no touching between segment $$$k+1$$$ of $$$s[1]$$$ and segment $$$i$$$ of $$$s[2]$$$, and $$$dp[i][j+1][2] = dp[i][j][2]$$$ otherwise.
    • $$$dp[i][j+1][1] = dp[i][j][1]$$$ for all $$$i <= k$$$, and $$$dp[k+1][j+1][1]$$$ is the max of $$$dp[i][j+1][2]$$$ for all segments $$$i$$$ in $$$s[2]$$$ that do not touch with segment $$$k+1$$$ in $$$s[1]$$$.
    In other words, the dp table is largely unchanged, except that we try to add $$$s[1][k+1]$$$ to everything that we can, which must be optimal. The transition for where the new segment is of type 2 is very similar. Our answer is the maximum over the whole dp table.

    Now, notice that for any new segment, the segments that do not touch it must be a prefix of the sorted segment list. Thus, these transitions are exactly range add modifications and maximum queries on two segment trees. We can easily find what range to add by using a binary search. Finally, we can eliminate the 2nd dimension with $$$j$$$ just by directly modifying the segment trees.

    See my submission for implementation details.

    • »
      »
      »
      4 years ago, # ^ |
        Vote: I like it +8 Vote: I do not like it

      So, for implementation purpose, we can ignore the $$$2nd$$$ dimension of the dp state you mentioned, right? So our dp state will be:

      $$$dp(i,j): $$$ maximum segments if we have only considered till the ith segment of type $$$j$$$ and this one is included. And we traverse the segments on the sorted order of their right endpoints.

      • »
        »
        »
        »
        4 years ago, # ^ |
          Vote: I like it +8 Vote: I do not like it

        Yes, exactly. When I was trying to figure the solution out based on other people's code, I found the 3 dimensions to be easier (as a solution that I could actually come up with), but you have it exactly right.

        • »
          »
          »
          »
          »
          4 years ago, # ^ |
            Vote: I like it 0 Vote: I do not like it

          Okay, sounds good. Thanks a lot.

»
4 years ago, # |
  Vote: I like it +5 Vote: I do not like it

O(1) solution for D 88447760

»
4 years ago, # |
  Vote: I like it 0 Vote: I do not like it

Could somebody please explain the dp approach to B?

  • »
    »
    4 years ago, # ^ |
    Rev. 2   Vote: I like it 0 Vote: I do not like it

    Dp approach of B will be like that we have two choices at every index either we can move left or right, we will explore the moves and the maximum between these two options will be considered. base cases are:

    1 . when we don't have any moves left (k==0), in this case, we will return the current index value.

    2 . when the last move taken were left or we don't have any left moves remained or we are at the starting index of the array, then we have only one option to move right at this index.

    3.else we have two option to move right and left, and take the maximum among those to the answer. you can check my solution for more clarity, 88714266.

    • »
      »
      »
      4 years ago, # ^ |
        Vote: I like it 0 Vote: I do not like it

      How did you decide the states to cache? because here index,prev etc also change in every state...

»
4 years ago, # |
  Vote: I like it 0 Vote: I do not like it

Can anyone help me with my code for Problem C ? Getting a Wrong Answer.

def func():
	cnst = "01233456789"

	test = input()

	ans = 1000000000

	for i in cnst:
		for j in cnst:
			c1 = 0
			c2 = 0
			for k in range(len(test)):
				if (i != j and len(test) % 2 == 1 and k == len(test) - 1):
					continue
				elif (k%2 == 0):
					if (test[k] == i):
						c1 += 1
				else:
					if (test[k] == j):
						c2 += 1

			c111 = c1 + c2
			ans = min (len(test) - c111,ans)

	print (ans)


t = int(input())
for i in range(t):
	func()
 
  • »
    »
    4 years ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    Your code is checking if i is at even and j is at odd positions, which is wrong. You need to find the longest SUBSEQUENCE of alternating i and j of even length. INPUT: 1 2025 YOUR OUTPUT: 1 CORRECT OUTPUT : 2

»
4 years ago, # |
Rev. 5   Vote: I like it 0 Vote: I do not like it

Same approach as Editorial still can't be accepted in python3 due to TLE on test 2 , what is this?

1389C-Good String 88530443

  • »
    »
    4 years ago, # ^ |
    Rev. 2   Vote: I like it 0 Vote: I do not like it

    First: Submit in PyPy instead of CPython

    Second: Convert the input to a list of ints once instead of converting it in each iteration.

»
4 years ago, # |
  Vote: I like it 0 Vote: I do not like it

O(1) solution for problem D Segment Intersections.

  • »
    »
    4 years ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    If anyone is interested in the explanation then comment.

    • »
      »
      »
      4 years ago, # ^ |
        Vote: I like it 0 Vote: I do not like it

      interested

      • »
        »
        »
        »
        4 years ago, # ^ |
          Vote: I like it +3 Vote: I do not like it

        There can only be three initial configurations for the two intervals:

        1> One completely overlaps the other.

        2> There is some partial intersection b/w them.

        3> They are disjoint.

        Now, let us tackle them one by one.

        1>> We will have an initial I which can be calculated easily, thereafter if the current I is smaller than k we can increase the intersection length by one for all n pair of segments in just 1 step and we can do this untill all the pairs have become equal It is obvious that we cannot increase I by more than 1 in a single operation, So this is optimal. Now if still I is less than k we only have one way to increase I by increasing the segment lengths of a pair this will give us a +1 for each 2 step. Also, we can do this indefinitely.

        2>> Its the same as the first one except that the initial I has to be calculated differently.

        3>> Initial I=0 , Now how can we increase I? we first have to make a pair of segment meet each other while doing this our I remains the same, lets call this cost as gap. Now as soon as the segments meet we can start having a bunch of +1s . Let us calculate how many +1s we can get in a row, Suppose the worst case when initially both segments are points, then we will have no of +1s equal to the no of +0s (we used to fill the gap) . And in every other case the no of +1s will be greater than +0s , Remember this. Now let us see what choice do we have after using up these two operations , At this point we reach a situation analogous to one explained earlier in 1>> where we will be getting +1 for every 2 steps. We have all the tools ready . Let us expand this for n. I claim that in an optimal final solution we will have a situation where x of the n segments are completely overlapping and at most 1 pair of segments which has been extended by a 2 step operation or has a partial overlap , Also x will be maximum. Suppose a case where x is not the maximum then we have a case where x-1 segments have complete overlap and 1 segment with a partial overlap or an extended 2 step operation segment. We can simply rule out the first case because we can never compensate for the loss of one completely overlapping segment with a partial one. As for the other case the no of 2 step operation will be greater than the complete overlap of the segments as compensation. This is where we use the fact we proved earlier, that we can get a complete overlapping with total no of steps less than or equal to twice the size of the segments, which will always be at least as efficient as the +2 step operation and hence we can carve out a completely overlapping segment from this segment without loosing optimality (for the lack of better word).

        • »
          »
          »
          »
          »
          4 years ago, # ^ |
            Vote: I like it +3 Vote: I do not like it

          Thanks, man for the effort! stating 3rd case more mathematically -

          suppose we have to make 'rem' out of 'n' segs, let 'ex' be the amount to 'activate' the seg (= max(l1,l2)-min(r1,r2)), and 'tot' (= max(r2,r1)-min(l1,l2)) we can get by doing 1 inc per 1 move operation (+1/1 ops) after 'activation'.

          Its oBvious that we have to 'activate' the 1st seg and do +1/1 ops If still we have something remaining (let it be 'x')

          now if 'x' >= 'tot' then its better to activate another seg and get tot why? because 'tot'+'ex' <= 'tot'*2 as 'ex' <= 'tot'

          if 'x' < 'tot' then we decide on the basis of which is bigger, 'x'+'ex' or 'x'*2

          so how is this o(1) -> after the 1st activation, we can activate segs until reqd amount < 'tot' and then we decide between 'activation'+'+1/1 ops' and '+1/2 ops' for the last one.

          • »
            »
            »
            »
            »
            »
            4 years ago, # ^ |
              Vote: I like it 0 Vote: I do not like it

            Yeah at one point I thought the way I described it isn't going to help anyone, happy to see that you got it... P.S. yours seems much formal and clear.

»
4 years ago, # |
Rev. 3   Vote: I like it -8 Vote: I do not like it
  • »
    »
    4 years ago, # ^ |
    Rev. 3   Vote: I like it 0 Vote: I do not like it

    Try to write the code in the code snippet. Yours is very unreadable and takes a lot of space in the comments

    for(int i = 0: i < n; i++) {
        likeThis();
    }
    

    EDIT: For your problem, a string is good if it has all same characters in case or even length, or same parity indexed characters are same in case of odd length.

»
4 years ago, # |
  Vote: I like it 0 Vote: I do not like it

Can anyone provide the Test case 2 — 19th input for task B. Most people had this error "expected 218 found 216". If not exact same test case, then maybe something similar?

  • »
    »
    4 years ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    I don't know the exact test case but you should also handle for the case k=0 explicitly because my solution got accepted after I make a case for k==0.

»
4 years ago, # |
  Vote: I like it 0 Vote: I do not like it

in problem B, the tutorial assumes that the left steps will always be accompanied by right steps as they have been taken in pairs but it may so happen that the last step will be a left step, so in that case the tutorial fails and so does the solution in the following test case

18 11 4 11 19 18 19 19 5 14 15 17 4 10 9 8 17 9 2 15 10

  • »
    »
    4 years ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    The tutorial assumes left steps are always preceded by a right step; Hence the order (right,left).

»
4 years ago, # |
Rev. 2   Vote: I like it 0 Vote: I do not like it

The idea of G is quite similar to this problem Museum Tour

Very interesting:

Undirected Graph -> find Bridges -> Tree -> Can be solved using DP

Directed Graph -> find SCC -> DAG -> Can be solved using DP

»
4 years ago, # |
  Vote: I like it 0 Vote: I do not like it

Not expecting DP in second problem.

»
4 years ago, # |
Rev. 3   Vote: I like it 0 Vote: I do not like it

88352400 for probblem C , can someone please tell me what's wrong in my code?

Spoiler
»
4 years ago, # |
  Vote: I like it +8 Vote: I do not like it

I have tried to make editorial for questions A-E . please have a look. Language :- Hindi

https://www.youtube.com/watch?v=S_-BaaH7P80&list=PLrT256hfFv5X1GNpiqEh5njN_WJmQu8Q6

»
4 years ago, # |
  Vote: I like it 0 Vote: I do not like it

In problem C, if we are using brute force, for all 100 combinations it is O(n) then it becomes overall O(100*n) which is O(10^7) and with that we have 1000 test cases also, so how does it even work? At first i didn't do it because i thought maybe it will cause TLE, but I realized that they have done the same thing.

»
2 years ago, # |
  Vote: I like it 0 Vote: I do not like it
»
18 months ago, # |
  Vote: I like it 0 Vote: I do not like it

Brainless segment tree solution for problem F:

  1. Coordinate compress the left and right bounds of all the segments, so that we can build a segment tree on these segments.
  2. I'll just call the two types of segments as "Red" and "Blue". Create two segment trees, one for the red segments $$$R$$$ and one for the blue segments $$$B$$$.
  3. $$$i'th$$$ Leaf Node of segment tree $$$R$$$ will hold two values: $$$dp_{i}$$$ which is the maximum number of segments we can choose if the last segment chosen is red (till that iteration, context to be given later) and $$$cnt_{i}$$$ which will store number of blue segments with $$$leftbound > i$$$ (till that iteration). Segment tree $$$B$$$ is also symmetric to $$$R$$$, just interchange blue and red segments in definition. These segment trees will allow us to query $$$max(cnt_{i} + dp_{i})$$$ in some range.
  4. Sort all segments in increasing order of $$$rightbound$$$ and iterate over them. Let's say you are at red node, then you will transition from segment tree $$$B$$$. So firstly increment $$$cnt$$$ for all leaf nodes in $$$B$$$ in range $$$(1, leftbound - 1)$$$, as red and blue segments cant intersect. To transition, you just have to do a max query on range. (Once again, for blue segments, everything is symmetric, you will perform shit o $$$R$$$ in that case.)

Implementation: link