awoo's blog

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

1622A - Construct a Rectangle

Idea: BledDest

Tutorial
Solution (Neon)

1622B - Berland Music

Idea: adedalic

Tutorial
Solution (awoo)

1622C - Set or Decrease

Idea: adedalic

Tutorial
Solution (adedalic)

1622D - Shuffle

Idea: BledDest

Tutorial
Solution (BledDest)

1622E - Math Test

Idea: BledDest

Tutorial
Solution (Neon)

1622F - Quadratic Set

Idea: Neon

Tutorial
Solution (Neon)
 
 
 
 
  • Vote: I like it
  • +80
  • Vote: I do not like it

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

How can we solve Problem D in O(n)?

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

    Compute the location of each “1” in the string and for each of them, count the number of strings where it is the first “1” that is moved. This is just the difference of two binomial coefficients.

    EDIT: here is a relatively clean example solution

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

      I had same solution, but I precompute all binomials in O(n^2). Thanks for your code! I think I learned something!

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

Is someone solved C using the ternary search on final value of minimum element and cost of comparison equals no of moves required for that final value?

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

Why do we need random numbers for primes in problem F?

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

    For hashing. Xor have this property that if you xor same value even number of times, then you get 0. We want to check if in 1! 2! ... n! all primes exists even number of times.

    I know this trick from Zobrist hashing. Not sure how it is call in CP.

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

Here I've got a solution to problem F without hashing or any other randomicity. Though I don't know how to prove it, it seems to be correct. Due to the limitation of the space, I'll only put the conclusions here.

First if $$$ n = 1 $$$, the answer subset include only $$$ 1 $$$. Now we assume $$$ n \not = 1 $$$.

If $$$ n \equiv 1 \pmod 2 $$$, we check if $$$ n \cdot (\frac{n - 1}{2} - 1) $$$ is a square number. If so, the answer subset is all numbers from $$$ 1 $$$ to $$$ n $$$ except $$$ \frac{n - 1}{2} - 2 $$$ and $$$ n - 2 $$$.

If $$$ n \cdot (\frac{n - 1}{2} - 1) $$$ is not a square number, $$$ n $$$ will not be in the answer subset, and we let $$$ n \leftarrow n - 1 $$$ and continue our discussion.

So we now have $$$ n \equiv 0 \pmod 2 $$$. Let $$$ m = \frac{n}{2} $$$.

If $$$ m \equiv 0 \pmod 2 $$$, the answer is $$$ 1, \cdots, n $$$ except $$$ m $$$.

Else we have $$$ m \equiv 1 \pmod 2 $$$. Check if $$$ \frac{m + 1}{2} $$$ is a square number. If so, answer is $$$ 1, \cdots, n $$$ except $$$ m + 1 $$$.

We also check if $$$ m = 9 $$$. If $$$ m = 9 $$$, answer is $$$ 1, \cdots, n $$$ except $$$ 7 $$$.

In the case that neither $$$ \frac{m + 1}{2} $$$ is a square number nor $$$ m = 9 $$$, answer is $$$ 1, \cdots, n $$$ except $$$ 2 $$$ and $$$ m $$$.

Obviously there must be $$$ \text{answer's size} \geqslant n - 3 $$$, for there will be at most $$$ 2 $$$ absent number for even $$$ n $$$ and one more for odd $$$ n $$$.

Check 140891764 for code. I use this OEIS sequence to reach some of the same conclusion as in the official solution.

As you see, this solution is partly based on the official solution, but to construct the subset by just discussing about $$$ n $$$, instead of using hashing.

  • »
    »
    13 months ago, # ^ |
      Vote: I like it -10 Vote: I do not like it

    How to proof this constructed method?

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

    This solution is actually wrong; if we replace the special $$$m=9$$$ with any $$$m=a^2$$$ such that $$$a$$$ is a solution to the pell equation $$$a^2-2b^2=1$$$, then removing only $$$(m-2)!$$$ works. Here $$$m$$$ is obviously odd.

    Everything else does look fine though.

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

I don't know why I printed max(result, 1) and ruined my submission in problem D :'(

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

In problem C i'm facing overflow issue. Can someone please help to find out where is my code overflowing i have verified the calculations and data type multiple times. 140977108

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

Problem B: "The general way to prove it is to show that if the order has any inversions, we can always fix the leftmost of them (swap two adjacent values), and the cost doesn't increase." Can anyone give a proof on that?

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

Can someone please explain D, tutorial explanation is somewhat tough to understand.

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

For problem D test case 23 has answer 0.How is 0 answer possible given we always have the string s?

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

    The answer is not 0, but 0 mod P, which means that the actual integer answer is a multiple of P = 998244353.

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

For problem F, solution part: Why do we have to include rf[x]!=x in if (rf.count(x) && rf[x] != x) return {rf[x], i};?

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

Is there any efficient ternary search algorithm???? I have used ternary search on 1622C - Set or Decrease. It passed the pretest but give a wrong verdict on system test case. Though, I have also checked +-2 range from the expected range manually. Here is my solution 140786085

Please help me.

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

I tried using binary search for C. But the solution gets TLE. I can't find the reason for my mistake. Can somebody help?

Here's the link to my submission 141226085

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

    at some point lo and hi becomes equal and ur loop becomes infinite loop. try lo<hi in the loop condition. Also don't use i<=mid for inner loop condition, rather use i<=mid && i<n for better performance.

    Here's the link 141232531

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

How sometimes answer for D can be 0. Could you please explain?

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

Can someone explain the accurateFloor function in problem C? why can't we simply write c/d — 1 when c<0?

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

For problem D, can't we use the inclusion/exclusion principle to count duplicate occurrences?
Here is my solution, but it's giving WA in test 10.
These are the steps of my solution:
1. Compute all the intervals [i,j] such that the number of 1 in the interval is k. Let s1, s2, s3,...sk be the intervals.
2. Number of common combinations between s1, s2, s3,...si is the arrangement of one and zero that are present in the intersection of these. Then use inclusion and exclusion principle to get union of these.

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

    Consider this testcase

    6 3
    111011
    

    We need to permute substrings containing exactly three $$$1s$$$. Since the entire string contains a single $$$0$$$, the uniqueness can be identified by the position of $$$0$$$. Since $$$[1110]$$$ contains exactly three $$$1s$$$, we can place a $$$0$$$ at any of the first four spots. Since $$$[1011]$$$ also contains exactly three $$$1s$$$, we can place a $$$0$$$ at the last two spots too. Hence, there are 6 possible configurations.

    However, your code outputs 7.

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

      Thanks for providing a test case. Can you explain to me why my logic is wrong?

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

        The problem involves substrings instead of subsequences. You can prove by drawing or otherwise, that only adding contributions from k-one substrings and removing those of (k-1)-one substrings is optimal, rather than c(k) - c(k-1) + c(k-2) - c(k-3)....

        You would also have to make sure that all the intervals must follow (l == 0 || v[l - 1] == 1) && (r == n - 1 || v[r + 1] == 1), as its only feasible to stop moving left or right when we come across a 1 or else we will be recounting things.

        code for above approach

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

          Understood it. Thanks for providing the explanation.

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

For Problem D, I think there's a pretty manageable way of iterating over the substrings of interest and keeping track of how shuffling different substrings may result in the same string.

Iterating from left to right, say we have processed the substring $$$[l_1, r_1]$$$ and we're now processing $$$[l_2, r_2]$$$. $$$[l_2, r_2]$$$ has a part that overlaps with $$$[l_1, r_1]$$$ and a part that doesn't. Shuffling $$$[l_2, r_2]$$$ will result in a duplicate string if the part that doesn't overlap remains the same as the original.

If there are $$$c$$$ ones in the overlapping interval, then there are $$${{r_1 - l_2 + 1} \choose c}$$$ duplicate strings that result from shuffling $$$[l_2, r_2]$$$.

This approach can reach $$$O(n)$$$.

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

We can solve problem-D like this:

  1. Store no. of 1's in prefix a count array, say cnt.
  2. For each index i, find the next index r such that cnt[r]-cnt[i-1]==k. Also maintain prev_r which stores r of i-1.
  3. Now, 
    if r<=prev_r: 
       continue, 
    else: 
       answer+= C(i, r) - C(i, prev_r) 
    prev_r= r
    

where C(i,j)= no. of combs. of elements between i and j.

So C(i, prev_r) subtracts the common part between the range of current and the prev index.

Check for corner cases too, here is my solution

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

Excuse Me, I want to ask something. Why did the Solution of Problem C return the floor not the ceiling of the division? I really confuse, can anyone help me?

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

    As mentioned in the tutorial, for i set values, we are searching for corresponding x value. so equation becomes: (a is 0 indexed) a[0] — x <= (k + a[0] — prefixsum[n — i — 1])/(i + 1) now a[0] and x are integers, so we take floor of RHS (We cannot take celing since it'll mess up the inequality) I guess looking at this equation might help..

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

Solved problem B in O(N) complexity.

  • Calculated the number of zeros in s. Let it be cz.
  • Find all the indices which was disliked but has rank greater than cz.
  • Find all the indices which was liked but has rank lower than cz.
  • Swap the corresponding indices.

My solution code

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

    I have a doubt , how can the difference be minimum in this approach ? please explain.

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

Can anyone tell me why we can ignore the illegal permutations in problem E? thanks a lot!