eren__'s blog

By eren__, 12 months ago, In English

Thank you for participating! We hope you enjoyed the problems and, most importantly, had fun!


2102A - Dinner Time

Hints
Solution
Code (Python 3)
Bonus

2102B - The Picky Cat

Hints
Solution
Code (Python 3)
Bonus

2101A - Mex in the Grid

Hints
Solution
Code (Python 3)
Bonus

2101B - Quartet Swapping

Hints
Solution
Code (C++)
Bonus

2101C - 23 Kingdom

Hints
Solution
Code (C++)
Bonus

2101D - Mani and Segments

Hints
Solution
Codes
Bonus

2101E - Kia Bakes a Cake

Hints
Solution
Codes
Bonus

2101F - Shoo Shatters the Sunshine

Hints
Solution
Code (C++)
Bonus
  • Vote: I like it
  • +185
  • Vote: I do not like it

»
12 months ago, hide # |
 
Vote: I like it +7 Vote: I do not like it

Add it in contest material or announcement blog. I was waiting for this

»
12 months ago, hide # |
Rev. 2  
Vote: I like it +4 Vote: I do not like it

This is my submission for 2101B. Plz can someone help me where this goes wrong. I've followed a simple logic, with bruforce. I made a recursive function, which takes vector v and i as parameters, the task of the function is to sort all the elements with the same parity as i, starting from the i till the end of array, using the possible operations. I also try to make sure these operations don't mess with the part of array already we took care off. Hope you get it.

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

    No help :(, I tried looking for reason with chatgpt and Gemini but unfortunately they couldn't figure out the right thing. I'd be grateful if someone could help.

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

    ur solution is basically greedy,it wont always find the optimal answer,just compare the test cases where it failed with the right answer and dry run it to understand why,also the time complexity is bad

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

      I tried doing that, I'm not able to find the test case where it fails.I don't know what greedy means, but I don't know why it might fail? At every point we are sorting and going forward, I'm not able to see the logical flaw.Ya time complexity is bad,and I understood the optimal way,I'm just not able to find what's wrong in my solution.

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

        scroll down on ur submission,on the left side u can see

        "Click to see test details"

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

          It's not showing there, that's I'm asking here, it shows ..... after some point and the test case which fails is in that .... :(

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

            Ah,I see sorry I didn't notice..

            I just asked chatgpt(it took 2 tries) to give a case where your code would fail and here:

            1

            6

            2 3 4 5 1 6

            if im not wrong,doing ur code n times repeatedly from the start should give the right answer but yea then time complexity will get even worse..btw for the optimal solution I found 15-puzzle vid by numberphile kinda help me a bit-in a way,u can watch it

            try dry running on it on paper,btw I think u should learn what greedy means,it is a pretty important and easy to learn topic,learn fractional knapsack and 0/1 knapsack(in this greedy fails and u gotta use dp,learn why greedy fails).

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

              Aah i see, there was minor error that in each iteration I was not just sorting the elements of i parity but also trying to sort other elements also which gave wrong answer, not just changing j++ to j+=2 in the inner loop of bubble sort, gives right answer,it's not optimal solution but works.

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

                awesome,also,learn how constraints can be used to determine time complexity,you won't waste your time on writing code which wouldn't have qualified anyways

»
12 months ago, hide # |
Rev. 3  
Vote: I like it -30 Vote: I do not like it

Div2 F>>>>E? I solved F used about 40 minutes but E about 80 minutes. Maybe because Chinese are good at data structures but bad at greedy.

Div1 ABCE's observations are interesting, especially E.

The next observation is that we cannot see any node twice in a nice path (in other words, there are no cycles).

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

I got TLE two times on The picky cat, my submission did exactly whats told in the editorial, I just checked if its less than med+1 the position. I have two questions, first that how am I getting a tle when I the time complexity is just O(n), also when I removed everything from the entire code except the solutions, this logic of index <= median +1, gave wrong answer so I solved for even and odd n separately, which then gave correct answer, why is that the case? any help would be appreciated.

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

    I don't think, your solution has O(N) time complexity.

  • »
    »
    12 months ago, hide # ^ |
    Rev. 2  
    Vote: I like it +3 Vote: I do not like it

    So you were asking for Picky Cat Problem error which was causing TLE, error part in the code is:

    if (n == 1) {

    cout << "YES"<<endl;
    
    return 0;

    }

    Actually once you hit n=1 case, you didn't take further input for "temp" variable, but the checker is giving you that input, so that causes some error as you exited/returned before taking the complete input

    Make sure to take complete input then only return.

    In the first TLE submission you made you didn't take abs(res), I think you got that one later.

    For the even/odd thing just check some examples like n=4, n=5 how many have to be smaller etc, would realise that you had to make cases. The tutorial had floor/ceil in solution, so need to take care of it.

»
12 months ago, hide # |
Rev. 2  
Vote: I like it +20 Vote: I do not like it

This editorial for F looks overkill with calling a lot of stuff special, maybe after waking up I'll change my mind. I solved it using the following ideas:

  1. It's well known that when looking for a max path between two sets of vertices we can reduce each set to the endpoints of some diameter of it. The first 1/3 of the editorial seems to be proving that the center is always in any diameter but that's just well known.

  2. Fix the center of the blue diameter and solve it for that center. The center might be a vertex or an edge. Then to make that the actual center there must be at least 2 vertices that are blue in different subtrees at the lowest height that has some blue vertex (or the root in case it's a vertex center). Now the max distance between blue and red is just the distance of blue to the center + distance of red to the center (maybe +1 for the edge center case).

  3. To compute the thing above you can choose the color for vertices in reverse bfs order going out of the center. This conveniently gives us vertices of same height as a subarray and edges of same height and same subtree also as a subarray. Then it's a matter of dp going brr, the state will be the number of stuff that has been chosen and a state that's the cartesian product of {no blue vertex yet, first blue in current subtree, first blue at same height but different subtree, two blues done} x {no red, some red} while maintaining count of such states and the sum of their distances.

The edge center vs vertex center cases differ by not a lot, it changes some choices for the center in the last few steps of the dp and how you initialize the bfs (for one vertex just do it and for an edge start with the queue having both vertices with height 0).

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

    I don't understand editorial of F at all. your idea seems to be more understandable but I still have difficult in understanding it.

    I've searched the answer for days and got none. the only charted illustration on ytb is in Japanese. I don't understand the language but I watched through and still cannot get it.

    Can you please share the thoughts with video and whiteboard? thx in advance.

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

    it looks like a pretty straightfoward answer, but I just haven't had the technique to convert the counting problem into a solution by monotonic stack etc..

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

Who can give the sufficiency proof for Div.2 D(Div.1 B)?

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

    What do you mean by sufficiency proof? Could you clarify?

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

      Why can this construction scheme be obtained through several operations?

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

      hope my poor English can make you understand.

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

        It is really quite straightforward. It just requires you to try it, and brute force it.

        You can follow a procedure of setting up the final array from the beginning of the array. Since we are moving 2 elements at a time, none of the elements can change the parity (i.e. odd-ness/even-ness) of its index. At any partially completed stage, just look at all the elements (to the right, after the partially completed bit), that are in the correct modulo-positions, take the smallest of them, and you will just be able to simple move it into position, with zero resistance, or complications. Keep repeating the procedure. But when you reach a stage, where you have only 3 elements left, there will be nothing more you can do.

        The proof that there is nothing you can do, once you reach a stage where only 3 elements are left is as follows: The (n-2) element is already the ONLY one candidate for its modulo-positions. And the maximum you might be able to do, is to swap the (n-3) and (n-1) elements. But this is also NOT possible, since the parity of the number of inversions of the original array will be changed by such a candidate swap, it is impossible because the 2-swap weird operation given in the question, preserves the parity of the number of inversions.

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

          That's true.

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

            I think the original question was different than what is answered, the proof would be sufficient if it can be proven every possible permutation is achievable keeping the parity of inversions same.

        • »
          »
          »
          »
          »
          11 months ago, hide # ^ |
          Rev. 2  
          Vote: I like it 0 Vote: I do not like it

          why would the parity of inversions be same ? I thought the exact inversions in both odd and even array are going to be same and not the parity thing here.. Can you please help me and provide me a case where after applying some operations the parity remains same but inversions in odd and even array differ ?

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

            as the given array is a permuation,replacing abcd with cdab will either +2 ,+0,-2 the inversion count thus preserving the parity of inversion

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

For div1C, is $$$O(nlog^2n)$$$ not allowed? I realized after contest we can do it in $$$O(nlogn)$$$, but this $$$O(nlog^2n)$$$ submission passes in 4.5s while the time limit was set to 4s.

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

    I got one submission to pass with some optimizations, but sometimes it takes 4.5s and sometimes it takes 3.8s, which is very strange. Maybe you could improve it with golden-section search instead of binary search, but I don't know how to do that.

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

      You mean this? Haven't read this, so will look check it out, thanks.

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

        I also used ternary search. Got 3.8s result with std::set and 1.1s with std::priority_queue. Tried to optimize further via golden section, but failed, probably because of rounding to integers. If you will manage implement golden section for integers, please share.

  • »
    »
    12 months ago, hide # ^ |
    Rev. 2  
    Vote: I like it +21 Vote: I do not like it

    This is an $$$O\left(n\log^2n\right)$$$ solution using std::multiset. 319333985

»
12 months ago, hide # |
Rev. 4  
Vote: I like it 0 Vote: I do not like it

Can someone explain why the equality of parity of number of inversions in even and odd arrays checked ? In problem D.(I don't understand why it is considered for checking)

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

    Observe that an operation on any index can alter the number of inversions only by $$$0$$$, $$$2$$$ or $$$4$$$. You can consider all $$$24$$$ possible orders of four consecutive elements, but the relative order of the first and second elements, as well as third and fourth, does not matter, so the number of cases reduces to $$$6$$$.

    1. $$$a_{i} \lt a_{i+1} \lt a_{i+2} \lt a_{i+3}$$$. There are no inversions before the operation and $$$4$$$ afterwards ($$$a_{i+2} \gt a_{i+1}, a_{i+2} \gt a_{i}, a_{i+3} \gt a_{i+1}, a_{i+3} \gt a_{i}$$$).
    2. $$$a_{i} \lt a_{i+2} \lt a_{i+1} \lt a_{i+3}$$$. There is $$$1$$$ inversion before the operation ($$$a_{i+1} \gt a_{i+2}$$$) and $$$3$$$ afterwards ($$$a_{i} \gt a_{i+2}, a_{i+1} \gt a_{i+2}, a_{i+1} \gt a_{i+3}$$$).
    3. $$$a_{i+2} \lt a_{i} \lt a_{i+1} \lt a_{i+3}$$$. There are $$$2$$$ inversions before the operation ($$$a_{i} \gt a_{i+2}, a_{i+1} \gt a_{i+2}$$$) and $$$2$$$ afterwards ($$$a_{i+1} \gt a_{i+2}, a_{i+1} \gt a_{i+3}$$$).

    The other three cases are derived from these by swapping states before and after the operation. So the parity of number of inversions is the invariant.

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

    More generically: it is known that any swap changes the parity of the number of inversions in a permutation. Since our operation does two swaps, you know the parity must remain constant.

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

      I am a newbie to cp , the first observation of index parity was easy to spot for me but i wasn’t able to get the second observation of inversion parity thing neither i was aware of such terms so i wanted to ask is it ok to learn these things as we solve the problems or is it bot ao hard to spot such observations without knowing about even swaps results in same parity of inversion this is an example there are numerous such things that i dont know about and they might come in next contest say

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

        I think most people learned about inversion parity from a past problem that needed it — looks like this is that problem for you.

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

      I agree that the parity of total inversion count of the array should remain constant but what about the parity of only odd and even index positioned elements that is there in the solution.

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

is this correct for C: 8 9 10 11 7 0 1 2 . 6 5 4 3 . . . . . . I mean like a number spiral but it starts from middle

»
12 months ago, hide # |
 
Vote: I like it +8 Vote: I do not like it

i spent a lot of time stuck trying to make sense of counting inversions in div2D/div1B but then i realized theres a easier way to solve:

suppose we have elements wxyz .. ab:

  • if we want to swap w and a, we can just do wxyz..ab -> abyz..wx
  • if we want to swap w and b, we can do wxyz..ab -> wabz..xy -> bzwa..xy

corner case: if we have xyab we can do xyab -> abxy

so i just maintained an index lookup table and a list of odd and even parity elements and swapped them greedily for every i from 0 to n-3 (one-indexed) and got the AC :3

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

Great editorial.

By the way, this round is much better than CodeForces Round 1000 (Div. 2)! I think your problem can deserve this historic round! Every problem is interesting, thank you!

sorry for my bad english. XD

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

Can someone explain the parity solution for 1B/2D? I used greedy and swaps in my solution

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

    Imagine the swap abcd -> cdab. Exactly four pairs change their order:

    1. ac swapped to ca
    2. ad swapped to da
    3. bc swapped to cb
    4. bd swapped to db

    All other pairs stay in their order. Now let's look at what those 4 changes can do to the number of inversions:

    • 4x +1 and 0x -1 = +4
    • 3x +1 and 1x -1 = +2
    • 2x +1 and 2x -1 = 0
    • 1x +1 and 3x -1 = -2
    • 0x +1 and 4x -1 = -4

    This means a swap can change the number of inversions by +4, +2, 0, -2 or -4. This also means that if the number of inversions is odd initially, then swaps will never change that, and if the number of inversions is even initially, then swaps will keep the number of inversions even.

»
12 months ago, hide # |
 
Vote: I like it +8 Vote: I do not like it

E with maximum segment tree + binary search 319356051

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

I am screwed, B was damn easy idk what happened to me i was just overwhelmed tomorrow by A , i screwed damn damn damn damn

»
12 months ago, hide # |
 
Vote: I like it +8 Vote: I do not like it

I need to trust my intuition more. Lost so much time on A.

»
12 months ago, hide # |
 
Vote: I like it +20 Vote: I do not like it

I quite liked Div. 1 C!

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

I solved Div.1 C with hard way:tripartite(3 parts) search.I searched for how many pairs of numbers should I use.I gussed the answer will increase than decrease,so I used tripartite search

Link

Seems that just using greedy is OK.I should find the largest pairs of numbers that we can use

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

Div.1 A Bonus: I think that first for every x,I count how many subgrids have MEX that >=x,this is (left-up counts)*(right-down counts).For example,if n==5,9*9=81 subgrids have MEX that >=0

Then I use inclusion-exclusion to count how many subgrids have MEX that ==x,so I can know the sum of MEX of all subgrids

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

    You don't need inclusion-exclusion, the sum of all $$$\mathrm{MEX}$$$ values can just be written as $$$v_1 + v_2 + \dots + v_{n^2}$$$, where $$$v_i$$$ is the number of subgrids with $$$\mathrm{MEX}$$$ more than or equal to $$$i$$$ (this is because a grid $$$\mathrm{MEX}$$$ equal to $$$k$$$ will be counted exactly $$$k$$$ times: once in each of $$$1, 2, \dots, k$$$), so you can simply take the sum of all the values you calculated.

»
12 months ago, hide # |
Rev. 9  
Vote: I like it +11 Vote: I do not like it

Div2D with sorting+dfs O(nlogn)319316859
Observation:The key is element at odd/even position will remain it parity after swap,so we can just directly sorting the element at odd and even position but there is obviously a counter example [1,4,3,2],so for last four element it exist special condition need to be handle.

Assume that a[n],a[n-1],a[n-2],a[n-3] is the last four element then a[n-3]<a[n-1] always handle and there is two possibly answer which is [??,a[n-3],a[n-2],a[n-1],a[n]] or [??,a[n-3],a[n],a[n-1],a[n-2]]

Proof

The key is parity of permutation,
Defination of the parity of permutation:The parity of inversion in the permutation. So obviously a[n] is not equal to a[n-2] so they may have a inversion or not,this imply the parity of two possible answer will different.
Claim 1:Swap operation in this problem does not change the parity of permutation

Proof

From claim 1 we know that we can count the parity of original permutaion as v1,and count the two possibly permutation as u1 and u2 and just compare v1 to u1,u2 then we can know which one is the answer,but naive solution to count the inversion cost O(n^2),so we need to optimize our algorithm
I will introduct functional graph,i believe most of you already know it if you regard p[i] as a directed edge from i to p[i] then you will get a functional graph,and one of the properties of functional graph is every disjoint connected component will exist exactly one loop(may be self loop) so the permutaion will become a set of disjoint cycle,but how it related to the problem?
Claim 2:for a permutation,for any swap between two element it will change the parity of the permutation

Proof

From claim 2,I think this one is also a well known problem,given you a permutation,how many swap to make the permutation identity?Let ∂i be the number of cycle with length i then the answer for cycle length i is (i-1)*∂i,why?

Proof

So this the total swap operation to make original permutation identity is ∑(i-1)∂i
Claim 3,The parity of the permutaion is the n-number of cycle

Proof

So the parity of the permutation is n-c,so there is a lot of algorithm to count the number of cycle like dfs/bfs/dsu
Thank for your reading

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

If confused for Div 2 A shouldn't there be n — p + 1 blocks? Just like the first test case for n = 3 and p = 2

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

    The editorial means non-overlapping blocks, because it is calculating the sum of all elements in the array.

»
12 months ago, hide # |
Rev. 2  
Vote: I like it +18 Vote: I do not like it

For the Bonus for d1A, isn't it for each (i1,j1)(i2,j2), we do (i1+1)(j1+1)(n-i2)(n-j2)(k), where i1,i2,j1,j2 is smallest subgrid to contain mex k, we do that for each k > 0, is that right?

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

tks bro

»
12 months ago, hide # |
Rev. 2  
Vote: I like it +30 Vote: I do not like it

I hate geeksforgeeks, I copied their inversion count for div2 d and forgot to change the int type to change to long long which passed the pretest....... fell from 130 to 1300+ sedlyf

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

Hey everyone, I need some help. In the Bonus Section of Problem 2101B — Quartet Swapping, It's given: "Do you know how to count the parity of inversions without counting the number of inversions?" Can anyone help me out with this?

»
12 months ago, hide # |
 
Vote: I like it +16 Vote: I do not like it

A different and (I think) simpler approach for 2101D: (I got this idea in the contest but failed to find the way of implementation)

We call an array acute if either the first or last element equals the maximum or the minimum of the array. An array is cute if all of its subarrays are acute.

We call an array lcute if the first element equals the maximum or the minimum of the array and rcute if the last element equals the maximum or the minimum of the array. These two can be found using 4 monotonic stacks. Then we can extend the rcute bounds to cute bounds (I don't know what name of the algorithm is but it works). Time complexity $$$O(n)$$$.

#include <iostream>
#include <tuple>
#include <vector>
#include <bitset>
#include <algorithm>
#include <set>
#include <map>

#define pii pair<int, int>
#define ll long long

using namespace std;

int n;

int a[200010];

// 2 1 4 3
// 3 1 4 2
// vector<pii> ban;
int lm[200010];
int lM[200010];
int rm[200010];
int rM[200010];
int lf[200010];
int rt[200010];

// int xds[800010];
// int tag[800010];

// void Update(int p) {
//     xds[p] = max(xds[p << 1], xds[p << 1 | 1]);
// }

// void Build(int p, int l, int r) {
//     tag[p] = 0;
//     if (l == r) {
//         xds[p] = lf[l];
//         return;
//     }
//     int mid = (l + r) >> 1;
//     Build(p << 1, l, mid);
//     Build(p << 1 | 1, mid + 1, r);
//     Update(p);
// }

void Hebing() {
    vector<vector<int>> rp(n);
    for (int i = 0; i < n; i++) {
        rp[lf[i]].push_back(i);
    }
    int cc = n;
    for (int i = n - 1; i > -1; i--) {
        cc = min(cc, i);
        while (cc && rt[cc - 1] >= i) {
            cc--;
        }
        for (const auto& x : rp[i]) {
            lf[x] = min(lf[x], cc);
        }
    }
}

void Solve() {
    cin >> n;
    for (int i = 0; i < n; i++) {
        cin >> a[i];
    }
    for (int i = 0; i < n; i++) {
        lm[i] = i - 1;
        while (lm[i] > -1 && a[lm[i]] > a[i]) {
            lm[i] = lm[lm[i]];
        }
        lM[i] = i - 1;
        while (lM[i] > -1 && a[lM[i]] < a[i]) {
            lM[i] = lM[lM[i]];
        }
        lf[i] = min(lm[i], lM[i]) + 1;
    }
    for (int i = n - 1; i > -1; i--) {
        rm[i] = i + 1;
        while (rm[i] < n && a[rm[i]] > a[i]) {
            rm[i] = rm[rm[i]];
        }
        rM[i] = i + 1;
        while (rM[i] < n && a[rM[i]] < a[i]) {
            rM[i] = rM[rM[i]];
        }
        rt[i] = max(rm[i], rM[i]) - 1;
        // cout << i << ',' << rt[i] << endl;
    }
    // set<int> vis;
    // for (int i = 0; i < n; i++) {
    //     vis.insert(i);
    // }
    // for (int l = 0; l < n; l++) {
    //     int r = rt[l];
    //     auto itl = vis.lower_bound(l);
    //     auto itr = vis.upper_bound(r);
    //     for (auto it = itl; it != itr; vis.erase(it++)) {
    //         auto x = *it;
    //         lf[x] = min(lf[x], l);
    //     }
    // }
    Hebing();
    ll res = 0;
    int cc = 0;
    int l = 0;
    for (int i = 0; i < n; i++) {
        l = max(l, lf[i]);
        // cout << lf[i] << ',';
        res += i - l + 1;
        // cout << i << ',' << l << endl;
    }
    // cout << endl;
    // ban.clear();
    // ll res = 0;
    // int l = 0;
    // set<int> cur;
    // vector<int> lf(n);
    // vector<int> rt(n);
    // for (int r = 0; r < n; r++) {
    //     cur.insert(a[r]);
    //     while (a[r] != *cur.begin()
    //         && a[r] != *cur.rbegin()) {
    //         cur.erase(a[l]);
    //         l++;
    //     }
    //     lf[r] = l;
    // }
    // cur.clear();
    // int r = n - 1;
    // for (int l = n - 1; l > -1 ;l--) {
    //     cur.insert(a[l]);
    //     while (a[l] != *cur.begin() && a[l] != *cur.rbegin()) {
    //         cur.erase(a[r]);
    //         r--;
    //     }
    //     rt[l] = r;
    // }
    // for (int i = 0; i < n; i++) {
    //     cout << lf[i] << ',';
    // }
    // cout << endl;
    // for (int i = 0; i < n; i++) {
    //     cout << rt[i] << ',';
    // }
    // cout << endl;
    // int cc = 0;
    // for (int i = 0; i < n; i++) {
    //     while (cc <= rt[i]) {
    //         lf[cc] = min(lf[cc], i);
    //         cc++;
    //     }
    // }
    // for (int i = 0; i < n; i++) {
    //     cout << lf[i] << ',';
    // }
    // cout << endl;
    // for (int i = 0; i < n; i++) {
    //     res += i - lf[i] + 1;
    // }
    cout << res << '\n';
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    int T;
    cin >> T;
    while (T--) {
        Solve();
    }
    return 0;
}
  • »
    »
    12 months ago, hide # ^ |
     
    Vote: I like it 0 Vote: I do not like it

    Hi,you can use Hanzi(or Chinese characters) instead of Pinyin in coding.Compilers have supported Unicode long ago.

    Make sure the code is encoded in UTF-8 then you can use Hanzi.And with compilation command -finput-charset=UTF-8 -fexec-charset=UTF-8,we can input or output Unicode characters

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

      Using Chinese characters should always be avoided in programming (even in comments), no matter whether it is in Codeforces or not. I got some fatal errors in my game in the past because of using characters that are not in ASCII.

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

    Oh yeah,the time complexity is O(n). And I don't know whether this can be hacked.Seems it's right

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

In Problem div.2 C, can we count distinct matrices which maximises the sum of mex over all subgrids ?

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

A much simpler solution (in my opinion) exists for Div2D/Div1B.

If we want to bring an element with index i to index j where i>j && i%2==j%2, we just keep swapping back until we get it to j.
Now we see that we need to shift all the elements which will take a lot of time. So to get rid of shifting we would shift the pair of elements which was originally at j,j+1 to i,i+1 using swaps.
This means we can actually just swap elements at i,i+1 with j,j+1.

So we do a simulation for 1<=i<=n-3 each time we bring the minimum possible element to i with swaps.

»
12 months ago, hide # |
 
Vote: I like it +10 Vote: I do not like it

My solution 319241757 for Div2D/Div1B does not rely on inversions.

Every allowed move flips the “even‑part” inversion parity and the “odd‑part” inversion parity together, So parity_even ⊕ parity_odd is preserved.

What I did?

  1. Split the permutation into two mini‑arrays:

    • positions 0,2,4,…   → EVEN block
    • positions 1,3,5,…   → ODD  block
  2. Work out the inversion parity of each block. (ele_count — cycle_count)

    parity = 0 ➜ even permutation
    parity = 1 ➜ odd  permutation
  3. Two cases:

    ▸ Parities already match
    → Just sort each block independently and weave them back together.
    (This is lexicographically optimal because every slot gets the smallest possible value still legal for its parity class.)

    ▸ Parities differ
    → We must flip exactly one block’s parity.
    The cheapest way to do that is a single swap of its two largest elements (they sit at the end, so they disturb the prefix the least).
    (Try flipping the last pair from EVEN and ODD block, and keep the lexicographically smaller result.)

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

    I had got everything correct but had not checked the inversion parity (WA 2Nd), I did not understand the reason for checking the inversion parity. could you please explain that. Thanks a lot!!

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

      Take the case of N=4 and A ={1,4,3,2}.

      what would be your output and what should be the expected?

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

      The reason is consider, we have 5 4 3 2 1 permutation what will be the best possible smallest lexicographical array obviously it is 1 2 3 4 5. Now Let's see what happens when we perform an operation mentioned in the problem and how it behaves. Elements whose positions have same parity that means elements at odd position will always stay in odd positions and same is true for elements at even positions. Now if we wanted to do an operation on (i and i+2) then (i+1 and i+3) or (i+1 and i-1) should be swapped that means one operation leads to 2 swaps 1 on odd positions and 1 on even position. That means to achieve the best possible permutation total swaps happened together(on odd and even positions) will be even and let say it as Y now consider a1 is no of swaps performed on even positions and a2 on odd positions to achieve the optimal solution then (Y = a1 + a2 ) here Y should be even right then when will sum of 2 numbers become even ? obviously when a1 and a2 both have same parity. But here the question is how can we decide the a1 and a2 parity? For this let's consider an example (3, 2, 1) let's try to sort this in increasing order but we can only swap the adjacent elements then it takes at least 3 swaps to sort it, now try to sort (3, 2, 1) with more than 3 swaps you can see whenever the array become sorted after x operations then x will always be odd. That means parity of no of swaps remains constant. So using this observation we can count the minimum no of swaps required to sort the elements at odd and even positions (a1 & a2) separately as parity remains. Hence, we can say that we will achieve the best possible answer if and only if parity of inversions or swaps is same otherwise, we can just revert one swap on the best possible answer so that it will satisfy the parity of both a1 and a2.To achieve this we reverse last n and n-2 elements why because they are the large elements. This problem is related to Invariant problems and all above observations are invariant properties we need to observe while solving. Sorry for my bad English.

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

Problem- B

Can someone tell testcase where it is failing — Submission

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

Someone pls explain the editorial of div2 E, i am unable to understand it

»
12 months ago, hide # |
 
Vote: I like it +24 Vote: I do not like it

This is a copy-pasted comment from the announcement. I just want to share "simpler" and more straightforward solution for D1.

I have $$$O(n)$$$ solution for D1.

Observation: if $$$p[1...n]$$$ is cute, then $$$p[1...{n-1}]$$$ and $$$p[2...n]$$$ are cute as well. So, we can use two-pointers to calculate intervals $$$(i, r_i)$$$, where $$$(i, r_i)$$$ is the longest cute subarray starting at $$$i$$$.

To do so, we have to maintain LIS and LDS in deque (to simulate two pointers), but notice that $$$(i, r_i)$$$ is cute, meaning that LIS + LDS $$$ = r_i - i + 2$$$ (this fact simplifies problem).

Since, we only considering cute subarrays adding $$$p_j$$$ to the back of the deque is easy, since LIS or LDS must extend by element $$$p_j$$$, and non-extending longest subsequence can only change its last element.

Deleting $$$p_i$$$ from the front of the deque is a bit harder, since it is possible for $$$p_i$$$ to be a beginning for both LIS and LDS. In this case, we can proof that after erasing $$$p_i$$$ we always can extend LIS/LDS in the front by the first element of LDS/LIS.

And the last case occurs when LIS = 1 or LDS = 1 (but not at the same time), in this situation the one having single element must change it to the last element of the other one.

My solution: 319304301

»
12 months ago, hide # |
Rev. 4  
Vote: I like it +6 Vote: I do not like it
$$$\bf{D1C}$$$

could be solve using Binary Search

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

Based on what's i've understood from D's solution Even if the question was modified to be: "Swap any two elements from odd indices 1..3..5 but you've to swap any two even indices from 2..4..6 also" in one operation the solution would be the same. Since the parity of the number of operations does not change.. regardless if you're just swapping adjacent elements or any two elements.

eg: 4 1 3 5 2 : Odd indices would require 1 swap if we can swap any two odd indices (1&5) but if adjacent odd indices allowed, it would require 3 swaps.

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

Dear @Shoo @eren__ @sweetweasel @MikeMirzayanov , In the recent codeforces round 1024 Div. 2 contest, i received a mail regarding my C solution https://mirror.codeforces.com/contest/2102/submission/319277228 that it coincided with many other solution. This was purely a coincidence as I have previously solved a similar question on leetcode, here's my submission link for leetcode https://leetcode.com/problems/spiral-matrix/post-solution/?submissionId=1563392392. Traversing a spiral matrix was a standard question on leetcode and it's not difficult to see that subtracting each element will give me the required answer, the logic was completely mine. i have given enough evidences can you pls reconsider about my situation.

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

I had a bit different solution for D1D. The observation that there is some position $$$i$$$ that is in both sequences is still important. We know, that elements to the left of $$$i$$$, that are also $$$ \gt a_i$$$, ascend if going from $$$i$$$ to the left. Similarly elements $$$ \lt a_i$$$ descend. Let's find the first position $$$L_i$$$, when such property fails. Suppose $$$a_{L_i} \gt a_i$$$. Then there is position $$$L_i \lt j \lt i$$$, such that $$$a_{L_i} \lt a_j$$$. Let's find the closest greater element to the right of an element, for all of them. Let's call it $$$rge_i$$$. Then $$$L_i = \max j: a_j \gt a_i, rge_j \lt i$$$.

We can think of this as points on a 2D plane: $$$(i, a_i)$$$. We need to find the maximal $$$j$$$ such that $$$(a_j, rge_j)$$$ is higher and to the left of point $$$(a_i, i)$$$. This can be found via scanline with segment tree or fenwick tree.

What if $$$a_{L_i} \lt a_i$$$. We can do similar thing. Only now we need to find $$$rle_i$$$ — the closest integer to the right that is less that $$$a_i$$$. Similar scanline can be done, so we can find $$$L_i$$$.

To find $$$R_i$$$ we do 2 more scanlines (try not to bug while copypasting). Now we know, that all segments $$$[l, r]$$$ such that $$$L_i \lt l \le i \le r \lt R_i$$$ are good. In order to find the total number of segments we can find the area of unity of rectangles $$$(L_i + 1, i, i, R_i - 1)$$$.

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

I wonder why... I did see much difference between my code and the ac solution...but I got WA

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

For problem C of this competition, I guessed the answer was a spiral matrix. After verifying it for a while, I directly used the code I had previously submitted on Luogu. However, it was skipped due to similarity.https://www.luogu.com.cn/problem/P2239

»
12 months ago, hide # |
Rev. 2  
Vote: I like it +5 Vote: I do not like it

In my opinion D2B was easier than D2A, I got 5 "wrong answer on pretest 2" until I got A right

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

My code in java at https://mirror.codeforces.com/contest/2102/submission/319616903 got run time error, it exit with code 11, can anyone help me?

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

I am wondering if there is a solution for F that, for each coolness value, calculates the number of tree colorings with this coolness value. I only know how to solve this in $$$O(n^2 \log^2 n)$$$ time.

»
12 months ago, hide # |
Rev. 3  
Vote: I like it 0 Vote: I do not like it

and therefore always exactly one super-special node if there exists at least one special node.

I don't quite understand, if there's only one red and one blue node(obviously there will exist special nodes on the path between them), will there exist a super-special node?

eren__ pls answer, thx.

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

Div.2 B can be written in Python simpler.This is the style of Python

T=int(input())
for _ in range(T):
    n,=map(int, input().split())
    a=list(map(int, input().split()))
    cnt=sum(1 for x in a if abs(x)>abs(a[0]))
    # cnt=len(list(filter(lambda x:abs(x)>abs(a[0]), a))) # or this
    if cnt>=(n-1)//2:
        print("YES")
    else:
        print("NO")
»
12 months ago, hide # |
Rev. 2  
Vote: I like it -8 Vote: I do not like it

Dear Shoo, eren__, sweetweasel, MikeMirzayanov, In the recent codeforces round 1024 Div. 2 contest, I received an email regarding my C solution https://mirror.codeforces.com/contest/2102/submission/319271302 that it coincided with many other solution. This was purely a coincidence as I have previously solved a similar question on leetcode, here's my submission link for leetcode https://leetcode.com/problems/spiral-matrix/submissions/1490667996/ and from where I had learnt this a few months ago https://takeuforward.org/data-structure/spiral-traversal-of-matrix/. Traversing a spiral matrix was a standard question everywhere. Can you please reconsider my skipped solutions.

»
12 months ago, hide # |
 
Vote: I like it +8 Vote: I do not like it

Can anyone direct me to where I am wrong?

This is my submission

»
12 months ago, hide # |
 
Vote: I like it -9 Vote: I do not like it

How can i farm positive contribution?

»
12 months ago, hide # |
 
Vote: I like it +9 Vote: I do not like it

Thank you for Editorial

»
12 months ago, hide # |
 
Vote: I like it +8 Vote: I do not like it

Can anyone tell how to find sum of mex of all subgrids in Div2 C question

»
11 months ago, hide # |
 
Vote: I like it +8 Vote: I do not like it

anyone know how to solve the follow up Bonus question for Mex in the grid question

"We had the idea of dividing this problem into two subtasks, in which the second subtask requires you to count the sum of MEX of all subgrids! "

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

bad editorial for div1c , what is meant by it can be proven ? The editorial is incomplete .

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

for problem Div1C

how to prove its always possible to obtain array B with the maximum matches for each index ?

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

can someone please give an explanation of 2101B — Quartet swapping, of the part of ordering of last three elements.

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

Could you guys tell me what's wrong with my submission for 2101B? https://mirror.codeforces.com/contest/2101/submission/321669351

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

Why C give the incomplete and ambiguous editorial?

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

For the Div2E (23 Kingdom), why does their approach of adding up the number of begins (at or before index $$$i$$$) and the number of ends after $$$i$$$ not overcount the distance?

For example, take: 1 1

Here, the distance should be 2. The number of begins at or before index 1 is 1 and the same for index 2. The number of ends after index 1 is 1, and 0 for index 2. Adding these together gives 3 which is not 1.

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

First, sorry for necroposting.

Here is a different idea for div1D.

For an array to be cute, every element should be part of LIS or LDS, and one should be part for both.

For each index $$$i$$$ we will find longest subarray to the left $$$[L[i], i]$$$ that is good, and longest on the right $$$[i, R[i]]$$$.

To find $$$L[i]$$$, we will run two stacks, one for finding nearest smaller and one for nearest greater. Now

$$$[L[i], i]$$$ is good if sum of number of elements $$$ \gt = L[i]$$$ in both stacks is $$$i-L[i]+1$$$.

We can find $$$R[i]$$$ in the same manner.

To implement, we can bin search on $$$L[i]$$$ and to find element $$$ \gt = L[i]$$$ we can also bin search the stacks.

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

Problem 2101 kingdoms , really skips much desired explanation, in everything it's like it can be proved .

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

Can someone please explain this part in the div2 C solution :

<< Proof: The multiplication of two numbers with the same sum is maximized when they are as close to each other as possible. Therefore, for each subgrid, if we consider the empty rows above and below it and separately consider the columns on its right and left, if one is fixed, the place is optimized to be in the middle. Now, similarly, we can say that it is optimal if the length and height of the subgrid are close to each other as well, as increasing one will cause the other one to decrease. With these two observations combined, a subgrid having the above properties is optimal when it's a nearly square subgrid in the middle of the grid. >>

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

For anyone looking for the problem B code in C++

351530979

#include <bits/stdc++.h>
using namespace std;

int main()
{
    int t;
    cin>>t;
    while(t--)
    {
        int n;
        cin>>n;
        vector<int> a(n);

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

        int first_el = a[0];

        sort(a.begin(), a.end());
        int pos = (n / 2) + 1; // The element at the pos (n / 2) + 1

        if(first_el <= a[pos-1]) cout<<"YES\n";
        else cout<<"NO\n";
    }
    return 0;
}
»
4 months ago, hide # |
Rev. 2  
Vote: I like it -16 Vote: I do not like it

Dear Shoo, eren__, sweetweasel, MikeMirzayanov,

Please pay attention to this.

I am getting the bigger answer than the test case. for n = 3,

Answer given is : 8 4 5 6 0 1 7 2 3 sum = 23

My answer:

0 1 4 2 3 5 7 6 8 sum = 30

Basically the approach I used was to keep numbers as near as possible to 0.

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

can any one tell me why this the first approach of mine of start with right then down then left then up passes but the approach with down left up right fails!! 369534888 369534946