nifeshe's blog

By nifeshe, 4 months ago, In English

Special thanks to reirugan, weirdflexbutok and Intellegent for helping with editorials.

2191A - Array Coloring

Hints
Solution
Code
Rate The Problem!

2191B - MEX Reordering

Hints
Solution
Code
Rate The Problem!

2190A - Sorting Game

Hints
Solution
Code
Rate The Problem!

2190B1 - Sub-RBS (Easy Version)

Hints
Solution
Code
Rate The Problem!

2190B2 - Sub-RBS (Hard Version)

Hints
Solution
Code
Rate The Problem!

2190C - Comparable Permutations

Hints
Solution
Code
Rate The Problem!

2190D - Prufer Vertex

Hints
Solution
Code
Rate The Problem!

2190E - Median Permutation

Special thanks to Sana for showing me how to go from $$$O(n^2)$$$ to $$$O(n)$$$.

Hints
Solution
Code
Rate The Problem!

2190F - Xor Product

Hints
Solution
Code
Rate The Problem!

2190G - Maximize Determinant

Hints
Solution
Code
Rate The Problem!
  • Vote: I like it
  • +150
  • Vote: I do not like it

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

D is easier than C....

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

my implementation is sooo trash i got every observation correct in div2 E :((

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

It seems as if dp[ind][some state] was easier to comprehend than the official soln in D2. Altho looks similar to me.

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

this is how i felt solving c

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

    i felt it was like a prev "c" i had done in some prev contest where parity of blocks matter ... i was stuck in that qn for like the whole contest and didnt realise u could just do it in 1 move ::

    d1 was too easy but i vowed to nevr commit to the sin of greed before consistently solving c

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

    This made my day, I usually always feel this way in Codeforces. Many of the easier problems are closer to riddles, while their implementation is so straightforward it makes me feel dumb after understanding it xd.

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

    I KNOW RIGHT. I thought it was talking about sequences not subsequences, like i was trying all sorts of things and i couldnt do it for like almost the end of the contest and during the end i saw the end of the problem and it mentioned "subsequence" and it clicked and i solved it in like 4 minutes. I had like 5-6 incorrect attempts and a hell of penalties xD

»
4 months ago, hide # |
 
Vote: I like it +48 Vote: I do not like it
solution for D

It's surprisingly nice and doesn't require complex calculations, although precise reasoning can make a problem difficult just as much as calculations.

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

I can say thank you very much for this contest; it was great. But I felt that from A to D, it was easier than a normal Div. 2. Am I wrong?

And I hope I become a specialist in this contest.

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

it seems like i am about 10 points away from CM... :(

i really hope all cheaters will be knocked out

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

why the solution of c in div 2 is not here :(

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

Interactive but not binary search, chat is this legal?

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

Day 1 of trying to find the purpose of the first condition in problem D div2.

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

Div2 B hint 2 and hint 3 are the same

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

358297716

Spoiler

This line in jiangly's Div. 1 C code is incredibly elegant.

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

my first contest..gives me a good idea of how to approach the others in the future

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

I am not to sure about it, but actually I am pretty certain, that for 2190B1 — Sub-RBS (Easy Version) you actually only have to check, whether there exists 2 more opening brackets after the first closing bracket and then return string length minus 2.

Theorem the following are equivalent:

(1) There exists a better subsequence of length l-2

(2) There exists a better subsequence

(3) there are more than 2 opening brackets after the first closing bracket

(1) => (2) is trivial

(2) => (3) Let balance(string) be the balance mentioned in the solution. If there is a better subsequence t, that differs at position i, then b:= balance(t[0:i]) = 2 + balance(s[0:i]) since a ")" was replaced by a "(". To still be regular there must be b closing brackets after i in t, since all these b closing brackets also must have been in s, for s to end with a balance of 0, there must be 2 more opening brackets in s after position i.

(3) => (1) Lets construct a string of len(s)-2 that is a better subsequence than s in the following manner: There are 2 opening brackets after the first closing bracket. let the first of these be at position i. and the second at position j. t = s[0:i-1] + s[i:j] + s[j+1:] so, we only delete the first closing bracket that is directly followed by a opening bracket and the first (or actually any opening bracket) that isnt this first opening bracket at position i. Since the closing bracket was deleted before the opening bracket, the balance remains greater than zero at each position and reaches zero in the end.

Hence (1)=>(2)=>(3)=>(1)

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

    I just submitted the following code and it got accepted:

    n = int(input())
    for _ in range(n):
      l = int(input())
      brackets = input()
    
      i = brackets.index(")")
      if brackets[i+1:].count("(") >1:
        print(l-2)
      else:
        print(-1)
    
    • »
      »
      »
      4 months ago, hide # ^ |
       
      Vote: I like it 0 Vote: I do not like it

      Yes my logic was the same for both parts of Div2 D — the maximum possible len will be n-2 for the subsequence(which can be explicitly constructed in the favourable case like you mentioned). And if the )( condition is not enforced for atleast one index then "(" is basically leading in the string s at all times so you cannot remove any brackets such that "(" comes before ")" in a regular subsequence.

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

    yes this is true, used this for the hard version

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

Good Editorial;) and interesting problem D

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

Bro, i got a n^2 solution of div2 D2. We can say that we will count a subsequence if it has ")((" as subsequence. Now we can use 4 arrays to track states of the above subsequence. We can make each of them as a n length array of pair where first element of pair represents the count of element and the second element represent the sum of length of subsequencestill now. Here we are using the array of length n as it represents the stack value (count of open bracket — count of close bracket) for those subsequences. Now for each element in s, we can try to move to further state by picking that element or we can stay in same state. Answer will be the sum of subsequence on zero stack value in final state. Check my submission for more clarity

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

Problem B2 can also be solved in $$$O(N^2)$$$

Solution

358385878

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

How to think and intute in div 2 C i am not able to think and make any intution

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

    Bro bob can only win if there is no ones before zeros. In that case Alice can't do anything. If there was some ones before zeros then for each 1 from front select a zero from the end. Solve it by two pointers.

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

      Bro can u proof?

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

        yes.

        If there is no ones before zeros, every sequence Alice can choose to have the characters non increasing must have the characters monotone and as such also non decreasing, so the reordering doesnt change the bitstring and Alice cant make a valid move.

        In every other case alice can finish the game in one move, by picking the indices of the first k 1s and the last k zeros, such that k is the biggest k such that the kth-first 1 (index i) is still before the kth-last zero (index j). this results in the first k 1s to be replaced by 0s and the last k 0s to be replaced by 1. As such, there is only 0s before index i and only 1s after index j. Inbetween index i and j, there can be no 1 left of any 0 (or otherwise k isnt maximal), so the whole string is now sorted.

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

In d1A/d2C, I misunderstood the "subsequence" conveyed by the question as a "substring", which led to a very poor performance during the competition :(, However, I'm very curious about how to solve this problem if it can only be reversed in the form of a substring

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

why div 2 C is look like this

i am not sure ,is the statement was unclear or i haven't solved enough problem to understand this statement??!!....

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

~~~~~ D2 was literally state finding problem of dynamic programming.In D1,We are all about to find that it is possible to get n-2 iff for a close bracket there is 2 opening bracket after that! In D2,we have to find the sum of all answers of regular subsequece of the give irregular sequence.So what things we need must? 1. Current position 2.current sum which can give info iff the subsequence is regular 3.size of the subsequence 4.number of opening bracket following the closing bracket.we will store only at most 2 5.boolian for a close bracket Move:We will go knapsack for every position either taking it or not taking it.sum will be increased or decreased following the bracket type. If we get a close bracket,and previously not taken a close,we will take it and mark the boolian true...else normal move.Then if the bracket is open, We will check if we got a close in previous through boolian!If it is then we will increase the counter of opening bracket else else do normal move....and every time do a not taking move....at last return max(0,n-2) iff boolean is true and counter>=2. [submission:358392558]

~~~~~

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

Can anyone explain the solution to Sub-RBS(Easy)?

Moreover, why will counting the max no of "()" pairs not be correct? If maxPairs > first occurence of ')' in s, then shouldn't the ans be 2 * maxPairs?

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

    Correct me if I misunderstood, but it is an RBS. For every '$$$($$$', there is a corresponding '$$$)$$$', so the number of non-overlapping "$$$()$$$" pairs will always be $$$\frac{n}{2}$$$. Instead, we want to delete two values (which must be alike to keep RBS nature). We can do this by deleting the earliest occurrence of '$$$)$$$' and then deleting a later occurrence of '$$$($$$' as long as there is another '$$$($$$' later in the string. This works because we will still have an equal number of open and closed brackets, and it will not cause the first or last bracket to be facing the wrong way. Ensuring another '$$$($$$' bracket exists is beneficial to ensure that the sequence fits the constraints. This is because all brackets after the first deleted will be shifted over to the left, so it will be guaranteed that some '$$$($$$' bracket will be shifted into the location that was previously occupied by a '$$$)$$$' bracket.

    Hope this helps! (Slightly simplified approach than editorial for D1, but this is much simpler and keeps the same idea. It is described in the beginning of the editorial for D2.)

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

Div2 C cooked me so badly in this contest, so much so that even when I started to think about solution for D I was unable to focus completely, any suggestions on how should I deal with such scenarios

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

    Try to Clear the Mind by zoning out for a while or walk for like 5 minutes,Then come back .You can refocus and you may solve both C and D :)

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

Apparently, just checking if the no. of prefix ('s is < (n/2-1), then ans is n-2, else 0 just works in D1

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

As constraints are very low we can use the black box technique for the problem A.

code:---

358406798

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

Good contest!

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

Simple solution for div 2 , d1 d2

d1 :- after first occurence of ')' there should be 2 occurence of '(' , if there then answer is n-2 else answer is -1

d2 :- use dp to count the number of regular sequence in which there are 2 '(' after the fisrt occurence of ')' , you can see my code

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

My solution to problem B is to count the frequency of each number's occurrence. Find the global MEX value m. Check every number between 0 and m-1: if any number only appears once, output YES. If all numbers occur>=2 times: if m<=1->output NO. Otherwise ->output YES.

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

In problem C, I think this line:

A move is valid if and only if it strictly modifies the string s,

should have been added to the previous paragraph, too. The paragraph starting with "Formally" must have the sole purpose of explaining what was said previously in a formal way. In this case, it added more information.

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

It seems like checking if position of first closing bracket is less or equal to $$$n / 2 - 1$$$ is sufficient to determine whether RBS has better subsequence of length $$$n - 2$$$ in B1. See submission: 358423397. Using this fact, one can come up with $$$dp[i][l][b][p]$$$ for B2 – number of bracket subsequences on prefix $$$i$$$ with length $$$l$$$, balance $$$b$$$ and position of first closing bracket $$$p$$$ (if there is no closing bracket, $$$p = 0$$$). Transitions are fairly easy to derive and to calculate final answer just iterate through RBS length $$$l$$$ and first closing bracket position $$$p$$$, if $$$p \lt = l / 2 - 1$$$ add $$$(l - 2) * dp[n][l][0][p]$$$ to the answer. Submission: 358443056.

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

B1 -> B2 boring

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

can anybody explain why this extremely simple code gets accepted on 2190B1 — Sub-RBS (Easy Version)?

Somehow if the there is a ')' in the first half -1 (so n/2-1) of characters, the answer is n-2, else it is -1.

Is it just luck, or is there a reason behind it?

#include <bits/stdc++.h>
using namespace std;
 
int t,i,y,m,n,cnt,rsp,pr;
char v[200005];
 
int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
 
    
    cin>>t;
 
    while(t--){
        cin>>n;
        cin>>v;
        int k=0;
        cnt=rsp=pr=0;
        
        for(i=0;i<n;i++)
            if(v[i]==')' && i+1<n/2){
                rsp=n-2;
                break;
            }
        
        if(rsp==0)cout<<-1;
        
        else cout<<rsp;
        
        cout<<'\n';
    }
    
}
  • »
    »
    4 months ago, hide # ^ |
     
    Vote: I like it +3 Vote: I do not like it

    as prooven and not yet disprooven in my comment you gotta return l-2 if and only if after the first closing bracket there are gonna be 2 more opening brackets in the string else return -1

    this is equivalent to:

    (4) there is a ")" in the first n/2-1 characters

    argumentation: there are exactly n/2 opening brackets in each regular bracket sequence.

    (4) There is a ")" in the first n/2-1 characters <=> The first ")" is in the first n/2-1 characters <=> there are only n/2-2 "(" before the first ")" <=> there are atleast 2 "(" after the first ")" (1)

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

This time i felt c easier but can't talk much about D as i haven't much readed and tried to attempt it but c felt easier like 1200+ rated

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

Nice Problems :)

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

In Problem B sort the array and just check the mex at every index before it and after it. If they are different for all index it is possible.

Solution Link — https://mirror.codeforces.com/contest/2191/submission/360196480

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

Hello everyone, I would greatly appreciate someone telling me what am I doing wrong in the easy version of problem B. My idea is the following:

Let's say we have regular string s of brackets. Since s is regular we have the following sequence: $$$a_1, b_1, a_2, b_2, \ldots, a_n, b_n$$$. $$$a_i$$$ represents number of consecutive '(' and $$$b_i$$$ of consecutive ')'. If $$$\sum_{i = 2}^{n}a_i \gt 1$$$, then we can construct regular subsequence of $$$s$$$ by removing the last ')' in $$$b_1$$$, and removing any '(', that is not the first '(' in $$$a_2$$$. If the sum condition does not hold we have two possible situations:

1 — $$$a_1, b_1, 1, b_2$$$

2 — $$$a_1, b_1$$$, where $$$a_1 = b_1$$$. Here I also highlight that the condition on prefixes holds!

The second case trivially does not have greater subsequence! Now for the first case we see that we have to keep the first $$$a_1$$$ '(' since those exist in original string $$$s$$$, but then we have to remove the last '(', and subsequently another ')' from either $$$b_1$$$ or $$$b_2$$$. But then we will share the prefix $$$a_1$$$ followed by $$$a_1$$$ ')', obviously this string would be either a prefix of $$$s$$$ or would be smaller as we would have ')' in subsequence where '(' would be in original sequence.

Now my question is, is there anything that I am not seeing correctly here? As my solution would yield $$$n-2$$$ solution in case the sum condition holds, which would maximize the length of subsequence, and I think I have proven that this condition is both sufficient and necessary. Also I am linking my implementation for the possibility of implementation being incorrect.

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

void solve()
{
    int n;
    string s;
    cin >> n >> s;

    int l = 0;
    int o = 1;

    for (int i = 1; i < n; i++)
    {
        if (s[i] == ')' && o == 1)
            o = 0;
        if (s[i] == '(' && o == 0)
            ++l;
    }

    if (l > 1)
        cout << n - 2 << endl;
    else
        cout << -1 << endl;
}

int main()
{
    int t;
    cin >> t;
    while (t--)
        solve();
}

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

weirdflexbutok && nifeshe

There is a flaw in the editorial of yours for question B1(RBS — Easy Version) Please change it.

In here at the end it should be — sufficient '(' in the statement

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

i dont get it the A problem statement we have to chose a charecter in decreasing order but 4 1010 in this test case alice choose 1 2 3 4 and won like how ?

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

why my solution is wrong for problem A

void solve()
{
    int n;
    cin>>n;
    vector<int> a(200);

    int color = 1;
    for(int i = 0; i < n; i++){
        int elem;
        cin>>elem;
        a[elem] = color;
        if(elem > 1 && a[elem] == a[elem-1]) {
            cout<<"NO"<<endl;
            return;
        }
        if( elem < n && a[elem] == a[elem+1]) {
            cout<<"NO"<<endl;
            return;
        }
        //parity
        if(color == 1) color++;
        else color--;
    }
    cout<<"YES"<<endl;
}

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

problem A is missing stating the fact that the array is a permutation in the problem statement. Am i missing something?

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

too hard