Блог пользователя vovuh

Автор vovuh, история, 6 лет назад, перевод, По-русски

1154A - Восстановление трех чисел

Идея: MikeMirzayanov

Разбор
Решение

1154B - Сделай равными

Идея: MikeMirzayanov

Разбор
Решение

1154C - Котик-гурман

Идея: le.mur

Разбор
Решение

1154D - Идущий робот

Идея: MikeMirzayanov

Разбор
Решение

1154E - Две команды

Идея: MikeMirzayanov

Разбор
Решение

1154F - Магазин лопат

Идея: MikeMirzayanov

Разбор
Решение

1154G - Минимально возможный LCM

Идея: MikeMirzayanov

Разбор
Решение
Разбор задач Codeforces Round 552 (Div. 3)
  • Проголосовать: нравится
  • +38
  • Проголосовать: не нравится

»
6 лет назад, # |
Rev. 2   Проголосовать: нравится +8 Проголосовать: не нравится

Could somebody explain the LCM question clearly ? I could not understand the editorial

»
6 лет назад, # |
  Проголосовать: нравится 0 Проголосовать: не нравится
**O(n)** solution for problem **E**:
  • For every index i, we can maintain two links, one for its left, l[i], and another for its right, r[i], pointing to the first yet undecided student on either sides.
  • When we assign a sub-array to a particular team, m, update only the right link of left of leftmost cell, and the left link of right of rightmost cell, provided these cells are not out of bound.
  • In this manner, we iterate over every index at most two times.

Link of Code

»
6 лет назад, # |
  Проголосовать: нравится +8 Проголосовать: не нравится

E can be solved using Linked List if O(n). you just need to implement the operations. since every student will be chosen exactly once so the time complexity is O(n). code: 52920413

  • »
    »
    6 лет назад, # ^ |
      Проголосовать: нравится 0 Проголосовать: не нравится

    sorting?

    • »
      »
      »
      6 лет назад, # ^ |
      Rev. 2   Проголосовать: нравится 0 Проголосовать: не нравится

      If you use a non-comparing sort algorithm. It could be done in linear time.

      • »
        »
        »
        »
        6 лет назад, # ^ |
          Проголосовать: нравится 0 Проголосовать: не нравится

        I don't know of any non-comparison sort algorithm that works in O(n)time. The best I know of is works in O($$$n\sqrt {log(log(n)}$$$). Can you tell one that works in linear time.

    • »
      »
      »
      6 лет назад, # ^ |
        Проголосовать: нравится 0 Проголосовать: не нравится

      if you read my submission you can see that I didn't sort anything!!!

      • »
        »
        »
        »
        6 лет назад, # ^ |
          Проголосовать: нравится 0 Проголосовать: не нравится

        Can you explain your O(n) solution.

        • »
          »
          »
          »
          »
          6 лет назад, # ^ |
            Проголосовать: нравится 0 Проголосовать: не нравится

          build a Linked List and every time you are removing a student remove him and his k left and his k right students from the Linked List.

          • »
            »
            »
            »
            »
            »
            6 лет назад, # ^ |
              Проголосовать: нравится -6 Проголосовать: не нравится

            But finding the max in remaining list will take O(n). so it will be O(n*n) solution.

            • »
              »
              »
              »
              »
              »
              »
              6 лет назад, # ^ |
                Проголосовать: нравится +4 Проголосовать: не нравится

              You don't need to find that and nor need to sort it. Since all students are between 1 to N and occur exactly once just create an array to point out their location in the linked list.Whenever you remove a student just clear this pointer.Keep a pointer to last maximum player used and decrement this until a player which exists in the linked list!

            • »
              »
              »
              »
              »
              »
              »
              4 года назад, # ^ |
                Проголосовать: нравится 0 Проголосовать: не нравится

              you can see my solution it o(n) and do not use the sorting 90431019

  • »
    »
    6 лет назад, # ^ |
      Проголосовать: нравится +3 Проголосовать: не нравится

    Honestly for the first time I saw the use of linked list making a solution optimal. Very good approach

»
6 лет назад, # |
  Проголосовать: нравится +3 Проголосовать: не нравится

could you tell me about solution you mentioned in first line or provide a link (in problem G).

»
6 лет назад, # |
  Проголосовать: нравится +2 Проголосовать: не нравится

Such a fine editorial !

»
6 лет назад, # |
  Проголосовать: нравится +13 Проголосовать: не нравится

My approach to problem G :

let G = gcd(A,B)
so we can write
A = GR
B = GS
Now, lcm(A,B) = AB/gcd(A,B) = AB/G = GRS
We need to minimize GRS Here G is common factors between two numbers
R is uncommon factors in number A
S is uncommon factors in number B

So we iterate on G (which will be <= 10^7) and for each G we store smallest two multiples of G which are present in our array. Say those multiples are A,B.
Now G(A/G)(B/G) (lets denote it with Z) is our best possible answer with G as gcd.

We find minimum value of Z for all the possible values of G and print corresponding indices as required.

NOTE
The declaration of A = GR and B = GS requires R and S to be coprime (gcd(R,S) = 1).
While finding Z for G , We may have gcd(A,B) > G but it will always be multiple of G (think why). Thus gcd(R,S) = k
Now gcd(A,B) = kG. Even if answer Z stored for G will be G(A/G)(B/G) = Gk2RS ,
when calculation answer Z for kG (G') as gcd we will get Z as G'(A/G')(B/G') = kGRS which is actual answer.

Link to my solution!

»
6 лет назад, # |
  Проголосовать: нравится 0 Проголосовать: не нравится

in problem c: why we take the modules day:=(day+1)%7 ?

  • »
    »
    6 лет назад, # ^ |
      Проголосовать: нравится 0 Проголосовать: не нравится

    That because we want the index of the current day If you just increase it, day will be bigger than the last index of the vector (last index =6) so you want the day to go to the first element after the last one, so you put day%7 Let's say that the day =6 and there is nothing after 6 and you want it to go back to first element, day=(6+1)%7 = 0, and you are again on the index 0 Actually you also can do it like this : If day == 7: day = 0 :))

»
6 лет назад, # |
  Проголосовать: нравится 0 Проголосовать: не нравится

the problem G. As far as I know, $$$\sum_{i \le x}d(i) = O(x \log x)$$$, where $$$d(i)$$$ is the number of divisors of n. So time complexity is equal $$$O(a \log a)$$$, maybe it is equal $$$O(n \log a + a)$$$. Can you point me other $$$O(a \log a)$$$ simple solution?

»
6 лет назад, # |
  Проголосовать: нравится 0 Проголосовать: не нравится

In problem D, in the second example 6 2 1 1 0 0 1 0 1

How is the answer 3? It should be 5 right? Because we can use a battery in first segment so battery decreases by one and accumulator increases by one ==> b=1 , a=2. For the next 2 segments we use 2 accumulators ==> b=1,a=0. The for the fourth segment we use a battery ==> b=0,a=1.For the fifth we use the accumulator!

Can anybody explain how the answer is 3?

  • »
    »
    6 лет назад, # ^ |
      Проголосовать: нравится 0 Проголосовать: не нравится

    It is mentioned in the question that charge of accumulator cannot exceed it's maximum capacity. "If the current segment is exposed to sunlight and the robot goes through it using the battery, the charge of the accumulator increases by one (of course, its charge can't become higher than it's maximum capacity)."
    So when we make first move using battery the charge of accumulator wont increment to 2 as it is already at it's maximum capacity (1).

»
6 лет назад, # |
  Проголосовать: нравится 0 Проголосовать: не нравится

Because it was said about optimization, I would like to give some. First, the provided code find all divisors any number $$$a$$$ in not $$$O(d(a))$$$, because to find one divisor required between $$$\Omega(k)$$$ and $$$O(d(n))$$$, where $$$k$$$ is the amount of unique prime divisors. it needs to be changed by itarative algorithm. Second it's necessary to sort the array a. I thinks both tips will reduce the running time.

»
6 лет назад, # |
  Проголосовать: нравится +4 Проголосовать: не нравится

Solution of E NlogN: https://codeforces.me/contest/1154/submission/52892001 Create segment Tree with = on segment and take maximum on segment Also create segment tree with add on segment and sum on segment And segment tree for calc Ans, with = on segment, and take value in the point

  • »
    »
    6 лет назад, # ^ |
      Проголосовать: нравится 0 Проголосовать: не нравится

    it's very hard way to solve this problem)) We can also solve it using set of pair (value, index) and two array left[n], right[n], which point for unchosen elemnts indexes of their left and right closest unchosen elements.

»
6 лет назад, # |
  Проголосовать: нравится 0 Проголосовать: не нравится

Thanks for hosting the nice round. I don't quite understand the iterator part of the problem E. If I didn't read it wrong, the iterator --it and ++it are supposed to refer to the original index of the students. If it is so, how do we deal with cases when the next students the couch has to choose is out of reach of k steps (i.e. some of them in between have be chosen in previous rounds)?

  • »
    »
    6 лет назад, # ^ |
    Rev. 2   Проголосовать: нравится +1 Проголосовать: не нравится

    The students that already been chosen in between are getting removed from the set at the end of each turn. In the editorial, it is written that we iterate the "add" array (that holds all students that were picked in the current turn) and delete all students in the "add" array from the total students set. Thus, in the next turn, the "students in between" that have already been chosen aren't in the students set anymore.

»
6 лет назад, # |
  Проголосовать: нравится +3 Проголосовать: не нравится

I have changed my opinion about Div3 contests. In this contest the last 3 problem was intresting ans usefull for some experts, imho

»
6 лет назад, # |
Rev. 2   Проголосовать: нравится 0 Проголосовать: не нравится

Can Anyone help me finding bug in my Recursive DP approach on problem F : https://codeforces.me/contest/1154/submission/52944910 Thanks in Advance..

Update: Done !! Recursive DP solution: https://codeforces.me/contest/1154/submission/52951559

»
6 лет назад, # |
  Проголосовать: нравится 0 Проголосовать: не нравится

In problem F editorial can someone please explain this statement

"If shovel A is for free, then we may "swap" shovels A and C, otherwise we may swap shovels A and B, and the answer won't become worse"

»
6 лет назад, # |
  Проголосовать: нравится 0 Проголосовать: не нравится

F can be solved in O(k^2). First, any deals requiring us buy more than k shovels can be immediately discarded since the problem tells us to buy exactly k shovels. We are left with a subset of deals that have us buy between 1 and k shovels. This means that if we have an array deals[] with each element i corresponding to us having to buy i number of shovels we can simply find the maximum free for every index of this array. therefore, we since this array is size k, instead of running through every single deal, we only have to run through these, giving the solution a time complexity of k^2

»
5 лет назад, # |
Rev. 2   Проголосовать: нравится 0 Проголосовать: не нравится

How we can solve problem E if we have to choose a student which the summation of k closest students to the left and right is maximized?

Sorry for my english

»
4 года назад, # |
  Проголосовать: нравится +8 Проголосовать: не нравится

Could anyone explain a "very easy" O(a log a) solution to G?

AFAIK, all the comments here are somewhat related to the official solution in the editorial, but I'd like to know the other one(s). I just can't understand the solutions from the round as is, so thanks in advance!

»
4 года назад, # |
  Проголосовать: нравится 0 Проголосовать: не нравится

Someone please explain my segfault for D https://codeforces.me/contest/1154/submission/95521763

»
3 года назад, # |
  Проголосовать: нравится 0 Проголосовать: не нравится

E can also solve using Doubly linked list and Hashmapmy solution: 127237017

»
17 месяцев назад, # |
  Проголосовать: нравится 0 Проголосовать: не нравится

My NlogN solution works for the G problem.
Code
Idea: Similar to sieve, assume K = gcd() , iterate over all possible K values.

»
10 месяцев назад, # |
  Проголосовать: нравится 0 Проголосовать: не нравится

242484506 I think for the A problem this might be a more comprehensive code if the numbers are more than 3, but still Im a newbie so I might be wrong.