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

Автор CutieSmileHaruka, история, 5 месяцев назад, По-английски

We hope you enjoyed these problems!

2222A - A Wonderful Contest

Author: Lyz09

Hint 1
Tutorial
Implementation

2222B - Artistic Balance Tree

Author: lizhous

Hint 1
Tutorial
Implementation

2222C - Median Partition

Author: ma2021tyoi0037

Hint 1
Hint 2
Tutorial
Implementation

2222D - Permutation Construction

Author: Lyz09

Hint 1
Hint 2
Tutorial
Implementation

2222E - Seek the Truth

Author: Lyz09

Hint 1
Tutorial
Implementation

2222F - Building Tree

Author: hzy_____ Preparation: Rebex

Hint 1
Hint 2
Tutorial
Implementation

2222G - Statistics on Tree

Author: Lyz09 Developer: Z-301

Hint 1
Hint 2
Hint 3
Tutorial
Implementation

2222H - Counting Sort?

Author: CutieSmileHaruka

Hint 1
Hint 2
Hint 3
Tutorial
Implementation
  • Проголосовать: нравится
  • +97
  • Проголосовать: не нравится

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

Auto comment: topic has been updated by CutieSmileHaruka (previous revision, new revision, compare).

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

yet another implementation round

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

In C i got to the point where median of segments = median of fulll array if it exists

the trick lies in where we can compute if median of a segment = M using the comparisons which are precomputed once we fix the median

Learned something new.

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

my apparach for C

First, I precomputed the median for all prefix subarrays of odd size. Then for each recursive call, I enforced that prefix median onto the rest

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

Cool problem E.

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

Subject: Weak Test Data Report: Inefficient DP Solution Passes Time Limit in [Insert Problem Letter] — Nanatsukaze — Save Our Sound https://codeforces.me/contest/2222/problem/A

To the Problem Setter / Contest Coordinators,

I am writing to report a case of weak test data for the problem Nanatsukaze — Save Our Sound.An incredibly inefficient, Unbounded Knapsack Dynamic Programming solution is currently passing all system tests and being Accepted. The code has a worst-case time complexity that should clearly result in a Time Limit Exceeded (TLE), but it passes because the system tests are missing a specific "worst-case" scenario where the answer is "Yes" and $$$N$$$ is maximized.The Flaw in the Accepted CodeThe code attempts to check every possible score from $$$0$$$ to $$$100 \cdot n$$$ by running an unbounded knapsack DP for each target score independently.Furthermore, the code dynamically allocates a vector inside the DP function during every single iteration of the loop.

Here is the exact submission that gets accepted: https://codeforces.me/contest/2222/submission/372573074

Why it should TLE (Complexity Analysis)If the array does not contain 100 (meaning the answer is "No"), the DP quickly fails on target = 1 and breaks the loop. The execution time is negligible.However, if the array does contain 100 (meaning the answer is "Yes"), the DP loop never breaks early. It must check every number up to $$$100 \cdot N$$$.For a maxed-out test case where $$$T = 100$$$, $$$N = 10$$$, and all $$$a_i = 100$$$:The outer loop runs $$$100 \times 10 = 1,000$$$ times per testcase.can() is called $$$1,000$$$ times, allocating a vector on the heap every single time.The nested loops inside can() result in roughly $$$10,000,000$$$ operations per test case.For $$$T=100$$$, this results in $$$\approx 10^9$$$ operations and 100,000 dynamic memory allocations, which takes roughly 5 to 10 seconds locally and heavily exceeds standard time limits.

The Missing Worst-Case Test DataTo break this code, the system tests need a file filled with maximum values where the answer is "Yes".

100 10 100 100 100 100 100 100 100 100 100 100 10 100 100 100 100 100 100 100 100 100 100 ... (repeated 100 times total)

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

I find the explanation for problem C not very understandable. Can you explain in more detail how to compute dp[i] exactly.

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

    let dp[i] = max number of partitions of the subarray [0..i] into odd length segments with median = full median. dp[i] = max(dp[j]+1) over all j where j<i, the subarray [j+1..i] has odd length, and the median of [j+1..i] = full median. if all those conditions are satisfied, its a valid partition. you can check for the 3rd condition by checking if num(elements <= median) <= (i-j+1)//2 and num(elements >= median) <= (i-j+1)//2, you can use prefix sums to precompute these counts

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

My approach for C was: 1. Start walking through the array, if the median so far is as desired AND it is possible to split the rest of the array in some way to fulfill the condition, then I should cut off the array so far, sum+=1, start over with the shorter array. 2. My approach to finding out whether or not the rest of the array was splittable at all was: if the rest of the array is odd length, just check if the array as whole is valid (has correct median), if the rest of the array is even length, check for every index where we would split it into two odd-length arrays if both of these odd-length arrays are valid. If that is the case at no index, I figured, it wouldn't be possible at all.

This survived the first 3 test cases, but failed on pretest 4. I am not even quite sure which assumption is incorrect (1. or 2.). Does someone know what is wrong with my approach?

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

    I had a very similar approach, and I too am stuck at test case 4. If you find out the reason your submission failed, would you kindly let me know as well?

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

      Haven't really had much time to think about it but I wrote a test generator and used the solution's code on a little grader script:

      No test case amount provided. Setting amount to 1000 by default.
      WRONG_ANSWER on test case #157
      Test Case #157:
      1
      19
      1 5 5 8 8 7 5 6 2 3 5 5 3 7 2 5 4 5 7
      user output:
      3
      correct answer:
      5
      
      • »
        »
        »
        »
        5 месяцев назад, скрыть # ^ |
         
        Проголосовать: нравится +3 Проголосовать: не нравится

        The explanation is actually quite obvious. There is clearly no difference between numbers that are both below or both above the median. So the above input is:

        - . . + + + . + - - . . - + - . - . +
        

        If you cut off the first three elements, as - . . has the correct median, you are missing the oppotunity to include the next two + which would be benefitial, because in the section after that, there are more -s than +s. With less +, you can cut off the next group earlier. The two partitions of my idea and the correct one are:

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

C can be solved without the fact that the median of each subarray is also the median of the whole sequence. Let $$$f(l, r)$$$ be the median of $$$a_l, a_{l+1}, \dots, a_r$$$. We fix the median as $$$x$$$, and let $$$dp_i$$$ be the number of subarrays when each one has median $$$x$$$. The idea is we only iterate through all $$$j$$$ such that $$$f(j, i)=x$$$, meaning across all $$$x$$$ there will be $$$O(n^2)$$$ transitions. I'm just unsure if $$$f$$$ can be computed faster than $$$O(n^2 \log n)$$$.

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

    Yeah I had a similar idea, did anyone check for tle

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

    Yes, it is possible to compute the medians of each subarrays in $$$O(n^2)$$$. Like in the $$$O(n^2 \ln n)$$$ solution, for each start $$$l$$$, we move $$$r$$$ to the right.

    Let's maintain a frequency array, a pointer to the current median, and the number of elements strictly less than that median. When add new elements median moves by at most one position.

    So our goal effective find nearest non-zero value in frequency array (to move median). A naive scan require $$$O(n^3)$$$. So, for each element we need link to nearest non-zero neighbour – essentially maintaining a sorted linked list.

    Last, we need to find position to insert new value into this linked list. This can be precomputed: we looking for element which is closest to new value $$$a[r]$$$ among all elements in segment $$$[a_l, a_{l+1}, ..., a_{r-1}]$$$. The new value $$$a[r]$$$ will be "attached" to that element.

    Image

    submission: 372654561

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

i used a similar but a bit different method on E. To track the value of c i just constructed it bit by bit from n-1 to 0th bit.

372518426

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

I think C can be solved in O(nlogn).

Let m denote the median of the original sequence. Make a new array B s.t. Bi=-1 if Ai < m, Bi=0 if Ai=m, and Bi=1 if Ai>m Then a subarray from l to r will have m as its median if the sum of Bi from l to r is equal to 0. Or, put in another way, if we define prefB as the prefix sum of B, prefB[r] = prefB[l-1]

We can define dp[i] as the maximum number of segments constructible if we only consider prefix ending at i. Then: dp[i] = max(dp[j] + 1) for all j s.t. prefB[i]=prefB[j-1] and j has the same parity with i

We need 2 maps: one that saves dp[i] for odd i and one for even i.

The answer will be dp[n]

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

I don’t get why my solution for C got AC it should’ve been TLE

Code: 372534662

»
5 месяцев назад, скрыть # |
← Rev. 2  
Проголосовать: нравится +42 Проголосовать: не нравится

For the E bonus: In the situarion where 0 <= c < 2^n - 1 isn't it impossible? Because then we have f(x) = x for both xor and or operations if c=0.

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

fast editorial but i did not like the contest.

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

C is solvable in O(nlog^2n) with CDQ Div n Conquer

Denote m as the median of the original sequence. Make 2 new sequences P and Q.

P is defined with: P[i] = P[i-1] + (A[i] >= m ? 1 : -1)

Similarly Q is defined with Q[i] = Q[i-1] + (A[i] > m ? 1 : -1)

Then a subarray from l to r has median = m if:

P[r] - P[l-1] >= 1, Q[r] - Q[l-1] <= -1, r and l has the same parity

This is a version of 3d partial order problem that can be solved using CDQ DnC. However, we have an additional condition that r and l has the same parity. To handle this we use 2 different fenwick trees, one for odd indices and one for even.

Code

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

i didn't quite understand the editorial for problem D, can anyone explain me the proof for this part?

Consider maximizing the beauty of the permutation p. The ideal situation is that for all (i,j) where bj>bi and i<j, pi>pj is satisfied, and for all (i,j) where bj<bi and i<j , pi<pj is satisfied. ??

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

    To maximize the beauty, you want your construction to add only subarrays that have positve sum, thus if you are on index $$$i$$$ and you have prefix sum $$$b_i$$$ every $$$j$$$ $$$(j \lt i)$$$ that $$$b_j \lt b_i$$$ will give you positive score to your answer and the opposite goes to $$$b_j \gt b_i$$$

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

    Let $$$Pref_i = \sum_{k=1}^{i-1}$$$. Then $$$\sum_{k=i}^{j-1} a_k = Pref_j-Pref_i$$$. Now we will focus on contribution of $$$Pref_x$$$. The final answer will contain $$$\cdot Pref_x$$$ for each inversion $$$(i,x)$$$ (let $$$l_x$$$ be count of such inversions), and $$$-Pref_x$$$ for each inversion $$$(x,i)$$$ (similarly denote their count by r_x). Suppose there is $$$p_x$$$ on position $$$x$$$. Note, that if there are $$$r_x$$$ numbers smaller than $$$p_x$$$ on the right of position $$$x$$$, then there are $$$p_x-1 - r_x$$$ numbers smaller than $$$p_x$$$ on the left of position $$$x$$$, so there are $$$(x-1) - (p_x-1 - r_x)=x-p_x+r_x = l$$$ numbers larger than $$$p_x$$$ on the left of position $$$x$$$. So $$$\sum_{k=i}^{j-1} a_k = \sum_x l_x\cdot Pref_x - r_x \cdot Pref_x = \sum_x(x-p_x+r)\cdot Pref_x -r\cdot Pref_x = \sum_x (x-p_x)\cdot Pref_x = \sum_x x\cdot Pref_x - \sum_x p_x\cdot Pref_x$$$. So $$$l_x, r_x$$$ canceled out meaning the contribution from each position depends only on number we place on this position. Now the firt sum is constant, and to minimalize second sum we can use rearrangement inequality. Thats why we need to pair largest numbers from permutation with smallest $$$Pref_x$$$.

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

I used $$$a=2^n-1$$$ in the solution of Problem E. You can check out my code here 372655599, if it concerns you.

Note: Added some comments to make it easier to understand.

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

can someone tell me when we are creating a new graph,

and two nodes have same color,

what is the time required to connect to those nodes ?

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

Nice problem E!

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

On H it is kind of funny that $$$g(a)$$$ is deceptively small (at most $$$10$$$, proving that is kinda cool to build intuition) but it isn't useful at all to solve the problem.

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

全身全霊!MORE MORE JUMP!!

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

It's Nanatsukaze Round!

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

Here is a method to solve Problem C within a complexity of $$$O(n\log^2n)$$$。 First, note that the median of each partition is the same and fixed, which we denote as $$$x$$$. For other numbers, we only care about whether they are greater than or less than $$$x$$$.

Let $$$f_i$$$ denote the maximum number of segments into which prefix $$$i$$$ can be divided. $$$f_j$$$ can transition to $$$f_i$$$ if and only if the length of the interval $$$[j+1,i]$$$ is odd, and the number of numbers in the interval that are greater/smaller than $$$x$$$ is less than half the length of the interval. Now we have achieved a time complexity of $$$O(n^2)$$$.

Let $$$s_i, t_i$$$ denote the number of numbers greater/smaller than $$$x$$$ respectively for prefix $$$i$$$. The condition can be written as:

  • $$$j \lt i$$$
  • $$$i-j\equiv 1(\bmod 2)$$$
  • $$$2(s_i-s_j) \lt i-j$$$
  • $$$2(t_i-t_j) \lt i-j$$$

Without the restriction of parity, this is a well-known three-position partial order problem, which can be solved using the divide-and-conquer approach.

Taking into account the parity constraints, we consider maintaining two data structures to represent odd and even numbers during the divide-and-conquer process. During the merging process, only transfers between different sets of data structures are allowed. Thus, we have solved this problem with a time complexity of $$$O(n\log^2 n)$$$.

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

okak

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

Can anyone please explain, why the solution explained in problem D maximizes the answer? Because even if the value we add from every inversion [i; j] is not negative, how can we know that this guarantees biggest outcome?

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

    hi

    the maximum value we can achieve is the sum of all intervals with a positive sum without the last element [as the last element can never be included ]

    there can at max be n*(n -1)/2 pairs of positive sum sub arrays (and inversions)and we can graph them and we can choose weather to add a inversion (i j) yes or no.

    so for each pair of indexes we choose to add them if they are positive so max answer will be sum of all such pairs.

    you can create a directed acylic graph if you add an edge between i and j is sum between them is positive and when you run top sort on this you will get a array (topo) such that the value of ans[topo[i]] > ans[topo[j]] if i < j.

    link to code

    here is a submission i made with the idea of writing a divide and conquer methord like merge sort. although final code uses sorting as sort function is an inbuilt merge sort of a kind .

    i am thinking of another version using a segtree where we build a chain (linked list but with faster ability to swap and insert elements ) whern the ith element is inserted before the maximum right most prefix val before i -1 <= to it. although i am struggling to formulate this approach