nifeshe's blog

By nifeshe, 24 hours ago, In English
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
  • Vote: I like it
  • +53
  • Vote: I do not like it

»
5 hours ago, # |
  Vote: I like it +17 Vote: I do not like it

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

  • »
    »
    5 hours ago, # ^ |
      Vote: I like it +6 Vote: I do not like it

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

  • »
    »
    4 hours ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    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

  • »
    »
    4 hours ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    did gpt tell u that

  • »
    »
    3 hours ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    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.me/contest/2056/submission/301478898

    • »
      »
      »
      115 minutes ago, # ^ |
        Vote: I like it 0 Vote: I do not like it

      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

      • »
        »
        »
        »
        62 minutes ago, # ^ |
          Vote: I like it 0 Vote: I do not like it

        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;
        
  • »
    »
    2 hours ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

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

    submission

»
5 hours ago, # |
  Vote: I like it +15 Vote: I do not like it

B killed me and C buried me :(

»
5 hours ago, # |
  Vote: I like it +3 Vote: I do not like it

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

»
5 hours ago, # |
Rev. 2   Vote: I like it +14 Vote: I do not like it

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.

  • »
    »
    5 hours ago, # ^ |
      Vote: I like it +8 Vote: I do not like it

    Well for some people (me) B was significantly easier because the solution was pretty motivated while C was just trying random things and seeing what stuck

»
5 hours ago, # |
  Vote: I like it +10 Vote: I do not like it

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.

  • »
    »
    4 hours ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    bro, you are fucking right

  • »
    »
    3 hours ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

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

    • »
      »
      »
      63 minutes ago, # ^ |
        Vote: I like it 0 Vote: I do not like it

      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

      • »
        »
        »
        »
        50 minutes ago, # ^ |
          Vote: I like it 0 Vote: I do not like it

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

        • »
          »
          »
          »
          »
          32 minutes ago, # ^ |
            Vote: I like it 0 Vote: I do not like it

          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

»
5 hours ago, # |
  Vote: I like it +30 Vote: I do not like it

The difficulty distribution of this match is simply a disaster.

»
5 hours ago, # |
Rev. 2   Vote: I like it 0 Vote: I do not like it

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.

»
5 hours ago, # |
  Vote: I like it +1 Vote: I do not like it

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.

  • »
    »
    62 minutes ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    I also got idea of using 3-len palindrome just by reading this testcase

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

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

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

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

»
5 hours ago, # |
  Vote: I like it +9 Vote: I do not like it

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.

  • »
    »
    3 hours ago, # ^ |
      Vote: I like it +4 Vote: I do not like it

    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

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

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

»
5 hours ago, # |
  Vote: I like it +5 Vote: I do not like it

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

  • »
    »
    5 hours ago, # ^ |
      Vote: I like it +13 Vote: I do not like it

    Due to the checker code? If n is larger, it is hard to check whether the output is correct or not. I guess.

»
5 hours ago, # |
Rev. 2   Vote: I like it 0 Vote: I do not like it

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.

  • »
    »
    2 hours ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    yup C is easy but you can easily make wrong guesses hence increasing penalities.

»
5 hours ago, # |
  Vote: I like it +17 Vote: I do not like it

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.

  • »
    »
    4 hours ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

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

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

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

»
4 hours ago, # |
  Vote: I like it +3 Vote: I do not like it

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 .

  • »
    »
    4 hours ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    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.

  • »
    »
    4 hours ago, # ^ |
      Vote: I like it +3 Vote: I do not like it

    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.

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

I have the dumbest solution using BST to solve B
301463165

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

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++;
  }
}
»
4 hours ago, # |
Rev. 3   Vote: I like it +2 Vote: I do not like it

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.

»
4 hours ago, # |
  Vote: I like it +3 Vote: I do not like it

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.

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

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.

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

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.

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

E is solvable with xor hashing as well: 301474893

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

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

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

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

»
4 hours ago, # |
Rev. 2   Vote: I like it 0 Vote: I do not like it

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

Here is my submission.

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

»
4 hours ago, # |
Rev. 2   Vote: I like it 0 Vote: I do not like it

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

  • »
    »
    3 hours ago, # ^ |
      Vote: I like it +1 Vote: I do not like it

    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)

»
3 hours ago, # |
  Vote: I like it 0 Vote: I do not like it

Thanks for highlighting the unusual limits.

»
3 hours ago, # |
  Vote: I like it 0 Vote: I do not like it

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

»
3 hours ago, # |
  Vote: I like it +3 Vote: I do not like it

How to solve the C with Binary Search?

Thanks in advance!

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

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.

  • »
    »
    2 hours ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

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

  • »
    »
    2 hours ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    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.

»
2 hours ago, # |
  Vote: I like it 0 Vote: I do not like it

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

»
2 hours ago, # |
  Vote: I like it 0 Vote: I do not like it

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

»
106 minutes ago, # |
  Vote: I like it 0 Vote: I do not like it

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

»
85 minutes ago, # |
  Vote: I like it 0 Vote: I do not like it

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

»
66 minutes ago, # |
  Vote: I like it 0 Vote: I do not like it

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

»
66 minutes ago, # |
  Vote: I like it +2 Vote: I do not like it

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>

»
24 minutes ago, # |
  Vote: I like it 0 Vote: I do not like it

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