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

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

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
Разбор задач Codeforces Round 1024 (Div. 1)
Разбор задач Codeforces Round 1024 (Div. 2)
  • Проголосовать: нравится
  • +185
  • Проголосовать: не нравится

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

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

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

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.

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

    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.

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

    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

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

      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.

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

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

        "Click to see test details"

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

          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 .... :(

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

            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).

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

              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.

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

                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

»
18 месяцев назад, скрыть # |
← Rev. 3  
Проголосовать: нравится -30 Проголосовать: не нравится

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).

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

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.

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

    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.

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

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).

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

    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.

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

    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..

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

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

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

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

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

      Why can this construction scheme be obtained through several operations?

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

      hope my poor English can make you understand.

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

        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.

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

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.

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

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)

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

    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.

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

    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.

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

      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

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

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

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

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

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

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

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

    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.

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

E with maximum segment tree + binary search 319356051

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

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

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

I quite liked Div. 1 C!

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

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

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

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

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

    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.

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

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

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

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?

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

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

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

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?

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

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;
}
  • »
    »
    18 месяцев назад, скрыть # ^ |
     
    Проголосовать: нравится 0 Проголосовать: не нравится

    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

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

      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.

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

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

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

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

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

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.

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

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.)

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

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

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

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

»
18 месяцев назад, скрыть # |
← Rev. 4  
Проголосовать: нравится +6 Проголосовать: не нравится
$$$\bf{D1C}$$$

could be solve using Binary Search

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

Dear @Shoo @eren__ @sweetweasel @MikeMirzayanov , In the recent codeforces round 1024 Div. 2 contest, i received a mail regarding my C solution https://codeforces.me/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.

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

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)$$$.

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

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

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

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

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

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.

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

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.

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

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")
»
18 месяцев назад, скрыть # |
 
Проголосовать: нравится +8 Проголосовать: не нравится

Can anyone direct me to where I am wrong?

This is my submission

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

Thank you for Editorial

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

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

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

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! "

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

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

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

for problem Div1C

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

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

Why C give the incomplete and ambiguous editorial?

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

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.

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

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.

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

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

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

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;
}
»
11 месяцев назад, скрыть # |
← Rev. 2  
Проголосовать: нравится -16 Проголосовать: не нравится

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.

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

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

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

MEX in the grid:
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! Do you have a solution for this?

Yes, eren__

Let's call smallest rectangle containing numbers $$$0..k$$$ as $$$f_k$$$. Also, $$$f_{kl}$$$ means index (0-based starting from top left) of left edge of $$$f_k$$$ rectangle and so on $$$f_{kr}$$$, $$$f_{kt}$$$ and $$$f_{kb}$$$ for right, top and bottom edge respectively.

Ans = $$$\sum\limits_{k=0}^{n^2-1}(f_{kl}+1)*(n-f_{kr})*(f_{kt}+1)*(n-f_{kb})$$$

Because we can say, if all rectangle containing $$$f_k$$$ contributes +(k+1) to the answer, then all having $$$f_{k+1}$$$ will contribute +1 to answer not +(k+2) because then we double count already added +(k+1). So we use 0 as base case which contributes 0+1=1 to answer and all above 0 contributes +1.

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

Many people would be glad if someone made a better editorial for Div1C without using phrases like "it can be proven" without proof and something like "it's obvious that".