AdvancerMan's blog

By AdvancerMan, 10 days ago, translation, In English,

1263A - Sweet Problem

Idea: MikeMirzayanov

Tutorial
Solution (MikeMirzayanov)

1263B - PIN Codes

Idea: Stepavly

Tutorial
Solution (Stepavly)

1263C - Everyone is a Winner!

Idea: unreal.eugene

Tutorial
Solution (unreal.eugene)

1263D - Secret Passwords

Idea: Stepavly

Tutorial
Solution (Stepavly)

1263E - Editor

Idea: Supermagzzz

Tutorial
Solution (Supermagzzz)

1263F - Economic Difficulties

Idea: AdvancerMan

Tutorial
Solution (Rox)
 
 
 
 
  • Vote: I like it
  • +63
  • Vote: I do not like it

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

Problem C can be done in O(√n) , I think. I just have a for loop 1->sqrt(n) and add 2 numbers to the list and got accepted :D

  • »
    »
    10 days ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    I think I have the same idea with you

  • »
    »
    10 days ago, # ^ |
    Rev. 2   Vote: I like it -7 Vote: I do not like it

    Edit: There was no need to use set or sort the vector. I was wrong.

    You need to either use a set, or if you used a vector then you need to sort them. So the factor of log(N) comes from there.

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

      For each $$$i \leq \sqrt{n}$$$, I calculate $$$h= \lfloor \dfrac{n}{\lfloor \frac{n}{i} \rfloor} \rfloor$$$. If $$$i=h$$$, I push back $$$i$$$ into a vector. If $$$t=\lfloor \frac{n}{i} \rfloor \neq i$$$, I also push back $$$t$$$ into another vector.

      Rearrange the order of printing elements from both vectors and you are good to go.

  • »
    »
    10 days ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    You add numbers to a set. Adding to the set is log(n). So your solution has O(sqrt(n)log(n))

»
10 days ago, # |
  Vote: I like it -8 Vote: I do not like it

For Problem B a similar solution would be to consider a map for storing all the found pins while iterating through the list. Once it finds a pin that already exists in the map, randomly choose any position in the pin to replace it randomly with another digit. This repeats until we get a new 4 digit number. And this counts as a step.

Finally, print the step count and the new pins.

https://codeforces.com/contest/1263/submission/66044121

»
10 days ago, # |
  Vote: I like it +15 Vote: I do not like it

C can also be solved using the fact that $$$j = \left\lfloor \dfrac{n}{\left\lfloor\dfrac{n}{i}\right\rfloor }\right\rfloor$$$ is the largest $$$j$$$ such that $$$\left\lfloor\dfrac{n}{i}\right\rfloor = \left\lfloor\dfrac{n}{j}\right\rfloor$$$.
65967376

»
10 days ago, # |
  Vote: I like it 0 Vote: I do not like it

Can anybody explain me logic of problem E. How it is done using segment tree?

  • »
    »
    10 days ago, # ^ |
      Vote: I like it +20 Vote: I do not like it

    Suppose that '(' is 1 and ')' is -1, and others are 0. If the string is a correct text, these conditions will be satisfied:
    - The sum of any prefix should not less than 0 because that means the number of ')' is greater than '(' in this prefix.
    - The sum of the whole string should be 0 because the number of '(' and ')' should be equal.
    If the two conditions are not satisfied, the answer is -1.
    Otherwise, the answer is equal to the maximum depth of these brackets. It is equal to the maximum sum prefix because the sum of a prefix means how much '(' do not match with a ')' in the prefix.
    So we can use a segment tree to keep the sum of all prefixes and get the maximum and minimum sum prefix.
    My code: 66065668

    • »
      »
      »
      9 days ago, # ^ |
        Vote: I like it 0 Vote: I do not like it

      Thanks got it.

    • »
      »
      »
      9 days ago, # ^ |
        Vote: I like it +1 Vote: I do not like it

      Can you explain your solution a little bit. What is the use of tag.

      • »
        »
        »
        »
        9 days ago, # ^ |
          Vote: I like it 0 Vote: I do not like it

        I use a segment tree to keep prefix sum array, so when I modify the $$$i$$$-th element in the original array, I need to update sums of prefixes that contain the $$$i$$$-th element. They are a continuous range $$$[i, n-1]$$$ in the prefix sum array, so we need to use lazy tag to do range updates. If the tag of a node is $$$t$$$, that means every element in the range should plus $$$t$$$, and the maximum and minimum value of the node should plus $$$t$$$, too.

    • »
      »
      »
      9 days ago, # ^ |
        Vote: I like it 0 Vote: I do not like it

      What's the time complexity? Is it n^2*log(n) ?

      • »
        »
        »
        »
        9 days ago, # ^ |
          Vote: I like it 0 Vote: I do not like it

        No, the time complexity is $$$O(n \log n)$$$.
        The time complexity of doing a range update or range minimum/maximum query is $$$O(\log n)$$$. In my code, when I process a command, I do range update at most twice and range minimum/maximum query at most 3 times, so the time complexity of processing a command is $$$O(\log n)$$$. There is $$$n$$$ commands, so the time complexity is $$$O(n \log n)$$$.

»
10 days ago, # |
  Vote: I like it +3 Vote: I do not like it

How to solve problem E using lazy segment tree? :)

»
10 days ago, # |
  Vote: I like it 0 Vote: I do not like it

Stepavly How can I solve D using std::set ?

»
9 days ago, # |
  Vote: I like it +5 Vote: I do not like it

A different O(n) approach for E:

Maintain a prefix sum and store maximum and minimun in it.

Be lazy when the operation is L\R or when #( != #) (number of brackets).

when #( = #), update the prefix sum just in the range the cursor touched, amortize time is O(1) since L,R operations paid for each update in this range.

To maintain maximum and minimum store an histogram of prefixes.

solution: 66080615

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

    i was thinking in this way, but then i've remembered that i'm dummy and came with obvious segment tree solution

»
9 days ago, # |
  Vote: I like it +5 Vote: I do not like it

Sweet candies problem If we are given 8 2 8 candies, there is no possible way that we can eat it for 9 days. Can you tell me what is wrong in my approach?

  • »
    »
    9 days ago, # ^ |
      Vote: I like it +5 Vote: I do not like it

    It is possible that we can eat candies for 9 days. One scenario for example would be that we eat 7 candies from biggest and medium piles i.e. 8 and 8 here. This can be done for 7 days. Then we are left (1 2 1) candies. Then eat such that the pile is (0 1 1) on the 8th day and on 9th day, it would be (0 0 0).

»
9 days ago, # |
  Vote: I like it 0 Vote: I do not like it

In Problem A, how is the answer if r < g +b is (r + b +g)/2, what is the maths behind? Or is it purely observational?

  • »
    »
    9 days ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    When the maximum number is less than the sum of the other two numbers, you can try to simulate this process. Use a priority queue to select the maximum two numbers from each time and subtract one. When this operation is completed, the maximum number is always less than the sum of the other two numbers until it becomes 0.00 or 0.01. The answer (B + G + R) / 2 mathematical induction can prove that my English expression is not very good I hope my answer will help you When the maximum number is larger than the sum of the other two numbers, answer is obviously the sum of two less numbers.

    • »
      »
      »
      8 days ago, # ^ |
        Vote: I like it 0 Vote: I do not like it

      Would please show the proof for the mathematical induction of (r+b+g)/2

      • »
        »
        »
        »
        8 days ago, # ^ |
          Vote: I like it 0 Vote: I do not like it

        Suppose B>=G>=R, and B is less than or equal to G + R, When (B + G + R) <= 2, (when B G R is 1 1 0 or 0 0 0 respectively), the answer is (B + G + R) / 2 When (B + G + R) > 2, when we reduce B G by one, the maximum number of the answer plus one or three numbers is still less than or equal to the sum of the other two numbers, so we prove that. Above all.

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

Can anybody explain the Segment tree solution of E problem a little elaborately, I have read solutions of many people but couldn't decipher the logic of what they are trying to do. I know these things that the minimum sum prefix has to be greater than -1, total sum has to be 0 and maximum sum prefix would the answer, but how exactly are we computing this using a segment tree?

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

    Suppose you know that the total length of everything is n. Let each '(' to be a +1, each ')' to be a -1, and each regular character to be a 0.

    So for example if I have string (((a))), we get [1, 2, 3, 3, 2, 1, 0]. Notice that if we drop below 0 then we have an invalid string since there are too many ')'. The maximum amount of nesting is what we want to query, and that's just the maximum value on the segment.

    When we make changes we can just perform updates on segment. For example if I change from 'a' -> '(' at index i, then I just add 1 for all elements from [i...n], if I change from ')' to '(', then I have to add 2 for all elements [i...n]. Really we have range sum updates and range max/min queries which a straightforward use of segment tree

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

      Thank you so much, this explanation is crystal clear. But I wanted to ask you this that changing a a to ( is a point update so can't we do that point update and calculate the max prefix and min prefix sum using some other way? I thought of a way that we store totalSum, MaxPrefixSum and MinPrefixSum for each node, now for every parent thw updation would be something like parent.MaxPrefixSum = max(Left.MaxPrefixSum, Left.TotalSum+Right.MaxPrefixSum) And similarly for MinPrefixSum TotalSum would be updated directly.

»
9 days ago, # |
  Vote: I like it 0 Vote: I do not like it

Can someone explain Problem C, I am not able to understand the math

  • »
    »
    9 days ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    The idea is that, given n, you need to iterate through all numbers i where (0<=i<=n) and see the result of dividing n/i and store it in a set.

    The optimization trick is to only iterate through i where (0<=i<=√n) and store both i and n/i in the set. So the time complexity is O(t√n). Here is my code: 65966154

»
8 days ago, # |
Rev. 4   Vote: I like it +8 Vote: I do not like it

Challenge Problem F completed:

The complexity of my code is linear. I only use algorithms like dfs, counting sort, monotonic stack.

My solution

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

please explain about B problem. I not getting What is logic.thanks.

»
8 days ago, # |
  Vote: I like it +8 Vote: I do not like it

(In the editorial of problem A)Is the line correct? "Then we make equal the piles g, b by eating g−b from the piles r and b".

I think it would be-"Then we make equal the piles g, b by eating g−b from the piles r and g".

  • »
    »
    6 days ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    You are right, thanks for pointing it out! The tutorial for A will be updated soon.

»
7 days ago, # |
  Vote: I like it +1 Vote: I do not like it

How to solve problem E with segment tree??? plz explain for me. thanks..

  • »
    »
    7 days ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    Lets assume '(' = 1 and ')' = -1 and any other character is equal to 0. For each segment store the following things:-
    1. Number of opening brackets.
    2. Number of closing brackets.
    3. Maximum prefix sum and minimum prefix sum.

    For each iteration update the tree. The string is balanced if minimum prefix sum is positive or zero and number of opening brackets == number of closing brackets. Number of different colors (final answer) is maximum prefix sum. You can have a look on my submission.66222540

»
7 days ago, # |
  Vote: I like it +5 Vote: I do not like it

Tutorial for problem has a typo I guess: Then we make equal the piles g, b by eating g−b from the piles r and b.

Here it should be piles r and g I think. Correct me if I am wrong.

»
7 days ago, # |
  Vote: I like it 0 Vote: I do not like it

Can someone explain how to solve Div2-F. I am not able to understand the editorial. P.S- What is " the segment [l,r]", I am not able to understand it. Thanks in advance.

»
7 days ago, # |
  Vote: I like it 0 Vote: I do not like it

How to solve F by lowest common ancestor approach?

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

Can anyone please tell me where my approach of Problem B is wrong .

https://codeforces.com/contest/1263/submission/66242150

It's even showing the wrong answer on test case 1, which i can't seems to understand.

EDIT: Found my mistake !!

»
6 days ago, # |
  Vote: I like it 0 Vote: I do not like it

Can you tell me why for the input 8 9 10

output is 13, it should be 16.

  • »
    »
    6 days ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    Tanya can't eat for $$$16$$$ days straight because the sum of piles' sizes is reducing by $$$2$$$ every day so she can't eat for more than $$$\left\lfloor \frac{8 + 9 + 10}{2} \right\rfloor = 13$$$ days.

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

For problem A, how can I prove this that if g+b>r, then the best possible approach would be to make the stacks equal?

»
5 days ago, # |
  Vote: I like it 0 Vote: I do not like it

66304111 I am unable to see due to which part my code is exceeding the time limit for Question D, used simple DFS in my approach. Above is the submission link. Thanks in Advance :)

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

How to solve D?

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

My submission is 66433037 and it's completely linear but it's giving me TLE for problem E?? Can anyone help me fix this? I've used a Stack of an object which is 4 generics, and all of my actions are push and pop and writing to an array; not really sure where I could clean this up; is there some Java structure I'm using which is particularly slow?

»
3 days ago, # |
  Vote: I like it 0 Vote: I do not like it

For D, 4 l k al ak Answer is 1. Can someone tell me how l and k are equivalent?

»
3 hours ago, # |
  Vote: I like it 0 Vote: I do not like it

"for (string &pin : a)" what does this mean?