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

Автор awoo, история, 4 года назад, По-русски

1487A - Arena

Идея: BledDest

Разбор
Решение (Neon)

1487B - Cat Cycle

Идея: adedalic

Разбор
Решение (adedalic)

1487C - Minimum Ties

Идея: BledDest

Разбор
Решение (BledDest)

1487D - Pythagorean Triples

Идея: Neon

Разбор
Решение (Neon)

1487E - Cheap Dinner

Идея: BledDest

Разбор
Решение (BledDest)

1487F - Ones

Идея: Neon

Разбор
Решение (Neon)

1487G - String Counting

Идея: BledDest

Разбор
Решение (BledDest)
  • Проголосовать: нравится
  • +83
  • Проголосовать: не нравится

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

The time complexity of the editorial of problem E seems to be O(n*m*logm), which seems more than 1E10. How does this solution get AC?

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

    It's just $$$O((n+m) \log n)$$$ since, for each consecutive pair of types of dishes we build a data structure for the first type (in $$$O(n \log n)$$$), and for every dish of the second type, remove the forbidden elements ($$$O(m \log n)$$$ over all dishes), query the minimum ($$$O(n \log n)$$$ for all dishes in total), and insert back the removed elements.

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

I've uploaded my post-contest stream (where I explain A-E) to Youtube with timestamps: https://youtu.be/bx35l4tx_xk

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

In problem E, I have a bit of a different solution to the DP optimization. From the previous course, we sort all dp values from low to high. Then for a certain new dish, we want to know the first option in the sorted list that isn't forbidden. First we assume every dish will pick the first option, and we put the option they choose in an array (initially all zeroes). Then go through all bad edges of the first dp option and increment the lowest possible dp option for each bad dish, then do the same for the bad edges of the second option, etc. Then it is simple to calculate the new dp values. This algorithm takes only $$$O(n \cdot log(n) +m)$$$, because it goes through each edge once, and does a sort on the dp values.

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

    My Submission

    I have done exactly the same it doesn't seem to work and is giving wrong answer on test case 16.

    I have created 3 dp vectors of pairs (strg1,strg2,strg3) which store minimum cost to have till 2nd 3rd and 4th course of meals.

    If we can't have a particular meal it's cost is marked as -1.

    Before starting to calculate the next dp array we sort the previous dp table and our counter starts from the 1st val which isn't -1.

    Then for a particular meal we increment the counter till a compatible meal is found and again reset the value to the minimum for the next meal.

    Time complexity is (n*logn +m).

    Please help me find my error or a test case which my code fails

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

      Accepted

      Sorry for the trouble It is working actually i forgot to check the index of sorted ones i.e. dish[i].second i was checking ith one.

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

My solution for F:

Supposing $$$m_i=111...1$$$($$$i$$$ digits). And you can think of this problem as: make $$$x=x+m_i/ x-m_i$$$with price $$$i$$$ and let $$$x=0$$$ with minimal price.

Let $$$dp_{y,x}$$$ mean after you plus/minus all $$$m_i (i > y)$$$ the minimal price with value $$$x$$$ now. And we can use map/unordered_map and Bigint(I write it myself) for $$$dp_y$$$. Initially $$$dp_{y+1,n}=0$$$

For every $$$x$$$ in $$$dp_{y,x}$$$ there are only at most two options for $$$x$$$ in $$$dp_{y-1,x}$$$. Assuming $$$x$$$ is nonpositive those two options $$$x$$$ are $$$x-km_y$$$ and $$$x-(k+1)m_y$$$ where $$$x-km_y$$$ is nonnegative and $$$x-(k+1)m_y$$$ is negative. Other $$$|x-km_y| \geq m_y$$$ mean you can always minus/plus a more $$$m_y$$$ and save much more price.

There seems to be at most $$$2^{50}$$$ different $$$x$$$ in $$$dp_y$$$, but we can see $$$m_i \mod m_{i-1} = 1$$$. It means plus/minus one more $$$m_y$$$ will at most result in $$$1$$$ difference in $$$dp_{y-1}$$$. So there are at most $$$O(|n|)$$$ different $$$x$$$ in $$$dp_y$$$.

Iterate $$$y$$$ from $$$|x|+1$$$ to $$$1$$$ $$$(O(|n|))$$$, enumere $$$x$$$ in $$$dp_y$$$ and update $$$dp_{y-1}$$$ $$$(O(|n|))$$$, with Bigint calculation complexity $$$O(|n|)$$$ and map $$$O(log|n|)$$$, the total complexity as I think is $$$O(|n|^3log|n|)$$$

my code

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

    OMG, I actuallly thought the complexity was $$$O(2 ^ {|n|})$$$ during the contest.

    I was too worried to code it. :(

    After all, $$$m_i\mod m_{i-1}=1$$$ seems the key to the solution.

    What a nice job you did,bro!

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

    Seemingly you claimed that if $$$|x| \geq m_y$$$, $$$m_y$$$ should be in the optimal solution. Why?

    Also, could you explain why is there $$$O(|n|)$$$ different $$$x$$$ in $$$\mathrm{dp}_y$$$? I cant see why it is true given $$$m_i \bmod m_{i + 1} = 1$$$.

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

      Well, every $$$m_y$$$ should be used less than $$$10$$$ times (in fact maybe even less), otherwise you can always change $$$10m_y$$$($$$10y$$$digits) into $$$m_{y+1}-m_1$$$($$$y+2$$$ digits). If your $$$|x| \geq m_y$$$ and don't use $$$m_y$$$ to reduce it. Considering $$$\sum_{i=1}^{y-1} 9m_i < \sum_{i=1}^{y-1}10^{i+1} = m_y$$$, Some of $$$m_i(i<y)$$$ must be used at least $$$10$$$ times to make $$$x$$$ become $$$0$$$ and you can always make it better.

      I claim that for all $$$x$$$ in $$$dp_y$$$ the value of $$$x \mod m_{y}$$$ are in exactly one intervals $$$[a,b]$$$ with length of $$$O(|n|)$$$. ($$$[a,m_{y}-1] \cup [0,b]$$$ is also seen as an interval in case of $$$\mod m_{y})$$$

      Initally $$$x$$$ has only $$$1$$$ possible value. Ok of course.

      Since $$$x\pm km_{y}$$$ doesn't change $$$x \mod m_{y}$$$, for all $$$x$$$ in $$$dp_{y-1}$$$ the value of $$$x \mod m_{y}$$$ are in $$$[a,b]$$$ too.

      Considering all $$$x$$$ in $$$dp_{y-1}$$$ must satisfy $$$|x|<m_y$$$, the possible value of $$$x$$$ is in only two intervals:$$$[a-m_y,b-m_y] \cup [a,b]$$$.

      $$$a-m_y=a-10m_{y-1}-1\equiv a-1 (\mod m_{y-1})$$$. So for all x in $$$dp_{y-1}$$$,$$$x \mod m_{y-1}$$$ is in $$$[a-1,b-1] \cup [a,b] =[a-1,b]$$$, only $$$1$$$ longer than $$$[a,b]$$$. So $$$[a,b]$$$'s length is $$$O(|n|)$$$.

      Since that the possible number of different $$$x$$$:$$$[a-m_y,b-m_y] \cup [a,b]$$$ is also $$$O(|n|)$$$.

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

What does the array dp mean in the solution code? Does dp[0] stands for f and dp[1] stands for g? Obviously the meaning of dp does not directly correspond with the tutorial.

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

    As I say in the last paragraph of the editorial, the third version of dynamic programming ($$$g$$$) can be used not only to calculate the number of strings which violate the constraint on a pair of characters, but also the number of strings which don't violate any constraints and which violate the constraints on one character; so, the only dynamic programming in this solution actually stands for $$$g$$$ (though it stores only two last layers of it to optimize memory usage).

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

in problem D , i searched it in Wikipedia "a = k(2xy) b = k(x^2 — y^2) c = k(x^2 + y^2)" then i calculated that: x = y + 1. So my idea is using a loop for from 1 to sqrt(n) and check. But it's wrong. Can anyone help me. This is my submission: https://codeforces.me/contest/1487/submission/107657994

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

    you just need to add cnt by 1 during the loop, because when you can keep running the loop, it means you find one answer for the question, not n/(y*y + (y+1)*(y+1))

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

For Problem G, is there any explanation for why the optimizations described in the solution work? Why do $$$l$$$ and $$$m$$$ only have 3 states and why do we only need to run DP once? I'm still kind of confused over these.

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

    My English is very poor...so if I make grammar mistakes, please forgive me. TAT

    The reason that $$$l$$$ and $$$m$$$ only have 3 states is we only need to know whether the last two characters are the two we fixed. If so, we can choose the rest 25 characters as the next one then update the value of $$$j$$$ or $$$k$$$. If not, we only need to choose the rest 25 characters as the next one, without updating the value of $$$j$$$ or $$$k$$$.

    The reason that why do we only need to run DP once is all the characters are essentially the same. No matter which two characters you fix, your DP processes are all the same.

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

      Ah the first part makes sense. I'm still not sure about the second part, mainly that the DP processes are all the same. How can you find the values for each pair of characters given just one DP?

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

        To get the answer, we calculate the number of strings of length $$$n$$$ that don't contain palindromes of odd length greater than $$$1$$$.

        Then we iterator all the $$$26$$$ characters and subtract the number of strings that violate the limit of at least one character(suppose this character is $$$c$$$, then the number is $$$\sum_{j=0}^n\sum_{k=limit_c+1}^ndp_{n,j,k,*,*}$$$ ) from the answer.

        Then we iterator all the $$$26$$$ characters and add the number of strings that violate the limit of at least two characters(suppose these two characters are $$$c1,c2$$$, then the number is $$$\sum_{j=limit_{c1}+1}^n\sum_{k=limit_{c2}+1}^ndp_{n,j,k,*,*}$$$ ) to the answer. You can think of the whole process as an inclusion-exclusion.

»
4 года назад, # |
Rev. 3   Проголосовать: нравится -15 Проголосовать: не нравится

Pythagorean Triples (Problem D)

int a = sqrt(n*2);

if( a%2 == 0)a--;

int ans = a/2;

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

BledDest

Можешь, пожалуйста, объяснить следующий момент в разборе задачи G:

"Чтобы обработать ограничения на количество символов каждого типа, воспользуемся формулой включений-исключений. Так как каждое значение Сi > n / 2, максимум два символа могут нарушить ограничение ...".

Какое ограничение могут нарушить максимум 2 символа? Почему именно 2 символа?

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

In problem B can anyone tell what are the collision points for n = 7.It is supposed to be collide after n//2 steps but for n = 7 ,collision happened only at pt 3 and 1 and after that it is not colliding.Please explain where m i doing wrong

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

    collision happened at 4 -> 1 -> 5 -> 2 -> ... {7,1},{6,2},{5,3},!{4,5},{3,6},{2,7},!{1,2},{7,3},{6,4},!{5,6},{4,7},{3,1},{2,3} it seems that the collision happened on pt (1+t*(n-1)/2)

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

In Neon's editorial of problem F, why is this condition met carry>=(carry+cp)/10? Thanks!

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

Pfft, that solution for B was way too complicated, I came up with much simpler solution. Obviously, the solution is (1 - n % 2) * (((k - 1) % n) + 1) + (n % 2) * (((((k - 1) % (n * (n / 2))) / (n / 2)) / 2 + (((k - 1) % (n * (n / 2))) % (n / 2)) + ((((k - 1) % (n * (n / 2))) / (n / 2)) % 2) * (n / 2 + 1)) % n + 1)

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

Can any one help me: Why 3n(n-1)/2 mod n = n / 2. I tried to prove it but I think 3n mod n = 0 so 3n(n-1)/2 also 0...

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

    Of course, 3 * n mod n = 0 and that's why 3 * n * (n — 1) mod n = 0. But after dividing 3 * n * (n — 1) by 2, this condition is not necessarily satisfied (for example, with n = 2: 3 * 2 * 1 = 6, 6 mod 2 = 0. But (3 * 2 * 1) / 2 = 3, 3 mod 2 != 0). Also statement 3 * n * (n — 1) / 2 mod n = n / 2 is correct if and only if n is even (if it is odd left part is integer and right is not). proof: 3 * n * (n — 1) / 2 mod n = n / 2 <=> 3 * n * (n — 1) / 2 — n / 2 mod n = 0 <=> (3 * n * n — 4 * n) / 2 mod n = 0 <=> n * (3 * n — 4) / 2 mod n = 0 — this is correct because 3 * n — 4 is even (because n is even) and (3 * n — 4) / 2 is integer.

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

      Yay, your reply is very helpful. I also have another question: is there any ways to find result: n / 2 without proving...

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

        Yes. You can do it in following way: We want to find a number from 0 to n — 1 inclusive such that 3 * n * (n — 1) / 2 mod n = x. 3 * n * (n — 1) / 2 mod n = x <=> 3 * n * (n — 1) / 2 — x mod n = 0 <=> 3 * n * n — 3 * n — 2 * x mod 2 * n = 0. It means (1) that 3 * n + 2 * x mod 2 * n = 0 (because 3 * n * n mod 2 * n = 0, as n is even and 0 mod 2 * n = 0) and (2) 2 * x mod n = 0 (because 3 * n * n — 3 * n mod n = 0 and 0 mod n = 0). It follows from (2) that x = 0 or x = n / 2 (because 0 <= x < n). If x = 0, (1) sometimes is false (for example, if n = 2). If x = n / 2, 3 * n + 2 * x = 4 * n, 4 * n mod 2 * n = 0. So the only solution to this problem is x = n / 2.

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

          If I could upvote more than 100 times, I will do it for your helpful reply :D. Thanks sir

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

Why solution in B do k-- ??? I cant understand

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

my implementation for c using 2D array

https://codeforces.me/contest/1487/submission/156393407

**if I choose v[i][j] to be equal to 3 then I had to make v[j][i]=0, my transition:

fisrt I make each of (0, 0) (1, 1) (2, 2).... is equal to 0...the I start to filling with 3 and 0 3 for winner and 0 for loser**

0'BASED index

if n=5;

(1, 0)->(2, 1)->(3, 2)->(4, 3)->(0, 4) =3

(0, 1)->(1, 2)->(2, 3)->(3, 4)->(4, 0) =0

(2, 0)->(3, 1)->(4, 2)->(0, 3)->(1, 4) =0

(0, 2)->(1, 3)->(2, 4)->(3, 0)->(4, 1) =3

and if n is even we can stop when the remainning cells that doesnot filled is equal to n I know that my solution is complicated but I want to share wit others..sorry for my bad english.. ****