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

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

1759A - Да-да?

Идея: MikeMirzayanov

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

1759B - Потерянная перестановка

Идея: MikeMirzayanov

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

1759C - Термостат

Идея: Vladosiya

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

1759D - Сделай круглым

Идея: MikeMirzayanov

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

1759E - Гуманоид

Идея: Gornak40

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

1759F - Всевозможные цифры

Идея: senjougaharin

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

1759G - Восстанови перестановку

Идея: MikeMirzayanov

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

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

I solved A by checking if there is a letter that is not in the word "Yes", and checking that each Y has s before it, each e has Y before it, and each s has e before it. Also I solved D using lcm, for each i such that 1<=i<=18 check if lcm(n,10^i)/n<=m. If it is, multiply the answer by m/answer

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

Great round! Hope to see another Div.3 soon. Div.3 almost always meant +ve delta for me, so I am always hyped for them.

To point out, F didn't need a very complex data structure. A single set (a sorted set would be preferrableI just checked, an unordered set worked equally as fast) was enough. If you look at the constraints, you will notice that $$$n \leq 100$$$. Therefore, if you save digits that did exist in the original number and the number after a carry, the size of the set is guaranteed to be less than 200. Now if we follow the process I explained in that another comment, we know we can manage $$$\mathbf{pp}$$$ and $$$\mathbf{pk}$$$ linearly starting from $$$p$$$ and the number of the last digit, and the loop won't take over 200 iterations. This is more than enough to fit in the TL. (I have not tested using an array here though, but the process will be the same anyways.) One great thing to note is that this solution's time complexity only relies on $$$n$$$. Maybe for this reason (and due to some additional constants), my solution turns out to be 5x faster than the jury solution. Good to know!

Here is a submission based on this approach — 181506873.

upd: The TeX for problem D's tutorial seems to be broken. Maybe you forgot come curly braces?

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

    yeah true, I also did a similar approach

    Submission: 187409654

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

1759D - Make It Round

Here's my clear and concise code for D

#include <iostream>
#define int long long int
     
signed main() {

    std::ios::sync_with_stdio(false);
    std::cin.tie(0);

    int t = 1;
    std::cin >> t;
    while(t--){
        int n, m;
        std::cin >> n >> m;
        int x = 10, ans = n*m;
     
        while(1){
            int g = std::__gcd(x, n);
            int k = x/g;
            if(k <= m){
                ans = k*(m/k)*n;
            }
            else
                break;
            x *= 10;
        }
     
        std::cout << ans << "\n";
    }
}

Greedy approach : We will try to append as many 0's at the end within given constraint How to find it ?

Let's take input examples : 6 11

  • We can have 30 60 90 120 ...
  • But only 30 and 60 falls within constraint
  • 6*5 = 30, k = 5
  • 6*10 = 60, k = 10
  • 6*15 = 90, k = 15 not possible

Now we will build our answer by checking if I append zeros can I get some k such that it falls under constraint, once we can't append 0's we will break loop

How to do it? How to find possible values of k? Well it's easy, we can use some mathematics


We will check if I can have some possible number as z*10 z*100 z*1000 and so on... z [1, 9] z should be minimum possible as we are using greedy
we have our n as 6 so to append 0 at the end I can have z*10
n   = 2*3
ans = z*10
so we have ans = n*k, 1 <= k <= m
n = 6 k = 5  z = 3 ans = 30
n = 6 k = 10 z = 6 ans = 60

How to find k ? Let g = gcd(x, n) = (10, 6) = 2 then k = x/g = 10/2 = 5, k can take minimum value as 5 within given constraints But as we have to find max answer if we append some zeros so we will find k as k = (11/5)*k k = 10

Thanks :), You can ask doubt

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

I solved D with binary search for answer. I think it would be better to add binary search tag to D.

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

I almost copy-pasted solution for problem E from the editorial and it's giving WA on test 1?!

Submission: 181810855

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

    Those global variables are the issue. Try declaring them inside the function.

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

      Thank you, it worked, but why?

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

        Not quite sure aswell

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

        Think about how solve() works. First, you compute g. No problems here. Second, you compute b. This uses a recursive call to solve(), so the global variable g may get overwritten during this subcall.

        Finally, you compute max(b, g). This can give WA if g is no longer the correct value.

  • »
    »
    22 месяца назад, # ^ |
      Проголосовать: нравится 0 Проголосовать: не нравится
    g=(green?solve(a, n, i, h*2, green-1, blue):0);
    b=(blue?solve(a, n, i, h*3, green, blue-1):0);

    how this work i don't understand

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

      I hope these few lines will make it clearer.

      $$$\text{green}$$$ is the number of green serums left and $$$\text{blue}$$$ is the number of blue serums left.

      $$$\text{g}$$$ is the answer if we use green serum at the $$$\text{i}$$$ -th step and $$$\text{b}$$$ is the answer if we use blue serum at the $$$\text{i}$$$ -th step.

      When writing boolean valus, x is same as writing x!=0 where x is a number.

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

For E, shouldn't it be $$$O(NlogN)$$$ instead of $$$O(N)$$$ since you need to sort the astronauts?

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

Can somebody explain why the solution to D works, or what is the idea about it?

Obviously it is somehow about the number of divisiors 2 and 5 in n, but how?

And how/why does the sol find "If there are several possible variants, output the one in which the new price (value n⋅k) is maximal."?

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

    The input number $$$n$$$ number can be factored as $$$x 2^a5^b$$$, $$$a, b \geq 0$$$

    The number of zeroes at the end of this number is equal to $$$min(a,b)$$$, so our goal is to maximize $$$min(a,b)$$$

    We start with $$$k=1$$$.

    while $$$k \leq m$$$, do:

    if $$$a < b$$$, multiply $$$k$$$ by $$$2$$$ and increment $$$a$$$.

    if $$$a > b$$$, multiply $$$k$$$ by $$$5$$$ and increment $$$b$$$.

    if $$$a=b$$$, multiply it by $$$10$$$. and increment $$$a$$$ and $$$b$$$.

    This guarantees that $$$k \cdot n$$$ has the largest amount of zeroes at the end, this does not guarantee that $$$k$$$ is the biggest possible number, to do this, we try to make $$$k$$$ as close to $$$m$$$ as possible.

    $$$k \cdot q \leq m$$$

    $$$q \leq \frac{m}{k}$$$

    $$$q = \frac{m}{k}$$$

    So the answer is $$$q \cdot k \cdot n$$$

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

    The greedy approach can be derived through the fact that we know that multiplying 2*5 always would add a 0 at the end, so greedy we say that instead of multiply k with 10, why not multiply it with an even smaller number -> 2 or 5, Thus i can say, we would try to equalize the count of 2 and 5.

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

Problem E: Please someone explain to me why we use solve(0, h, 2, 1) in the initial call, I don't understand why we use 2,1.? what's the logic behind it.?

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

    oh, my bad, I missed that part — "the humanoid took with him two green serums and one blue serum".

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

      I am getting no idea what we are doing in que E. why are we recursively calling that function and what that function is doing?

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

        @hemantpatil10125 it's just an observation. Try to figure out how it works, recursion is always hard, but very easy if you can catch the basic and base cases, sometimes tricky logic. Keep trying at least 20 easy recursion problems, understand how they work, then it will be much easier. I am also a newbie, so It may sound inappropriate to make a statement about that. Hope you will expert one day if you solve problems daily.

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

Can someone explain the segment tree solution for problem G?

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

    Did you got any segment Tree approach for this problem?

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

    hey have you got the approach for segment tree, i could not come up with the solution of G i was trying using segment tree but i got stuck somewhere, if you got it please could you share the code.

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

      I think I finally got it. Imagine there's a bracket sequence $$$s$$$ of length n, for each $$$i$$$, we set $$$s_{b_i}$$$ to a right bracket and set the rest to left brackets (e.g. the first example input is (())()). So our target would became for each right bracket we want to match it to a left bracket to its left, so the solution exists only if it's a valid bracket sequence, i.e. all prefix sums are greater than or equal to 0.

      Because we want the lexicographically minimal permutation, so for each $$$b_i$$$ we want to find the smallest $$$j < b_i$$$ such that if we remove $$$s_j$$$ and $$$s_{b_i}$$$, the remaining bracket sequence is still valid. We claim that we should choose the largest $$$j$$$ such that $$$pref_{j - 1} = 0$$$ (I'm too lazy to explain it so try to come up with it by yourself). In order to find such $$$j$$$, we use a segment tree by maintaining the sum, min prefix, and the position of the min prefix. See jiangly's code or my code 188831632 for implementation.

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

        You can also use a Range Max-Query Segment tree and then binary search for each value in the unused array so that you find the right most element in the input array ($$$a_i$$$) such that $$$a_i$$$ > $$$unused_j$$$

        Here is the code for this approach:

        Code

        And the corresponding submission: 210703511

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

For the problem G, can we binary search on the value we put before each element of the input array? For each element, the search space will be the elements in the unused set which are smaller than this element. And to check if a value is feasible, we will check that we still have sufficient options left for rest of the elements if we put this value before the current element in the final array. This can be done using ordered set (refer my solution). We will choose the least among all feasible values, to maintain the lexicographic condition. Also note that the search space will follow monotonicity.

I used the above approach, but got WA on TC3. Solution: LINK

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

can someone tell me the mistake in my code of problem G. I have done pretty much the same as editorial's approach,but I am getting WA on test case 14. (basically,my code is also including 0 in one of the outputs which I am unable to think of why) https://mirror.codeforces.com/contest/1759/submission/202043617

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

1759F - All Possible Digits

I implemented the algorithm below, which is O(n) time-complexity and O(n) space-complexity. But still, I am getting TLE for the 7th test case.

Could anyone tell me the error in my code and how I can optimize the algorithm's run-time?

The code can be found here: 218600714

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

    There are so many places, where it is not O(n), vector allocation, loops

    O(p), maybe, but p = 10^9, that's why TLE,

    Here is the solution that works in O(nlogn):

    259545032

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

In problem E, I kept failing using non recursive approach, didn't think about the case where you'd use green twice

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

what a incredible problem E

I was only thinking the recursive DP approaches which were too slow because to it depended on the health tooo, but reading editorial solution made me think I have lot to learn bring more problems like these.

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

can s'o explain problem B? i saw jiangly was hacked but i dont know what happened...

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

In problem G, After getting the numbers not in the array, I kept track of the number of options available to be placed before each element in b. For example, if n = 8 and b = [8, 7, 4, 5], the numbers not in the array would be [1, 2, 3, 6], and 8 and 7 would both have 4 options (as they are greater than all four numbers not in the array b), and 4 and 5 would have 3 options (as they are not greater than 6).

It makes sense that for the number of options i, at most i elements of the array can have it. Example A: for n = 8, and b = [2, 3, 7, 6], both 2 and 3 have only one option ( 1 ), but only one of them can use it, so there would be no answer. Example B: n = 8 and b = [8, 7, 4, 5], 4 and 5 have three options, so we can use two for them, and that would leave two options for 7 and 8 each.

My solution (in Java) maintained a TreeMap of the numbers of options and the indices of the corresponding elements that have those options. For every iteration, I would check the smallest number of options, and check if the elements having that number are more (which means no answer), or less (which means I have room to give the next smallest number to the next element in b), or equal (which means that they must be given the next smallest numbers).

This seems reasonable to me, and it passed the first two tests. However, I got a WA in test 3, and the test case is hidden. Can someone help me understand what is wrong with my solution? Here is my submission — 270539680.