awoo's blog

By awoo, history, 5 years ago, translation, In English

1288A - Deadline

Idea: adedalic

Tutorial
Solution (adedalic)

1288B - Yet Another Meme Problem

Idea: Roms

Tutorial
Solution (Roms)

1288C - Two Arrays

Idea: BledDest

Tutorial
Solution (Roms)

1288D - Minimax Problem

Idea: BledDest

Tutorial
Solution (BledDest)

1288E - Messenger Simulator

Idea: Neon

Tutorial
Solution 1 (pikmike)
Solution 2 (pikmike)
Solution 3 (pikmike)

1288F - Red-Blue Graph

Idea: BledDest

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

| Write comment?
»
5 years ago, # |
  Vote: I like it +28 Vote: I do not like it

Thanks for the nice editorial.

And , Actually am not able to understand how combinatorics is right to use and how it is applied for the Problem C . And also some have solved it using dp , i dont understand that approach either

So, can anyone please help me with my above stated problem.

Thanks in advance

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

    combinatorics — if you have n identical object and r different object number of ways of arrangement is (n+r-1) C r-1 or n+r-1 C n

    now in these question n=2*m and r=n and now total number of distribution is (n+2m-1)c(2m) and each distribution can be arranged in ascending order in one way (assuming all boxes identical for one movement and then which ever combination is there u have one way of arrange them in ascending order)

  • »
    »
    5 years ago, # ^ |
    Rev. 3   Vote: I like it +32 Vote: I do not like it

    For combinatorics solution of C, we need to find number of non decreasing sequences of length $$$2m$$$ with values between $$$1$$$ and $$$n$$$.

    We can convert this problem as follows ( since we want non decreasing values ).

    Let $$$x_{i}$$$ denote number of times we take value $$$i$$$ in our sequence. Clearly $$$x_{i} \ge 0$$$. Also,

    $$$ x_1 + x_2 + x_3 + ... + x_{n-1} + x_{n} = 2m $$$.

    The number of solutions to this is given by, $$$ \binom{n+2m-1}{n-1} = \dfrac{(n+2m-1)!}{(n-1)!(2m)!}$$$. You can read more about Stars and Bars.

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

      Those who haven't understood after reading Stars and Bars what should they consider star and what should they consider bar they can just assume all the position of an array as balls (so we got 2*m ball, right? ) and list all the numbers from 1 to n (inclusive) and think every number as container or a box.

      So when you are putting a ball in a box the ball will automatically get the number of the box on it. And you can put as many ball as you want in a box.

      so every time we will use all balls and will get different combination from it. And obviously there will be a single permutation for a combination for which after collecting all the balls from the containers(or box!) we can sort them in non-descending order according to their number. :D

      Pretty easy, huh? :3

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

        problem i am facing is there might be two sequence whose sorted arrangement produce same result so how can we consider both the sequence as both are same only because we are interested in final sorted sequence. it would be very helpful if someone can explain me this

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

      how can we directly use x1+x2+x3+...+x(n−1)+xn=2m or Stars and bars Theorem here ? isnt there is a condition that the array must be non decreasing sequences ?

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

        The question given in $$$C$$$ is equivalent to "choosing non decreasing sequence of length $$$2m$$$ with values between $$$1$$$ and $$$n$$$".

        Proof: If we obtain two sequences, $$$a,b$$$ that satisfy conditions given in question $$$C$$$, then we have $$$a_1 <= a_2 <= .... <= a_{m-1} <= a_{m} <= b_{m} <= b_{m-1} <= .... <= b_2 <= b_1$$$.

        Conversely, if we have a sequence of $$$2m$$$ elements, $$$s_1 <= s_2 <= .... <= s_{2m-1} <= s_{2m}$$$, then we can make arrays $$$a,b$$$ as $$$a=(s_1, s_2, ..., s_{m})$$$ and $$$b=(s_{m+1}, s_{m+2}, ..., s_{2m-1}, s_{2m})$$$.

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

          Yeah i understand that but my question was as my array must be non decreasing sequences how can i directly use Stars and bars theorm ? isnt that theorm is just randomly choosing repetedly element ?

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

            Oh. Choosing a non decreasing sequence of length $$$L$$$ with elements taking $$$D$$$ distinct values can be modeled as follows.

            Sort the $$$D$$$ distinct values. Number them from $$$1$$$ to $$$D$$$. Let $$$x_i$$$ denote number of occurrences of $$$ith$$$ element that we take in the sequence. Then we need $$$x_i >= 0$$$, and

            $$$ x_1 + x_2 + .... + x_{D-1} + x_{D} = L $$$

            Think of it like this, there are $$$L$$$ blank balls in a row, and you need to make $$$D-1$$$ vertical separators between them, and then in $$$ith$$$ segment write value of $$$ith$$$ number on all the balls, this gives a sequence that we require. Two separators can coincide too, so that means we don't take some element at all.

    • »
      »
      »
      4 years ago, # ^ |
        Vote: I like it 0 Vote: I do not like it

      OMG.Thank you so much.. I was not knowing about this Stars and Bars principle.After reading from wikipedia i got it.Thank you so much

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

    Have a look here for Combination with Repitition: https://sites.math.northwestern.edu/~mlerma/courses/cs310-05s/notes/dm-gcomb.

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

    Consider the non-descending sequence given in the editorial. It's a sequence of $$$2m$$$ elements, all between $$$1$$$ and $$$n$$$. The number of ways of building a sequence like that is the answer for our problem. We just need to figure a way of calculating it. So, basically, picking $$$r$$$ elements from a set of $$$n$$$ elements (with replacement, because we can select the same number more than once).

    Imagine that, since you are replacing elements, you are "adding" new ones to the set. Each of the $$$r$$$ elements we pick is replaced, except for the last one. In total, $$$r - 1$$$ replacements. The formula is $$${n + r - 1 \choose r}$$$. In this case, $$$r = 2m$$$ (amount of elements to pick). Finally, $$${n + 2m - 1 \choose 2m}$$$.

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

      Your approach of thinking for this problem of combination with replacement seems interesting. However I dont fully understand why that works. Or you can say I'm not able to get this intuition. Can you explain more?

      I only know how to understand this using the stars and bars method.

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

        The problems are equivalent! I picked $$$2m$$$ elements from a set of $$$n$$$, with repetitions, right? That's because I considered the sequence of length $$$2m$$$. What if I only considered the amount of times I picked an element, i.e their frequency?

        Let's call $$$freq_i$$$ the amount of times I picked the $$$i$$$-th element. It's easy to see that $$$freq_1 + freq_2 + ... + freq_n = 2m$$$. Because $$$2m$$$ is the length of the first sequence.

        Now, the question is: how many solutions does this equation have? Any solution can be arranged into two arrays that satisfy the original problem statement. The stars and bars problem will give us the answer: amount of ways to distribute $$$r$$$ identical objects among $$$n$$$ distinct containers. Here, $$$r = 2m$$$. Finally, the formula is (still) $$$n + 2m - 1 \choose{2m}$$$!

        Take a look at this comment. The link he posted shows some other problems that are equivalent to stars and bars.

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

          Thank you for the reply.

          I am sorry I should have asked my question properly. I already understand how the stars and bars problem relates to solving the problem of combination with replacement.

          What I am interested in understand is how you used the intuition of "adding new elements to the set" to consider replacement. i.e. your lines -- Imagine that, since you are replacing elements, you are "adding" new ones to the set. Each of the r elements we pick is replaced, except for the last one. In total, r-1 replacements.

          So how does this "adding to the set" thing works to fulfil the replacement issue?

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

            This is just a trick to easily remember the formula. It isn't necessarily right. You can find more explanation here.

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

      I can't understand the number n + 2m — 1, and I don't know r — 1 replacement.why except for the last one? why we can see selection as replacement? Can you explain more clear?Thank you very much.I see it so long but I can't understand.

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

        The number of ways to pick $$$2m$$$ elements from a set on $$$n$$$ elements with repetitions (also called replacement) is given by the closed formula I mentioned above. I recommend that you take a look in this Wikipedia page.

        Just explaining my view of it: Replacing an element is adding a new one to the set. We will replace every element we pick, with exception of the last one, because we will have finished the process then. This gives us $$$n + r - 1$$$ choose $$$r$$$. Substitute $$$r$$$ for $$$2m$$$ and you get the formula.

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

    DP Approach:
    Consider 2m places to be filled by numbers of range 1 to n and suppose we are at index i and value at (i+1)th index is x so the max value of all the indices less than/equal to i will be x.

    Now, we have two cases
    1) Assign value of x to ith index and find answer for i-1 places for values in range ( 1 , x ).
    2) Find answer for i places for values in range ( 1 , x-1 ).

    Base cases:
    1) when number of places left is 1 answer will be n (fill it with values from 1 to n).
    2) when max value can be 1 answer will be 1 (fill all places with 1).

    Recurrence relation num(x,m) = num(x,m-1) + num(x-1,m)

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

      Can you ai<=bi;

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

        Because the question is equivalent to finding a non-decreasing sequence of length 2m where each value lies in [1, n]. So, instead of thinking in terms of two arrays A and B, we now only think in term of a single array and this single array has to satisfy two conditions. i.e. it should be of length 2m and has every value in the range [1, n].

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

          Can you please explain how is the question equivalent to finding a non-decreasing sequence of length 2m?

    • »
      »
      »
      4 years ago, # ^ |
        Vote: I like it 0 Vote: I do not like it

      Why is this giving TLE

»
5 years ago, # |
  Vote: I like it +4 Vote: I do not like it

I think i have a better solution for A

If not, please let me know

https://pastebin.com/SjQMG52P

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

    can you explain the approach behind it?

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

    So nice solution. And here is the explanation.

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

      How do you get rid of the ceil function?

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

        x + ceil(d/(x+1)) can also be written as ceil(x + (d/(x+1))). For ceil(x + (d/(x+1))) <=n, x + (d/(x+1)) <=n holds.

        Very Nice solution.

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

        I think it doesn't matter if it's ceil or floor as long as there's a solution for x. If there's a solution for x, both ceil and floor will work for this problem.

»
5 years ago, # |
  Vote: I like it +16 Vote: I do not like it

Persistent segtree in E is mle.

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

    Unfortunate

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

    I managed to fit persistent segment tree (PST) in ML

    In my solution there are at most 6*10^5 update queries to PST. Moreover, every query creates O(log) new vertexes and one vertex need 3 integers (it's 12 byte). So, PTS requires 6*10^5*20*12 = 144 MB < 256 MB

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

      I dynamically allocated memory hence each node had an integer and two pointers. But that exceeded memory. Probably i should statically allocate all memory next time.

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

How to solve D using graph?? (There is graph tag in question!!) if anyone could provide any submission link using graphs that would be very helpful for me,thanks in advance.

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

    Create a graph where each node is a number from $$$0$$$ to $$$(2^m) - 1$$$. There is an edge between $$$u$$$ and $$$v$$$ ($$$u \leq v$$$) if and only if (u & v) == u. This way, you can go from one bitmask to every other comprehended inside it.

    When binary searching the answer, for each array on the input you should compute two bitmasks. Let's call them $$$G_{msk}$$$ and $$$L_{msk}$$$. If you are currently trying answer $$$x$$$, then mark elements greater than or equal to $$$x$$$ with a $$$1$$$ on $$$G_{msk}$$$, otherwise $$$0$$$. $$$L_{msk}$$$ is the complement of $$$G_{msk}$$$. That means L_msk = ~(G_msk).

    Start a dfs from every $$$G_{msk}$$$ and check if any of it's neighbors is among the $$$L_{msk}$$$. If it is, then you found a pair for current $$$x$$$ as answer. Notice that it means you found two bitmasks such that bitwise or is (1<<m) - 1.

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

I am having slow rankups XD.

»
5 years ago, # |
  Vote: I like it +14 Vote: I do not like it

Very nice solution for problem D (fast and compact one). Also very hard to find out such a nice solution like this. Thank for tough problem and its brilliant solution. This helps me learn more about bitmask which I didnt know too much before. Thanks again!

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

Can someone explain dp approach in problem C

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

    Consider the sequence given in the editorial. I'll call it a "good sequence".

    The answer to the problem is the number of ways of building it. Memoize the amount of good sequences you can build from a position $$$pos$$$ given that the last number you used was $$$last$$$. These are gonna be the two states.

    Now, we transition through the states: for every number from $$$last$$$ to $$$n$$$, try to use it as the $$$pos$$$-th element of the good sequence.

    Submission: 68825881

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

      Would you please tell us(people like me!) how you have created newline in your comment :D

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

        I did (Shift + Enter), but double Enter might do the trick aswell.

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

      In your code..I have seen if pos==m then return 1..let say,if I have 8 in pos-1 then at the last position i have choices of (n-8) then why to return 1

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

        First of all, I use recursive approaches for dynamic programming problems because it's more intuitive to me.

        Ok, so you gotta understand that if(pos == m) return 1 is a base case, where I need to stop making recursive calls.

        The meaning of it is: If the current position for which the function is called is equal to $$$m$$$ — the last position — it means that I was able to build a valid answer until this point. So I need to return $$$1$$$ to increment the answer count.

        This is a very common approach for problems that ask you to compute the amount of ways to do something. I hope I was able to explain it clearly, but you should take a look at AtCoder Educational DP Contest.

        I'm not a Sports Programming master, so you'll probably learn more by doing it!

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

      nvm i got it

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

not able to implement (n+2*m-1) C (2m)

please help me how to do it

i tried using tgamma function also as

tgamma(n)=n-1!

»
5 years ago, # |
  Vote: I like it +20 Vote: I do not like it

E can be solved easily using policy based data structure indexed_set and inserting the elements as a pair of negative timestamp of insertion and the index of friend. Every time you remove a friend and insert it at the top, store the max position of current friend, which can be calculated using order_of_key function and adjust the time as -(n+i) where i denotes the ith message, after all the operations loop over all the friends and find the current positions of friends in the set and update max position and min position accordingly.

My Solution : 68882102

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

    Although this is a good trick to know, consider using BIT's instead. They're faster.

    your solution modified: 68888920

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

哈哈

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

May someone please explain B?

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

    By the looks, you know it's a math related problem. So get some paper and write down some equation and solve it. So what is the equation? a*b + a + b = conc(a,b). Now the conc() part is mathematically weird. So you change it into real math. conc(12,23) = 1223 = 1200 + 23. So you are just putting 0s at the end of a, how many? equal to the number of digits that b has that is the length of b, then you are just adding b. So conc(a,b) = a*10^len(b) + b. Now by simple math you have b = 10^len(b) — 1. What does that mean? You started from a*b + a + b = conc(a,b). So if that is true then you have b = 10^len(b)-1, that is b must be equal to some power of 10 minus 1. you know that "some power" is the length of b. and (some power of 10) — 1 is always 9,99,999 etc. You can pick b = 9, 99, 999 or any such number. Then for every a, a*b + a + b = conc(a,b) is true. So all we need to find is how many 9, 99, 999 or this type of number there is within the given range of 1 to b, and you know for each of this kind of number , every possible a is a valid answer. For example, for the input 4 11, you know between 1 to 11 only 9 is a valid b. and for that b every a from 1-4 is a valid answer. so your answer is 1*4 = 4.

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

I haven't understood this part of problem A tutorial -> "Since the ceiling function is monotonically increasing so we can assume that ⌈f(x)⌉≤⌈f(x+1)⌉ for all x>=sqrt(d)"

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

    You first need to understand concavity. Try my explanation

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

    f(x) = x + d / (x+1)^2, differentiate to get the rate of change, f'(x) = 1 — d / (x+1)^2 . When the rate of change is positive the function is increasing. Here, when x >= sqrt(d), d / (x+1)^2 is always < 1, thus 1 — d / (x+1)^2 is always positive. So the function increases without ever decreasing (monotonically increasing) and that means for any x >= sqrt(d), f(x) <= f(x+1).

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

      f(x) = x+d/(x+1) => f'(x) = 1 — d/(x+1)^2. So as we are considered about positive slope then 1-d/(x+1)^2 >= 0 and form here we should get x >= sqrt(d) -1. so where is this -1 then?

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

        x >= sqrt(d) — 1 is actually the absolute correct equation. But without math, just by looking (which of course saves time) we could say for x >= sqrt(d) function f starts to increase monotonically so if we just run a loop from 0 to sqrt(d) and check for each value if it satisfies the condition is enough. But if your upper bound is sqrt(d)-1 that is correct too.

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

This competition is great!

»
5 years ago, # |
Rev. 2   Vote: I like it +1 Vote: I do not like it

why does this 68940816 for prob A fail on test 50! I have used the values of x that would give minimum.

»
5 years ago, # |
  Vote: I like it +1 Vote: I do not like it

can some one explain the first approach in E and what does the following mean in the editorial "You are just required to count the number of distinct values on some segments."

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

    Let's say you want to count the maximum place for friend 4. Consider the sequence

    4 1 3 1 5 4

    After the first 4, person 4 is in position 1. Then person 1 moves ahead of them, then 3 moves ahead of them. When 1 messages again, he doesn't move ahead of 4, because he's already ahead. Finally, person 5 moves ahead of 4. So because the segment between the 4's contains 3 distinct values moves, person 4 moves down 3 positions. Thus, for each friend, identify the segments between messages, count the number of unique values of a, and take the largest of them.

    I had a solution that's a good deal simpler than persistent segment trees, by exploiting the offline nature of the queries. First, a sweep gives another array p such that p[i] is the previous time a[i] messaged (so a[p[i]] = a[i], or p[i] = -1). Each query now takes the form of "how many values of i (l <= i < r) satisfy p[i] < y". I sort these queries by y, and also sort the elements of p by y, then do a sweep that updates and queries a Fenwick tree.

    Incidentally, one doesn't need to treat the initial state separately. Just prepend n, n-1, n-2, ..., 1 to the given sequence: the initial state is consistent with having received that sequence of messages.

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

      can we solve this using only segment tree (Not persistent segment tree) . We can make segment tree of size m and for each number store the position in array of m where it appears . query for each consecutive positions . Like suppose m is 4 1 3 1 5 4 . Make segment tree of this array and suppose for 4 , we can query (1,5) , (5,5) for finding number of distinct integers between them .

»
5 years ago, # |
Rev. 2   Vote: I like it +6 Vote: I do not like it

Why does problem A pass if we brute force from 1 to n ...very weak test set https://codeforces.net/contest/1288/submission/68953916 this is my submission that passed

edit: got it...it was my mistake...such an input is not possible that should give tle

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

I wrote some brood for A, and found what: Let's have some val = n/2; So lets check if our condition with x = val is true; Is it okey, and if yes, why it works?) https://codeforces.net/contest/1288/submission/68785380

»
5 years ago, # |
  Vote: I like it +3 Vote: I do not like it

I really liked $$$C$$$. I collected the ideas from many folks and wrote an editorial myself for the $$$C$$$ here.

Here is my code to all the problems that I solved for this contest, if anybody wants to refer.

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

How can we count the number of distinct values on some segments using segment tree with vectors in node? (In editorial of problem E, "The constraints allow you to do whatever you want: segtree with vectors in nodes, Mo, persistent segtree (I hope ML is not too tight for that)." ) Can someone explain the segment-tree approach?

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

    I think people typically refer to it as a merge sort tree (should be googlable). It's usually used to answer the queries of kind: given $$$L, R$$$ and $$$X$$$, find the number of values less than $$$X$$$ on a range $$$[L; R]$$$ of an array.

    So let's rephrase our subtask to these terms. Build an array $$$prev$$$ such that $$$prev_i$$$ is the index of the previous occurrence of the value $$$a_i$$$ or $$$-1$$$ if there is none. Now $$$query(L, R, L)$$$ on an array $$$prev$$$ will count only the values which have their previous occurrence to the left of the query segment. So for each unique value only its first occurrence in the segment will be counted. Thus the result of the query is the number of distinct values on a segment.

    My second solution is the example of the implementation.

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

      I see, thank you very much! It seems that we can apply this way to online queries.

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

        Can you please explain how to do the task if we have online queries ?

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

          I think we can deal with online queries using the same approach pikmike referred, because in this approach we don't need to sort given queries.

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

            How can we handle updates too ?

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

              Do you mean updates of values in an array? I refer to only an array whose values are fixed. (Online query means we cannot get next queries before we answer a current query.)

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

                Yes I mean if I update values in an array too. I got your explanation about online queries.

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

Anyone use BIT for counting number of different number in a given segment for dealing with E :)))?

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

You can also solve E in $$$O((n+m)+m\cdot logn)$$$ by using an ordered set.

Spoiler
  • »
    »
    5 years ago, # ^ |
    Rev. 2   Vote: I like it 0 Vote: I do not like it

    ordered set is $$$O(\log n)$$$ per query. That's literally my third solution but slowed down by using ordered set instead of BIT.

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

      I might be wrong but I would say that ordered set is easier, because you don't have to get the idea of reversing the order of people.

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

Hi, I had a fairly basic(and probably a stupid doubt). But I tried to run a Brute Force for Problem D in the contest and it gave me a TLE. However, as per my calculation, the complexity of my algorithm is O(m*m*n). Since m<=8 and n<=3*10^5, it shouldn't do that(as in throw a TLE).

Can somebody explain what am I calculating incorrectly? Here's the submission link: https://codeforces.net/contest/1288/submission/68810165

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

    In your program, you switched up the variables; m is what n is in the problem statement and n is what m is. So m is max $$$ 3\cdot 10^5 $$$ and n is max 8. This means, that your $$$ O(m^2) $$$ solution gets TLE. Also, it's not wise to declare arrays with variable sizes, it can cause errors depending on which C++ version and compiler you are using. You can look the details up online. I would recommend using vectors.

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

"If we want to verify that the answer is not less than x, we have to choose two arrays such that bitwise OR of their masks is (2^m)−1". Can someone please explain this line to me given in editorial of problem D.

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

    What we are doing is replacing every value in every array with 1 if it is at least x, and 0 otherwise. For example, if we had arrays {1,2,3,4,5} and {6,5,4,3,2}, and x=4 then these would become {0,0,0,1,1} and {1,1,1,0,0}. These are our "bitmasks." If we OR every pair of values at the same index for these two arrays, we get {1,1,1,1,1}. Interpreting this as a string of a binary number is where we get that this is one less than a power of two, in this case 2^5-1.

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

Guys in https://codeforces.net/problemset/problem/1288/B this problem can (8,9) or (7,9) be answers. 8 * 9 + 8 + 9 = 89 7 * 9 + 7 + 9 = 79 5 * 9 + 5 + 9 = 59 ???

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

I think I'm correctly implementing a Merge Sort Tree for E, giving me O((m+n)log(m+n)) complexity. Can anyone who understands Java help me understand why I'm getting TLE? Thanks!!

https://codeforces.net/contest/1288/submission/69596326

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

Beautiful combinatoric solution for C, I love it! I used dp with some kind of weird optimization (calculating prefix sum in the dp matrix). Then it goes to $$$O(n^2m)$$$ and got passed... 70214567

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

Alternative dp solution for C:

Let's have dp[diff][index] where diff the possible B index — A index. Notice that because the value of the next index for A can never get smaller and the value of the next index for B can never get bigger, the difference will either stay the same or get smaller. For the transition loop from newDiff = 0, to newDiff <= diff. Notice that with the current diff, newDiff can occur (diff — newDiff + 1) times, so we have to multiply it -> dp[diff][index] += nextDp(newDiff, index+1) * (diff — newDiff + 1). So we just sum of these values for the current dp state. The answer will be the sum of dp[x][0], for 0 <= x < n (if you do it recursively).

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

My code for D is significantly shorter and doesn't use binary search, running in $$$O(nm2^m)$$$ and barely squeaking past time limit.

Basically, for each subset of columns $$$S$$$, I find the maximum across all rows for the minimum element in that subset -- let's call that $$$MaxMin(S)$$$ -- and store whichever row ID gives us that maximum. Then, I loop over all the subsets of columns again. For each subset, I take the minimum of $$$MaxMin(S)$$$ and $$$MaxMin(S^c)$$$, and whichever subset maximizes that minimum is the correct one to take. I find whatever row ID gave us $$$MaxMin(S)$$$ and whatever one gave us $$$MaxMin(S^c)$$$ and print those two.

Maybe code can explain it better. My code is here.

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

I solved Problem C: Two Arrays ( 1288C - Two Arrays ) with O(n). Kindly have a look if anyone is interested. 75613441

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

is there any other approach of D!! i don't like BS

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

Hello, I have a doubt in problem A as the given function has a unimodal graph. I was solving it with a ternary search. As everyone must be aware that there is a condition in a while loop of ternary search. The condition is while(high-low>=3) I found that when I wrote ternary search solution it was giving the wrong answer then I changed that 3 in the condition to 11 it was still giving the wrong answer but on much later test case and when I finally changed it to 100 it got accepted. Can anyone please tell me why it is happening?

As far as I know while(high-low>=x) reduces your search space such that you will finally have x+1 points from [low, high] and the minimum of the function is guaranteed to be in this range.

code failing at test case 6, X = 11 code failing at test case 34, X = 20 code accepted X = 100

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

Really beautiful solution for problem E. I got the idea about to think for maximum only before its query and at the end, but i think i read the editorial too soon :/

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

For anyone like me, who neither got Stars and Barns nor any of the comments, try This.

Combinatorics Solution

Combinatorics CODE

Combinatorics is Not where I shine !

DP Solution

DP CODE

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

E solution using 1 BIT and pbds ordered_set 94656203

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

E is very simple if you use an ordered set.

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

For problem c,as in editorial consider the sequence as a1,a2,a3,..am,bm,bm-1,...b1 now imagine if you have a sequence which is non decreasing you can always form a and b arrays by dividing it from the middle,so question goes down to solving number of non decreasing sequences of length 2m,so since say u use numbers from 1....n with a1 no of 1,a2 no of 2,a3 no of 3s so now a1+a2+a3+a4+a5+..an=2m....(n+2m-1 C n-1)

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

It's quiet like Euler candy division's problem