awoo's blog

By awoo, history, 7 months ago, translation, In English

1487A - Arena

Idea: BledDest

Tutorial
Solution (Neon)

1487B - Cat Cycle

Idea: adedalic

Tutorial
Solution (adedalic)

1487C - Minimum Ties

Idea: BledDest

Tutorial
Solution (BledDest)

1487D - Pythagorean Triples

Idea: Neon

Tutorial
Solution (Neon)

1487E - Cheap Dinner

Idea: BledDest

Tutorial
Solution (BledDest)

1487F - Ones

Idea: Neon

Tutorial
Solution (Neon)

1487G - String Counting

Idea: BledDest

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

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

In Problem C, can anyone tell me, for n=4 the output 1 0 0 1 -1 1 is correct or not?? If wrong then how??

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

    ((1 vs 2 , 1 has 3 points now , 2 has zero)) ((1 vs 3 , 1 has 4 points now , 3 has 1 point)) ((1 vs 4 , 1 has 5 points now , 4 has 1 point)) ((2 vs 3 , 2 has 3 points now , 3 still has 1 point)) ((2 vs 4 , 2 still has 3 points , 4 has 4 points now)) ((3 vs 4 , 3 has 4 points now , 4 still has 3 points))

    1 — > 5 points 2 — > 3 points 3 — > 4 points 4 — > 4 points

    There is no ties

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

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?

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

    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.

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

      Thanks for explanation. i tried implementing it but got WA on test case 19. tried many things but could nt get AC.Tried even generating random inputs and comparing with accepted solution but in vain.. any clues for the test case 19??

      https://codeforces.com/contest/1487/submission/108129435

      thanks

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

        Sorry, I'm not familiar with Java. Does the TreeSet structure allow duplicates?

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

          Yes it allows if we add a custom comparator. No issues i will try to generate some more tests and see whats going wrong... thank you

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

        I used priorityQueue in java but got tle on test case 14. Can you please tell if priorityQueue can be used to solve this instead of treeset.

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

        it is true that on using treeset it is giving wrong answer on test case 19. That is quite peculiar.

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

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

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

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.

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

    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

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

      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.

»
7 months ago, # |
Rev. 2   Vote: I like it +21 Vote: I do not like it

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

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

    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!

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

    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$$$.

    • »
      »
      »
      7 months ago, # ^ |
        Vote: I like it +19 Vote: I do not like it

      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|)$$$.

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

        Thanks for your explanation! This looks plausible.

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

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.

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

    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).

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

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.com/contest/1487/submission/107657994

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

    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))

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

In Problem D, why are we checking i*i <= n-1 in the loop?

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

    You mean i*i <= 2*n - 1. We concluded that we need numbers that follow a^2=2b+1. You can think of this equation as squares which are odd and we don't want c or b to exceed n. From c = b+1, and a^2 = 2*b+1 implies a^2 = 2*c-1. Thus max value of a^2 can be 2*n-1. So in the loop we are counting all the a^2 less than 2*n-1 such that a is odd. You can see why it is more than 1 from simple maths.

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

      Understood. Thank You.

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

      How C = B + 1?

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

        It is derived in the editorial. we got b(b+1) = c(c-1) by solving the 2 equations. Clearly, substituting c = b+1 in c(c-1) gives you b(b+1), hence c = b+1 is true.

»
7 months ago, # |
  Vote: I like it +4 Vote: I do not like it

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.

  • »
    »
    7 months ago, # ^ |
    Rev. 2   Vote: I like it +7 Vote: I do not like it

    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.

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

      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?

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

        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.

»
7 months ago, # |
Rev. 3   Vote: I like it -15 Vote: I do not like it

Pythagorean Triples (Problem D)

int a = sqrt(n*2);

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

int ans = a/2;

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

Can anyone explain the " i*i<=2*n-1 " in the Neon's solution of problem C ? How can we get "2*n-1" ? Much obliged!!

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

    Oh , I got it! It seems we use a approximate number to describe the range of "a".. No problems any more...

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

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

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

    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)

»
7 months ago, # |
  Vote: I like it -8 Vote: I do not like it

If anyone needs a math based solution for D, you can have a look at my solution here

»
7 months ago, # |
Rev. 3   Vote: I like it +8 Vote: I do not like it

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

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

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)

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

I can not understand question F. Can anyone help me with it?

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

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 months ago, # ^ |
    Rev. 2   Vote: I like it 0 Vote: I do not like it

    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 months ago, # ^ |
        Vote: I like it 0 Vote: I do not like it

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

      • »
        »
        »
        »
        3 months ago, # ^ |
        Rev. 2   Vote: I like it +1 Vote: I do not like it

        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 months ago, # ^ |
            Vote: I like it +5 Vote: I do not like it

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

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

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

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

In question D. how (C = B + 1)?