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

Автор molney, 6 месяцев назад, По-русски

1899A — Игра с числами

Идея: EJIC_B_KEDAX, разработка: EJIC_B_KEDAX.

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

1899B — 250 тысяч тонн тротила

Идея: zwezdinv, разработка: zwezdinv.

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

1899C — Ярик и массив

Идея: meowcneil, разработка: meowcneil.

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

1899D — Ярик и ноты

Идея: zwezdinv, разработка: molney.

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

1899E — Очередная сортировка

Идея: Vladosiya, разработка: Vladosiya.

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

1899F — Лёшины капризы

Идея: EJIC_B_KEDAX, разработка: EJIC_B_KEDAX, zwezdinv.

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

1899G — Необычное развлечение

Идея: EJIC_B_KEDAX, разработка: Sokol080808.

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

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

Will there be a English editorial?

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

time of contest was so fu....g less than the time it should be

non of problems was that intresting and you can see that in the comments of announcement and there was no hard algorithm problem and i think in problem C you should say that what is a subarray problem F statement wasnt clear at all

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

E was too easy for an E. In my opinion, D > C > B > E > A in decreasing order of difficulty.

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

    I think maybe it's about the problem statements

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

    After not being able to implement B, i just tried C and didn't even bother to read D. But after this comment i read E and yupp ... this the correct order of difficulty mentioned by you.

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

    Nope i dont think so C was the most easiest one just apply sliding window

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

Pretests of C are horribly bad.

So many contestant has 0 if all of the array is negative.

Example code: https://codeforces.com/contest/1899/submission/233111911

Sample Input for Hack:

1

3

-3 -1 -1

NOTE: I am not writing this comment to bury authors (because it has really good problems and it can be best Div. 3 I have ever seen.) but I think how random case generator don't produce this example, it seems really interesting?

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

don't understand this part -> Since k is a divisor of n , there are O(n−−√3) such k is there a proof?

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

Can someone explain what problem F is asking? Can't comprehend it =(

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

    Initially, you have to assume any tree of n vertices. Then, there are q queries. Query is: You have given distance x, if the distance between any two leaf nodes equals x, then do nothing. If it is not then you have to make a change in the tree (only one operation).

    The operation is like this — choose any three vertices u, v1, v2 such that there is edge b/w u and v1 and there is no edge b/w u and v2. Then, remove edge b/w u and v1 and add edge b/w u and v2.

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

Explain what is wrong with my solution 233204396, problem D. I count the number of different numbers, using this information I calculate the number of pairs of identical numbers and pairs from (1, 2), add them up, get WA on the 11th test

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

Video solution for the Chinese:

BiliBili

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

Problem F, on example,2nd test case, in the third query already exist a path with distances 3 between 2 leafs , 5-4-2-3, but the answer wasn't -1 -1 -1.I'm receiving WA veredict for that?233191318

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

    Uh actually the wrong answer you're getting is for the case when D = 4 (Q = 4). According to your construction Tree at Q = 4 (1-2-3-5 & 2-4) here D is not 4. You make a change instead of -1 -1 -1 to preserve the answer for the upcoming query.

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

Problem G can be solved offline in O(nlogn + qlogn). answer queries in decreasing order of entry time of x. Add the exit time of each vertex v in the corresponding position in the permutation when we process the first query such that tin[x] <= tin[v] In this way, we can guarantee that we've only added vertices that have greater entry time than current x. So we can query for the minimum on segment.

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

Nice problemset. B was tough for me to understand :(

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

Bad problem state b and d! Also don’t like any of 1st 5 problems. Although it was fun to participate. Best luck for me and also for the problem setter. Plz try to explain some of the test cases.

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

Which data structure do I need to know to solve G?

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

Thank you so much!

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

G can be solved with small to large too

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

Can someone tell me why this code for problem B is not working

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

hash-table(for e.g., std::unordered_map in C++) can help to solve problem D with O(n) complexity, but, of course, O(nlogn) is enough to get OK

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

Problem b: 2^(a⋅2^b)=2^(b⋅2^a)⇔a⋅2^b=b⋅2^a how did we get the second part from the first part?

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

    Since the exponential function is strictly increasing, the equation 2^x = 2^y is equivalent to x = y.

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

      okay i get it now, but for the last part, shouldn't we get: 1/(2^k) = 1/(2^(a(1-2^k))) rather than: 1/(2^k) = 1/(2^(a(2^k-1))) ?

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

        No, because

        $$$\displaystyle\frac{2^a}{2^{a\cdot2^k}}$$$

        $$$\displaystyle=\frac{2^a}{2^{a\cdot2^k-a+a}}$$$

        $$$\displaystyle=\frac{2^a}{2^{(a\cdot2^k-a)+a}}$$$

        $$$\displaystyle=\frac{2^a}{2^{(a\cdot2^k-a)}\cdot2^a}$$$

        $$$\displaystyle=\frac{1}{2^{a\cdot2^k-a}}$$$

        $$$\displaystyle=\frac{1}{2^{a(2^k-1)}}$$$

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

    Let's define x = a * 2 ^ b and y = b * 2 ^ a.

    We know that 2 ^ x = 2 ^ y, so x = y <=> a * 2 ^ b = b * 2 ^ a, which is what you asked for.

    I hope that you understand!

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

    apply log2 to both sides you get :

    log(a)+ b*log(2) = log(b) + a*log(2)

    hence

    b — log(b) = a — log(a)

    this only occurs when 1,2 or when a=b

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

    take log base 2 of both sides, then you will get the expression

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

F can be solved with an easy O(n) solution

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

E and F both were extremely easy if you consider the given Testcases gave away the entire solution process

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

G can be solved using a normal segment tree or a Fenwick tree by offline query.   First, let's solve this the other way around. You don't have to find a descendant; you have to find an ancestor.

  1. You can just DFS and keep adding each node to the segment tree or Fenwick tree as you traverse.
  2. Once you reach node 'u', you check all queries and find the sum between [l, r] for each query.
  3. Remove the node once the whole iteration is done.

Now here, the sum initially doesn't change when you return because we remove the sum for all the children in step 3 before again reaching node 'U'. So we will remove step 3, and to make sure for each query we received any descendant, we will store the initial sum (number of nodes between L and R), and after we iterate all the children for node 'U', we recheck the count, and if there is any descendant, our sum will increase.

Submission Here

You can check code if don't understand the explaination.

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

G can be solved using a normal segment tree or a Fenwick tree by offline query.   First, let's solve this the other way around. You don't have to find a descendant; you have to find an ancestor.

  1. You can just DFS and keep adding each node to the segment tree or Fenwick tree as you traverse.
  2. Once you reach node 'u', you check all queries and find the sum between [l, r] for each query.
  3. Remove the node once the whole iteration is done.

Now here, the sum initially doesn't change when you return because we remove the sum for all the children in step 3 before again reaching node 'U'. So we will remove step 3, and to make sure for each query we received any descendant, we will store the initial sum (number of nodes between L and R), and after we iterate all the children for node 'U', we recheck the count, and if there is any descendant, our sum will increase.

Check Code Here

You can check code if you don't understand explaination.

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

For F, you can just connect the first $$$n-1$$$ as a sequence tree and keep the $$$nth$$$ node away. For each query you can just connect the $$$nth$$$ node to the $$$d_i$$$ and the distance will be $$$d_i$$$ from node $$$1$$$ (node 1 is a leaf node).

Visualization
Code
»
6 месяцев назад, # |
  Проголосовать: нравится +1 Проголосовать: не нравится

for D, Put the equation $$$\left(2^{x}\right)^{2^{y}}=\left(2^{y}\right)^{2^{x}}$$$ in a graphing calculator the graph directly shows x=y and (1,2),(2,1) . Then write code.

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

Why were the problem statements so vague? is this the first time that they are organizing a contest?

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

Easy sqrt-decomposition online solution of G: 233217422

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

For problem D, there is a simpler proof, without much symbolic manipulation.
We know that $$$2^a \cdot b = 2^b \cdot a$$$.
Write that as $$$2^a / a = 2^b / b$$$.
The function $$$2^x / x$$$ is increasing for $$$x > 2$$$.
So, the equality can not hold for any numbers greater than $$$2$$$.

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

Why don't you guys just rephrase the problem statements using chatgpt? The prompts were actually painful to understand.

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

    That is not actually you who does the rephrase of problem, which is actually a problem solving skill. Chat-GPT does it for you. I do not do this because I have work ethics

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

    maybe because each and every detail is very important and chatgpt might morph a detail.

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

Editorial has such a strange solution for F. You can just make a tree which is a straight line from $$$1$$$ to $$$n-1$$$ and attach vertex $$$n$$$ to vertex $$$d_i$$$ for all queries. Both vertex $$$1$$$ and $$$n$$$ are leaves and distance of $$$1$$$ from $$$n$$$ is $$$d_i$$$. Complexity $$$O(n)$$$.

https://codeforces.com/contest/1899/submission/233223051

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

B problem editorial not clear for me. Is it slower to find all divisors of n, and iterate over them than ignore it and just iterate over all possible k? I am sorry I am missing something in editorial although I solved it right after contest. I can't find how O(n⋅n−−√3) compares to O(nlogn) asymptotically.

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

Contest unrated? rank not updated

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

easiest A-F lol

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

F: At the beginning, let the tree be a chain, with the edge of (n-1) ->(n) constantly moving. It would be easy to set the distance between 1->x as d. https://codeforces.com/contest/1899/submission/233233780

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

More simpler solution to F is by just maintaining the last node we connected our nth node to https://codeforces.com/contest/1899/submission/233238323

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

A more simpler solution to F by just maintaining the last node we connected the nth node to- https://codeforces.com/contest/1899/submission/233238323

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

Problem B. can anyone explain why my code fails.

solution link: https://codeforces.com/contest/1899/submission/233239115

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

For b problem, how did yall find out we gotta use long long instead of int?

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

    Try put 1e18 instead of INT_MAX and put 0 instead of INT_MIN

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

    Bcoz ai<=10^9 ..and if u take sum of any subarray of some length lets say K then you can easily see it will become greater than intmax. Hence u had to take llong

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

    since the whole array can be 1e9, n > 3 can lead to sum overflow

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

Problem 1899F - Alex's whims can be solved easier like this:

Spoiler

Is there a counter to this method? Was quite surprised to see such small constraints.

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

    I did the same thing, and as far as I can see, the solution is completely right. They must've made the constraints low to troll participants.

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

      I literally saw the problem in last 4-5 mins and thought of this solution but I somehow convinced myself I'm wrong (Read: low confidence) and spent the time in understanding the problem statement of B (which also I failed).

      Anyway, great performance avighnakc, Congratulations on reaching Expert!!

      How did u get this good so quick? Did u practice a lot of $$$C, D$$$ of Div.2s or is it something else?

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

        I did CSES, practiced quite a lot of codeforces div2B’s and C’s and also 1500-1600 rated problems, and up solved some ABCs. Honestly, it’s all about how much effort you put in. I’m not that special per-se, if you grind as much as me, you’ll also improve fast. Good luck, and thanks!

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

      The constraints are low because to check if the solution is correct or not the checker needs O(n^2q) operations minimum. If the constraints were too large it would have taken ages to check each test case.

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

For problem F many have written the condition where last_node == current_node and printed -1 -1 -1 in that case but my code where i have commented the same condition is also accepted.

Submission link: https://codeforces.com/contest/1899/submission/233250281

But it shouldn't as the question mentioned for every u v1 v2 there should be an edge between u and v1 and no edge between u and v2. but my code outputs the same v1 and v2 for some cases and is still accepted. Am I missing something or the question was phrased slightly incorrectly?

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

Can someone explain 'F' ?

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

Can someone explain 'F' ?

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

    Yeah sure, here I have used the pointer approach. So, initially, we will consider the tree to be like a long stick. So all the nodes will lie in a straight line. Now, We will only move the end node and no other node and this will solve our problem easily. So initially we have the tree something like 1->2->3->4->5->6->7. So in this case we will only move the 7th node. Now, we know that 7 is connected to 6 initially so the dist between 1 and 7 will be 6. If 7 is connected to 3 then the dist between 1 and 7 will be. So, wherever the 7th node will be connected the dist will be equal to that node itself. Check this implementation now, or see my last submission.

    void solve(int xx){ int n, q; cin>>n>>q; rep(i,2,n+1)cout<<i-1<<" "<<i<<endl;

    int pnt = n-1;
    rep(i,0,q){
        int x I;
        if(x!=pnt){
            cout<<n<<" "<<pnt<<" "<<x<<endl;
            pnt = x;
        }else{
            cout<<"-1 -1 -1"<<endl;
        }
    }

    }

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

I'm wondering if the successful hacking attempt goes into the final test data. I wrote a brute force dfs in G, which only works when the data is given randomly, it passed pretests, but I think it's easy to hack when the give tree is a chain. I don't know why it passes the hack.

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

Didn't realise that F was an easy problem....

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

F was very easy: my code is more simple than authors.
https://codeforces.com/contest/1899/submission/233117295

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

There is a simpler solution for F: 233147982. Plus G is solvable with just std::set + small-to-large technique by storing inverse indexes and lower-bounding the left bound of a desired segment.

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

Why is my submission 233141859 timing out? I've used an unordered_map to solve it in O(n).

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

Problem G can be solved offline in $$$O((n+q)*log(n))$$$. First, push back all queries for node x into a vector. Then just DFS and for each node, you store its descendants' pos in array $$$p[]$$$ in a set. You can merge all sets of its children faster by swapping 2 sets to put all elements of the smaller set into the bigger one. In that way, you can answer all queries $$$[l,r,id]$$$ of x by using $$$lower bound$$$ to check that is there any element in the set sitting between l and r, and update the answer for the id-th query.

This is my submission: 233162558

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

For those who feel(like me) that the observations in problem D are hard to make analytically(and at the same time be sure that no edge case was missed), we can use high school calculus to solve this in a more structured and generalized format, which may be useful in some other questions-

After you get the equation a⋅2^b=b⋅2^a, rewrite it as (a.2^-a)=(b.2^-b). This holds when a=b. Furthermore, we ask, "For what distinct integral values of x is f(x)=x.2^-x equal to itself?" We can see this by making a rough plot of f(x). There is only one critical point(x=(1/ln2)~1.4), which is a maximum (we can easily observe this using just the first derivative). So the curve looks like a bell, strictly increasing before 1, peaking between 1 and 2, and strictly decreasing thereafter.

The equality cannot hold on either side of the peak since we have a strict ordering. Therefore the only possible equality is if f(1) matches with some x>=2. Now we can either find that this holds for x=2 by trial and error, or more rigorously...wait for it...binary search. Search on the strictly decreasing array for f(i) i>=2 for the value of f(1), to get x=2. Then we can count the occurrences using standard techniques.

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

    I mean, honestly, you didn't need to observe that (1,2) are the only solutions. You could just observe that since $$$(2^a)^(2^b) = (2^b)^(2^a) => \frac{2^a}{a} = \frac{2^b}{b}$$$, you can store the values of $$$\frac{2^a_i}{a_i}$$$ in a map and count that way.

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

      Yes, you are right! That makes absolute sense and is a much simpler way to solve this. Even in my original solution, I did not observe that (1,2) is the only solution, instead came up with a way to efficiently find all possible solutions using sorting and two pointers(based on the growth rate of exponents), Although I kept getting WA due to integer overflow(facepalm).

      But I feel that without this observation we end up doing more computation than is required(even though asymptotically it is the same) so I just wanted to wrap my head around it.

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

      But how you will store $$$2_i^a$$$ when a becomes 1e9

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

        Check my submission, you can just store the exponent and divide out all factors of 2 from $$$a_i$$$.

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

why long long lmax = LONG_MIN, lmin = LONG_MAX; this line gives wrong answer why this one gives right answer? long long lmax = -1e18, lmin = 1e18; why?

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

I was trying to solve 1899G with a different approach, but getting MLE.

For each node x, I'm keeping track of all nodes in the subtree rooted at x, but I'm not actually storing the child nodes directly, but rather their index in P. i.e if nodes 2,4 are in the subtree rooted at 3, I store mem[3]={idx[2],idx[4]} where idx is the index of 2 in P ( in sorted order ).

Then I can run Q(l,r,x), in mem[x] in log(n) time.

T(n) = O(nlogn) S(n) = O(n*n) — MLE

when I see the solution, even in the ST, for each segment we are storing an array of start times, even that would have a S(n) = O(n*n). how does it not give MLE?

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

For problem F, Can's I just maintain a chain and keep moving the last node to the dth node?

    int T; cin >> T;
    rep(t, 0, T) {
        int n, q; cin >> n >> q;
        rep(i, 1, n) cout << i << ' ' << i + 1 << '\n';
        int pre = n-1;
        // we keep modify the last node of the tree
        rep(_, 0, q) {
            int d; cin >> d;
            cout << n << ' ' << pre << ' ' << d << '\n';
            pre = d;
        }
    }
»
6 месяцев назад, # |
Rev. 2   Проголосовать: нравится 0 Проголосовать: не нравится

Why is O(NlogN) solution of D giving TLE in C++? LMAO Just checked that map is getting accepted but not unordered_map! WTH??? Someone tell the author that this isn't Div1. Also, if they wanted to use such tricks then at least they could have included these test cases during the contest. https://codeforces.com/contest/1899/submission/233145738

  • »
    »
    6 месяцев назад, # ^ |
      Проголосовать: нравится +3 Проголосовать: не нравится
    1. std::unordered_map is not $$$O(n \log n)$$$. It's $$$O(n)$$$ on average, and $$$O(n^2)$$$ with many hash collisions. std::map is guranteed $$$O(n \log n)$$$.
    2. In Div.3, all tests from the authors are included during the contest. In system testing the only new tests are tests from sucxesful hacks. This test wasn't included by the authors, it was a hack test.
    • »
      »
      »
      6 месяцев назад, # ^ |
        Проголосовать: нравится 0 Проголосовать: не нравится

      I am still shocked why so many people (including author) have used map for this question. I agree it is my fault for not using custom hash function for unordered_map but what is the point of using map when it is slower than unordered_map on average?

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

        Why does it matter that std::unordered_map is faster on average, if it has a slower worst case performance?

        People use std::map more often than std::unordered_map because hacks against std::unordered_map are very common on codeforces. If you use std::map, you cannot get hacked with those types of hacks. You should read this blog for a more in-depth explanation of this. That blog also contains a way you can still use std::unordered_map and not get hacked.

        Also, std::unordered_map isn't even that much faster on average. Even though it is $$$O(1)$$$ per operation and std::map is $$$O(\log n)$$$ per operation, std::unordered_map has a massive constant factor which makes it almost as slow as std::map, even on average (unlike for example arrays, which are also $$$O(1)$$$ per operation but they are much much faster).

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

          Thanks for the info ^_^

          Friendship ended with unordered_map, now map is my best friend!

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

New to CPP ; anyone could give reason as to why I'm getting TLE on problem D : https://codeforces.com/contest/1899/submission/233162158

When I would easily be able to solve this in python, and it's O(N) normally...?

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

    Someone hacked the solution that uses unordered_map :(

    Read this blog for more info https://codeforces.com/blog/entry/62393

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

      Yeah read this... Feel like when you're at the point where you need to create your own hash function because the tests cases are designed such that standard library hashing function is going to fail, it's not really CP anymore and just tedious knowledge requisite... If my solution is O(n), it should pass....

      Thanks for the link !

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

        You're right. $$$O(n)$$$ solutions should (and do) pass. But your solution is $$$O(n^2)$$$, unfortunately.

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

          I mean, if the hill you want to die on is that hashmap insertion is O(n), more power to you, but I believe, that pretty much everyone would say that hashmap insertion is O(1) (invite you to read about amortized operation ; are vector insertion O(n)?). Like if the goal is for hashmap to be unusable in the contests, we should just say so somewhere. No amount of pseudo-randomness is going to be enough if I really want to come up with an example which will destroy your hash function.

          Oh well, just another reason why python is just better than c++, shoulda known better !

          Also, just to be clear, no O(n) solutions exists for the problem from your point of view, so no, they don't pass !

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

            I mean, if the hill you want to die on is that hashmap insertion is O(n), more power to you, but I believe, that pretty much everyone would say that hashmap insertion is O(1) (invite you to read about amortized operation ; are vector insertion O(n)?)

            Hashmap insertions are $$$O(1)$$$ on average with random data, they aren't $$$O(1)$$$ amortized. If some operation is $$$O(1)$$$ amortized, even if a single operation can be $$$O(n)$$$ in the worst case, it is guranteed that $$$n$$$ such operations will work in $$$O(n)$$$ total time. But with many hash collisions $$$n$$$ hashmap insertions with a bad hash work in $$$O(n^2)$$$ $$$\Rightarrow$$$ not amortized $$$O(1)$$$.

            Like if the goal is for hashmap to be unusable in the contests, we should just say so somewhere.

            In my opinion it's a feature of the language, and the participant is responsible for understanding how their language works.

            No amount of pseudo-randomness is going to be enough if I really want to come up with an example which will destroy your hash function.

            Maybe it won't prevent it, but it will definitely slow down the creation of such hack test cases. And how are you going to deal with time-initialized randomness when creating those hacks?

            Oh well, just another reason why python is just better than c++, shoulda known better !

            Python sictonaries can also be hacked similarly (at least in PyPy), see this. And if python is supposedly better, why so you even use c++?

            Also, just to be clear, no O(n) solutions exists for the problem from your point of view, so no, they don't pass !

            $$$O(n)$$$ solutions do exist, like the hashmap solution with a randomized (and time-initialized) hash function, which can't be hacked. Or can you prove me wrong by providing a hack to the custom hash in the blog linked in the original reply?

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

              You're right about amortized, I was just making an analogy (poorly written I'll admit) to how you could make the same argument for amortized operations, the point being that talking about worst case is (in some cases, and in my opinion) non-nonsensical. I don't think anyone would call n insertions in a vector O(n^2), even if worse case is O(n), because we know it's O(1) amortized (which while not the same as average, is comparable).

              In my opinion it's a feature of the language, and the participant is responsible for understanding how their language works.

              What's the point to that though...? All this ends up doing is that everyone has to put some pseudo-random implementation in their template and then not think about it ever again. It's not about learning how the language work or anything deep, it's literally just another implementation of something that is in the standard library...

              O(n) solutions do exist, like the hashmap solution with a randomized (and time-initialized) hash function, which can't be hacked. Or can you prove me wrong by providing a hack to the custom hash in the blog linked in the original reply?

              Give me an implementation, an infinite amount of time and I will eventually find a test case which, at a particular time, will make you TLE. The complexity of your solution does not change because finding such a test case is harder, which is what I'm arguing against. Hashmap always will have that O(n) worst case, you can't change that no matter the implementation. Even worse ; a pseudo-random implementation makes it so that the same exact code that passed a problem super easily could TLE in another time, you're just gambling. To me, it's not a super interesting problem to solve, so either we should just say somewhere that hashmaps are not to be used ever, or we realise that hacking standard library implementation of hashmap is not really pertinent to the task of solving the problem...

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

void solve() { int n,q; cin >> n >> q; for(int i = 2 ; i <= n ; i++){ cout << i-1 << " " << i << endl; } int currDistance = n - 1; int node2 = n; int node1 = n - 1; while(q--){ int distance ; cin >> distance; if(distance == currDistance){ cout << "-1 -1 -1" << endl; continue; } cout << node2 << " " << node1 << " "; node1 = distance; cout << node1 << endl; currDistance = distance; } }

O(n + q) Solution for F 233329854

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

Did anyone solve Codeforces Round 909 (Div. 3) G with Treap?

It can be solved by it according to this USACO Guide article on Small-To-Large Merging :

$$$"$$$It's easy to merge two sets of sizes $$$n\ge m$$$ in $$$\mathcal{O}(n+m)$$$ or $$$(m\log n)$$$ time, but sometimes $$$O\left(m\log \left(1+\frac{n}{m}\right)\right)$$$ can be significantly better than both of these. Check "Advanced — Treaps" for more details.$$$"$$$

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

If anyone got TLE (like me) in the problem 1899D - Yarik and Musical Notes, maybe look at this blog post about unordered_map.

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

Why at problem D solution with unordered map get TLE and that with map dosent

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

Why are we performing lower_bound — lower_bound instead of upper_bound — lower_bound, given that in the instance where we have an entry time of 1,2,3 in the merge sort tree and our time in and time out of x is 2, then wouldnt lower_bound — lower_bound return 0 instead of returning 1?

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

In problem C while checking the parity of two consecutive numbers in array ~~~~~ int res = a[0]; int ans = a[0];

for (int j = 0, i = 1; i < n; i++, j++) { if (res < 0) res = 0; if ( (a[j] % 2) == (a[i] % 2) ) res = a[i]; else res = res + a[i]; ans = max(ans, res); } cout << ans << "\n"; ~~~~~

int res = a[0];
int ans = a[0];

for (int i = 1; i < n; i++)
{
     if (res < 0) res = 0;
     if ( (a[i] & 1) == (a[i - 1] & 1) ) res = a[i];
     else res = res + a[i];
ans = max(ans, res);
        }

        cout << ans << "\n";
»
6 месяцев назад, # |
Rev. 7   Проголосовать: нравится 0 Проголосовать: не нравится

In problem C while checking the parity of two consecutive numbers first code is accepted and second code is causing error just because i use % operator for checking parity in "if" why is it so?

First code (Accepted)

int res = a[0];
int ans = a[0];

for (int i = 1; i < n; i++)
{
     if (res < 0) res = 0;
     if ( (a[i] & 1) == (a[i-1] & 1) ) res = a[i];
     else res = res + a[i];
     ans = max(ans, res);
}
cout << ans << "\n";

Second Code (Causing error for testcase n=3 101, -99, 101 it outputs 103 but it should 101)

int res = a[0];
int ans = a[0];

for (int j = 0, i = 1; i < n; i++, j++)
{
     if (res < 0) res = 0;
     if ( (a[j] % 2) == (a[i] % 2) ) res = a[i];
     else res = res + a[i];
     ans = max(ans, res);
}
cout << ans << "\n";

the difference in both piece of code is... below line...

if ( (a[i] & 1) == (a[i-1] & 1) ) res = a[i];
if ( (a[j] % 2) == (a[i] % 2) ) res = a[i];

Please help... why don't the second code just assing the value to variable 'res' in 'if' ?

Accepted code link 233406392

Error code link 233406621

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

Будет что почитать сегодня

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

Can anyone tell me the pre requisites for the last question along with similar questions like this?

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

Can someone tell the pre requisites for the last problem? Also does anyone have similar problems like that

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

F but simpler 233608256

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

During practice, I submitted this solution for problem F.

My Code

It got accepted and is much simpler than the editorial's solution and its time complexity is O(q). Submission link: https://codeforces.com/contest/1899/submission/233759099

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

Linear O(n + q) solution of problem F 233781812

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

Linear / O(n + q) solution of problem F 233781812

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

i have a doubt plz can anyone help !!!

D). Yarik and Musical Notes

this is my code below and it is giving TLE but when i use map instead of unordered_map it accepted ,why???? :-

#include <bits/stdc++.h>
using namespace std;
#define ll long long
int main()
{  
   int t;
   cin>>t;
   while(t--){
            int n;
            cin>>n;
            int arr[n];
            unordered_map<ll,ll> m;
            for (int i = 0; i < n; ++i) cin>>arr[i];
            for (int i = 0; i < n; ++i) m[arr[i]]++;
            ll ans=0;
            for(auto i : m) ans += (i.second*(i.second-1))/2;
            ans += m[2]*m[1]; 
            cout<<ans<<endl;

   }  
   return 0;
}

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

мне нравится название 2 задачи :)

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

Can someone explain me why printf is printing zeroes ? this is submission for Alex's whims problem F

https://codeforces.com/contest/1899/submission/234314307

I changed printf to cout and it got accepted, do I need to select a specific compiler while submitting code containing printf because it worked find on my personal computer

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

D wasa bit time consuming to process, but it was definitely doable!!

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

Problem D Why does this solution give a TLE on test case 21 even after it being an O(n) solution ? https://codeforces.com/contest/1899/submission/234901875

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

Problem D: I got tle on test case 21 ......Can anyone tell me why??? int n; cin >> n;

unordered_map<int,int>mp;

for (int i = 0; i < n; ++i) { int x; cin >> x; mp[x]++; }

LL ans = 0;

for(auto &val: mp){ int x = val.second; if(x > 1) { ans += 1ll* x * (x-1) / 2; } }

if(mp[1] > 0 and mp[2] > 0) ans += (1LL * mp[1]*mp[2]);

cout << ans << '\n';

}

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

How can you get -1 in E? Can't it always be sorted. Can't understand editorial

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

can someone help me with the problem statement B? I tried a lot but couldnt find the error in my code... link to my submission:https://codeforces.com/contest/1899/submission/235506545

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

for anyone who is getting TLE on test case 37,use int instead of long long

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

Hello, would be good if someone could explain to me why my solution is getting MLE and what can be done to solve it! Thank you!

I have 2 main iterations

https://codeforces.com/contest/1899/submission/236010009

(This one I believe is due to the fact that I stored every single permutation so I switched it to a more brute force implementation below)

https://codeforces.com/contest/1899/submission/236019332

(This I am not sure why I will have a MLE)

Edit: I had a bug in my code, ignore thank you!

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

https://codeforces.com/contest/1899/submission/236063262

This one is accepted

https://codeforces.com/contest/1899/submission/236061994

This one gives a tle.

Both the codes are same just in the accepted one, 1 value of dictionary is initialized. Time complexity is same anyways. Can anyone figure out why is it happening ?

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

Can anyone please me the problem with my solution to problem C.I have used a two pointer approach. My solution

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

Can someone explain in problem G that, if we found some number of nodes with In timers within the range of T[IN],T[OUT] of x, then it assumes that the end Timers will be definitely less than T[OUT]. Can someone explain how ?

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

1899F — Alex's whims problem can be done in O(q) time,

Every day we just need d distance between any two nodes so we will first make the tree by connecting the vertices adjacent to each other, then we can just attach the nth node to the dth node this will give distance d; for ex:- for n = 5

then for d = 3, we will attach 5 to 3

1 - 2 - 3 - 4
        |
        5

so distance between 1 and 3 becomes 3

here is the submission link:- https://codeforces.com/contest/1899/submission/240359161

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

F can be solved in only O(n+q) by this solution . Given editorial is overkill for the solution.

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

From a functional perspective, understanding problem B becomes easier. We have $$$(2^a)^{(2^b)} = (2^b)^{(2^a)} \iff \frac {2^b}{b} = \frac{2^a}{a}$$$. Let $$$f(x) = \frac{2^x}{x}$$$. Clearly, when $$$a = b$$$, the equation holds true. When $$$a \neq b$$$, observing the function's graph, we find that the equation holds only when $$$x_0 = 1$$$ and $$$x_1 = 2$$$

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

Simple solution of Game with integers in Python.

def foo(): n = int(input()) count = 1 while count!= 11: if count % 2 == 1 and ((n+1)%3 == 0 or (n-1) % 3 == 0): return 'First' # elif count % 2 == 0 and count += 1 return 'Second'

if name == '__main__': t = int(input()) for _ in range(t): print(foo())

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

in 1899B , wouldn't the complexity be n*sqrt(n), instead of n*cuberoot(n)?

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

I have a solution with O(n+q) for Problem F. Solution

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

F is actually an easy problem but the problem statement is too difficult to read. It is only asking if we can form a distance d, 2<=d<=n-1 between two LEAVES. We construct a degenerate tree, which has exactly two leaves (let's index them 1 to n), then we can reconnect 1st or nth to any vertex to get distance d. (ex: moventh to (d+1)th vertex)

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

For G question, I found an easy approach of using dfs + minimum Segment tree. You can check out here, https://codeforces.com/contest/1899/submission/257742336 I got this intution because we need minimum intime of node from l to r, which have intime greater than intime of x and smaller than outime of x.

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

1899A should have mentioned that the first player has to play at least one turn. -_-