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

Автор nifeshe, 27 часов назад, По-английски
Rating predictions

2056A - Shape Perimeter

Hints
Solution
Code

2056B - Find the Permutation

Hints
Solution
Code

2056C - Palindromic Subsequences

Hints
Solution
Code

2056D - Unique Median

Hints
Solution
Code

2056E - Nested Segments

Hints
Solution
Code

2056F1 - Xor of Median (Easy Version), 2056F2 - Xor of Median (Hard Version)

Hints
Solution
F1 code
F2 code
Разбор задач Codeforces Round 997 (Div. 2)
  • Проголосовать: нравится
  • +58
  • Проголосовать: не нравится

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

i feel so dumb after seeing B's solution because I used topological sort and then sorted all the elements with equal entry times in descending order

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

    same mistake tho. In the hindsight B is actually a great problem with multiple solutions.

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

    Here is another way to solve B -> We will construct the array by traverse each string and with each traversal we will get value at one index. permutation numbers : i = 1, 2, 3, ... Lets take i = 3. Now to find where we will place 3 in our array we simply traverse the third string and count the number of '1's before 3rd index in that string and number of '0's after third index. That gives us the index at which 3 will come. Why this works? Think Yourself gg:)

    Code

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

    did gpt tell u that

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

    an easy solution that i came up with is start placing elements in increasing order. For each element $$$i$$$, count the number of connections $$$j$$$ such that $$$j > i$$$. for example $$$i=1$$$ , and it's connected to $$$j=3$$$ and $$$j=5$$$, so $$$count=2$$$ start from the back of the array to find the second empty position and place $$$1$$$ there. here is my submission https://codeforces.net/contest/2056/submission/301478898

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

      Hey, i thought of an almost same solution but there is something that i am getting wrong. I implemented it by checking for each index in increasing order, the place from the back of array where it should be placed based on the count, say j. But if index j has some value already placed there, keep checking if any index k, (k=j-1;k>=0;k--) is empty and place the number there. Basically, How to process when the count that we calculate is same for 2 different numbers??

      here is my submission,301471228

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

        What ur doing there is partially correct. Here is an example where it will fail

        3
        000
        001
        010
        

        this should output 2,3,1 but your code would yiled 3,2,1

        the problem is that, for example count(2) = 1, in my code it means "I need at least 1 zero after me" , but your code says "go to second position from the back and find me the first empty slot" replace your loop with this and i think it should work

                // get next available slot by having at least 'c' zeros
                ll j = n - 1;
                for(j=n-1;j>=0 && c;j--)
                    if(ans[j]==0) c--;
                // if slot is full, skip it
                while(j>=0 && ans[j]!=0)--j;
                ans[j] = i+1;
        
  • »
    »
    5 часов назад, # ^ |
      Проголосовать: нравится 0 Проголосовать: не нравится

    Exact same mistake, but I rewrote a recursive solution after the contest ended (something similar to Quick Sort I'd say).

    submission

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

B killed me and C buried me :(

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

Why the B looks easy here:(.I've done converting the undirected graph in directed based on pi<pj and then i had applied topological sort .Feelin dumb man

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

How did every single person predict that B was lower rated than C? C was much easier IMO. I think we need more lower rated testers.

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

C's solution can be put fairly simple:

Answer will be of the pattern:

[1, 1, 2, 3, 4, ..... ,n-2, 1]

then the max length f(a) is 3

and the count g(a) is: (n-2) + (n-3) = 2*n-5, which is greater than n for n >= 6.

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

    bro, you are fucking right

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

    Yes, it was somewhat obvious like this, but why the problem C is made like this...i thought we have to maximize this f(a). And that seems to be a problem...and idk.. plz share if anyone have idea how it gonna work in that case...

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

      if you maximize f(a) it will be equal to n like 1 1 1 1 1 1 1 1 1 you can insert 2 in between n is odd

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

        But maximizing f(a) like this doesn't guarantee to make g(a)>n and g(a) should always get 1 like this right? I meant maximizing f(a) while meeting the same conditions, like in sample 2 f(a) could be made 5 but as of now we solved it by keeping it as 3 only..!

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

          g(a) is no of palindromes with length==f(a) if you take f(a)=n then how will u find other palindrome of length n, g(a) will be always one and then for n>1 answer will be wrong as g(a) has to greater than n

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

The difficulty distribution of this match is simply a disaster.

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

Another solution for $$$C$$$ is for $$$n > 6$$$ construct the array $$$1,2,$$$ $$$[3...n - 2],$$$ $$$1,2$$$. Most elements will be part of two palindromes of length $$$3:$$$ $$$1,x,1$$$ and $$$2,x,2$$$. For $$$n = 6$$$ use the one in the example.

Edit: Nevermind I just realized this was mentioned in the editorial.

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

In C, If you realise that g(a) is 3n-11 for 6, and notice that 11 is a constant, and for each additional n, it will increase by 3, you notice that using the first construction directly solves the problem.

1,1,2...1,2 works because it follows 3n-11.

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

Any idea which test case is failing for my submission: 301434979 for Problem B. I used a bubble-sort-like approach.

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

It seems like i completely overcomplicated D, because i solved it in $$$O(nA^2 + A^4)$$$.

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

For C, I just took sample solution with 15, it already gives 190 and just pushed numbers in the end like 16 17 18 ... n.

The same way, for 15 > n >= 9 I took sample solution for 9, as it gives 24.

For 6 they already gave answer.

So I only needed to solve by hand 7, 8, which I did on paper.

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

    for 7, 8, you can also just assume adding 4 in the middle of the array for 6 will give at least 1 new palindrome, and adding a 5 after that will give at least 1 more palindrome, and you're guaranteed to not create a longer one

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

why lasts codeforces rounds are random ? rarely to find topics .. number Theory or graphs in first 3 problems ?!! can any one explain

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

What's with the constraint of n in C? Why is it so small?

Also you can just put random distinct numbers at the end of the 3rd sample answer, as g(a)=190 which is >=100

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

Tried sort in B and failed, using another 30 min to ponder another graph solution (almost like dijkstra)... absolutely overkill for B.

Learned, though C is really too easy for some reason.

Still I conclude C << B.

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

A different way to approach D:(Probably easier?)

Solution :

Solution

My submission

Hope you understand the solution, if you have any queries feel free to ask, and I'll try to help if possible.

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

    I did this approach but for "good" subarrays and ended up with the subproblem:

    Given two arrays, Count all pairs i<j such that a[i] < a[j] and b[i]<b[j].

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

Very nice problem F!(however probably too hard for div2)

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

    I think someone new to CP with strong math olympiad background can reasonably solve F1. And yeah, F2 is there to entertain out of contest reds even if it's not too much harder than F1 given enough CP experience

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

I don’t understand the editorial for D. For even length subarrays, when we are testing for x as the median, the bad scenarios seems a lot more than what the editorial depicts.

For instance, if we are testing for 9, the array 1 1 1 9 is certainly bad, but the number of numbers less than or equal to 9 is 4, not 2 as in the editorial .

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

    However, the sum of the array [1,1,1,9] is not 0, it is -4. You only count arrays where sum is 0 and the number appears at least once.

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

    Let's name the [(r-l+1)/2]-th element X and [(r-l+1)/2 + 1]-th element Y.

    A bad array has X!=Y.

    The editorial enumerate the X from 1 to 10, and count the above bad array when X is enumerated as the [(r-l+1)/2]-th element.

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

I have the dumbest solution using BST to solve B
301463165

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

Why we only update the cnt array when a[i] equal to chosen digit? I may think that when the conditieon is enough when we firstly seen the digit x? Like: n = 6, a = {1, 4, 4, 3, 1, 4} with chosen digit = 3. It only update when we at index 4 (a[4] = 3), so that we have {1, 3, 4, 4}. But there are also in the index 6, we can have an bad array {1, 1, 3, 4, 4, 4}, but in this case the author not update the cnt array.

Code:

if(a[i] == x) {
  while(j <= i) {
    cnt[pref[j]]++;
    j++;
  }
}
»
7 часов назад, # |
Rev. 3   Проголосовать: нравится +3 Проголосовать: не нравится

The constraints in the question C were kind of redundant because, I solved directly from visible cases. The answer for n=15 the third case they have given is 198, so I directly gave the same array as output for n>=15 followed by 16, 17, 18... until the required length is achieved. And for n=9 => the answer was 24, so 9-15 range is also sorted. So, only ones remaining are 7,8, we can manually generate that by appending some numbers after the given array for n=6.

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

In the solution of D, it's a little confused by using the term "median". It took me some time to realize "median" means the (r-l+1)/2-th smallest number here.

Also, this intended solution is interesting since the observation is apparent but useful, and it seems that only few contestants came up with it.

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

i didnt realize that the solution for b was that simple. i have just started thats why ig. i did something like trying to find a correct candidate from indexes 1 to n in p. if the number of pairs that can be formed from this index to the last is > than the number of edges our candidate should be connected to and also the number of elements that are larger than our candidate that have not been used yet are also greater than the number of edges our candidate should be connected to then skip this candidate and move on to the next one . since that would mean we would definitely connect an edge that is not supposed to be connected. i converted the undirected to directed first for that and then did it. well the contest time was over by the time i did that lol. but it did work when i submitted it later.

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

I did very dumb solution for C.

For n = [6...11] find answer by hand.

For example, I realized that the prefix of "AABCABCDCDD" where different characters stands for different number gives correct answer, and I used it. As you see, we just need 4 different numbers.

Now, divide n to small parts: many 6 and one x from range [6...11]. Solve for small parts separately, with different set of numbers. Then concatenate them to one big array. It's not hard to see it will give you correct answer.

My submission.

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

E is solvable with xor hashing as well: 301474893

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

in problem C 1 1 2 3 4 5 ... 1 the maximum f(a) is 3 the first and the last one will add n-2 and the second and last one will add n-3 so 2*n -5 it worked for n>=6

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

i did something very different for C and it took me an embarrasing amount of time, first ans = [1, 2, 3, ..., n].Then ans[n-1] = 1 and ans[(n-1)//2] = 1, essentially ans = [1, 2, 3, ... (n-1)//2 , 1, (n-1)//2 + 1 ,..., n-2, n-1, 1] and it got accepted as those 1 essentially made f(a) = 3 and g(a) = 2*(n-2).

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

We can even solve D by counting the good subarrays for every fixed median and sum them up.

Here is my submission.

https://codeforces.net/contest/2056/submission/301477712

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

5 00000 00010 00000 01000 00000 for this input jiangly's solution gives 5 3 4 2 1 while solution in editorial gives 5 4 3 2 1 and both solutions get accepted how is 'p' unique then for this case

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

    This is an invalid input as the question mentions that there exists a valid permutation for each test case. No permutation exists for your set of input, as there is an edge between 2 and 4 but no edge between 2 and 3 or 3 and 4. (no place can be determined for 3 here, as if 3 is after 2 then there should be an edge between 2 and 3 or if 3 is before 4 then there should be an edge between 3 and 4)

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

Thanks for highlighting the unusual limits.

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

I don't seem to have received any rating yet — was this contest unrated?

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

How to solve the C with Binary Search?

Thanks in advance!

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

From F:

For a fixed $$$\text{cnt}$$$, the number of ways to order a is $$$\binom{n}{\text{cnt}_0, \text{cnt}_1, \ldots, \text{cnt}_{m - 1}}$$$. By Lucas's theorem the contribution is odd iff for each set bit in n there is exactly one i such that $$$\text{cnt}_i$$$ has this bit set, and $$$\sum\limits_{i = 0}^{m - 1} \text{cnt}_i = n$$$. In other words, $$$\text{cnt}$$$ has to partition all set bits in $$$n$$$.

Can someone explain this more in detail? I can't connect the dots between Lucas's theorem and this formula.

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

    $$$\binom{n}{\text{cnt}_0, \ldots, \text{cnt}_{m - 1}} = \binom{n}{\text{cnt}_0} \cdot \binom{n - \text{cnt}_0}{\text{cnt}_1} \cdot \binom{n - \text{cnt}_0 - \text{cnt}_1}{\text{cnt}_2} \cdot \ldots$$$

    So $$$\text{cnt}_0$$$ has to be a submask of $$$n$$$, $$$\text{cnt}_1$$$ has to be a submask of $$$n - \text{cnt}_0$$$, etc.

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

    By Luca's theorem we know $$$\binom{n}{k} \equiv 1 \pmod{2} \Leftrightarrow k$$$ contains subset of bits of $$$n$$$ in binary notation.

    Also we have $$$\binom{n}{cnt_0, cnt_1, \ldots, cnt_{m-1}} = \binom{cnt_0}{cnt_0} \cdot \binom{cnt_0 + cnt_1}{cnt_1} \cdot \binom{cnt_0 + cnt_1 + cnt_2}{cnt_2} \cdot \binom{cnt_0 + cnt_1 + cnt_2 + cnt_3}{cnt_3}\dots \cdot \binom{n}{cnt_{m - 1}}$$$, to make lhs an odd number, terms on rhs should all be odd numbers. Consider the last term $$$\binom{n}{cnt_{m-1}}$$$, $$$cnt_{m-1}$$$ should contains subset of bits of $$$n$$$, then consider the previous term before it $$$\binom{n-cnt_{m-1}}{cnt_{m-2}}$$$, $$$cnt_{m-2}$$$ should contains subset of bits of $$$n-cnt_{m-1}$$$ which is exactly bits in $$$n$$$ but not in $$$cnt_{m-1}$$$, then consider previous terms and so on.

    This process is essentially deleting bits from $$$n$$$ and distribute them to $$$cnt$$$, then it become quite intuitive that the above claim is true.

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

you don't have to consider n = 6 / 9 /15 separately.

1 ,1 ,3 ,4 ,5 ,... ,n-1 ,1

has f(a) = 3

and 2*(n-3) + 1 = 2n-5 palindromes! so g(a) > n for n >= 6 which fits the constraints

301414951

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

Solved C like nothing, bricked at B. Nice Problemset. Here's my weird solution to C. Solution

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

For D, I was like with a median of x: |the number of elements larger than x — the number of elements smaller x|<|the number of element of x in the sequence|. And I cannot find a good algorithm. It was too counterintutive for me to count "bad subarrays".

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

i think i have a better solution for c, i hope you'll like it :D it only require hardcoding for n=6 i just messed it up during the contest in second for loop where i started it from n/2 and not n/2+1

anyways good contest ~~~~~~~~~~~~ //this is code

include <bits/stdc++.h>

using namespace std;

define fio ios_base::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL)

define ll long long

void solve() { int n; cin>>n; vectortemp={1 ,1 ,2 ,3 ,1 ,2}; if(n==6){ for(auto it:temp){ cout<<it<<" "; } cout<<endl; return; } vectorarr(n); int j=1; for(int i=0;i<=n/2;i++){ arr[i]=j; j++; } j=1; for(int i=n/2+1;i<n;i++){ arr[i]=j; j++; } for(auto it:arr){ cout<<it<<" "; } cout<<endl;

}

int main() { fio; ll t; cin >> t; while (t--) { solve(); } return 0; } //this is code ~~~~~~~~~~

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

I solved C differently. In sequence [1,2,3,…,n] I changed a[⌊(n+1)/2⌋]=1 and a[n]=1, so in this new sequence f(a)=3 and g(a)=n-3+n-2=2n-5 which is enough for n≥6. Here's my solution 301421526

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

for c just create sequence such that length of longest palindrome is 3 , putting 1 ....1 1 here all elements in between create 3 length palindrome now we have (n-2) palindromes ready now do it with 2 it will always exceed n>

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

~~~~~~~~ t = int(input()) for q in range(t): n=int(input()) buffer=[0]*(n+1)

adj=[buffer]
for i in range(n):
    adj.append(list('0'+ input()))
for i in range(len(adj)):
    for j in range(len(adj[0])):
        adj[i][j]=int(adj[i][j])

#print(adj)
arr=[]
for i in range(n,0,-1):
    arr.append(i)
#print(arr)
i=1
val=None
while(i<n):
    j=i-1
    #print(i)
    val=arr[i]

    while j>-1:
        if adj[val][arr[j]]==1:

            j-=1
            if j==-1 or (j>=0 and adj[arr[i]][arr[j]]==0):
                #print(j+1,val)
                arr.insert(j+1,val)
                #print(arr)
                arr.pop(i+1)
                i+=1
                break
        else:
            i+=1
            break
print(*arr)

~~~~~~~~~~~~~~~~

I did B this way: first arrange in descending order 5 4 3 2 1 now 4-->5 so 4 5 3 2 1 now 3 is connected to 5 but not 4 so: 4 3 5 2 1 now 2 is to 3 and 5 4 2 3 5 1 now 1 is to 3 5 4 2 1 3 5.

managing the loop parameters was a bit confusing but it is easy to get and works all good.301490247

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

geeked on b

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

I am too dumb for Smart contests.

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

I did so dumb for C.I never think that f(a) can be 3 for any input,I can't solve constructive problems:(