Serval's blog

By Serval, 13 months ago, In English
Fun Fact 1
Fun Fact 2
Fun Fact 3
Fun Fact 4
Fun Fact suggested by testers

2085A - Serval and String Theory

Hint 1
Hint 2
Solution
Code (Python)

2085B - Serval and Final MEX

Hint 1
Hint 2
Solution
Another Hint
Another Solution
Code (Python)

2085C - Serval and The Formula

Hint 1
Hint 2
Solution
Code (Python)

2085D - Serval and Kaitenzushi Buffet

Hint 1
Hint 2
Hint 3
Solution
Code (C++)

2085E - Serval and Modulo

Hint 1
Hint 2
Hint 3
Hint 4
Solution
Code (C++)

2085F1 - Serval and Colorful Array (Easy Version)

Hint 1
Hint 2
Hint 3
Solution
Code (C++)

2085F2 - Serval and Colorful Array (Hard Version)

Hint 1
Hint 2
Solution
Code (C++)
  • Vote: I like it
  • +156
  • Vote: I do not like it

| Write comment?
»
13 months ago, hide # |
 
Vote: I like it +59 Vote: I do not like it

Nice contest!

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

If only Hint 1 was available for D during the contest , good problems overall.

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

First time ever I do have a feel I can solve problems in Div2D and Div2F1.

Great problem setting, thanks for the contest.

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

very weird that last div2 contest my rank was 419 and delta was 26... now my rank is lower 454 but delta is 33 ... I was expecting it to be 1 digit or maybe negative.

but I am ok with this... ha ha

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

hint 1 for problem D feels like a personal attack, I spent quite some time thinking for DP solution lol.

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

Nice contest, Even more nice editorial. Thanks to the everyone involved.

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

    yes, this editorial is very well presented, with hints given, which can help someone who wants to upsolve. kudos

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

You can easily solve D by going backwards in time and doing a regret greedy

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

    Wow, regret greedy? Do you have a minute to explain what that is/provide an associated blog post that explains it? (after a quick search, I did not find any material on the matter)

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

      A regret greedy is a greedy where you can exchange decisions from the past with more optimal decisions from the present

      In D, you can swap a plate you took earlier for a more delicious plate now

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

      I think the idea of regret greedy is that you don't have to take the optimal decision just yet, but later when it is not longer possible to postpone you will have all of the information to take a more optimal decision about the past/present. I don't know if that's what he meant but I also thought of this problem in a similar way. IMO you could also say that this problem resembles a job scheduling problem.

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

    wow, I learn a new term today called "regret greedy"

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

    I have created a blog and practice contest on Regret Greedy

    https://mirror.codeforces.com/blog/entry/127492

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

Serval please fix problem C's first hint !

thanks in advance!

UPD: Fixed!

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

Finally hit CM! Very enjoyable round imo (definitely not biased), peak problems.

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

Good contest with fast Editorial. The problems are interesting will try them all.

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

Solution to the C is out of the box, wow. i was able to get upto (x + K ) and (y + k) = 0. I think hard after this. but that was the wrong direction.

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

    Yeah, same here! At first, I was trying to guess each bit of $$$k$$$ which just made the solution way more complicated than it needed to be. But after refining it, it finally got accepted! 311918873

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

Can someone explain D to me? that % one is not very intuitive

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

    Let i = n-j(k+1)+1. Then we have n-i+1 = j(k+1), which is divided by k+1. i here is the position where we should determine which plate to take.

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

One of the best contests in a while , not many prerequisites and just logical stuff . And also the solutions are surprisingly short :pray:

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

serval my glorious king im honored to give a contest written by you

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

in problem A,can't i just swap s[0] with something smaller than s[0] from the string,or swap s[n-1] with something greater than s[n-1] when s[0]==s[n-1] and if k>0? like abba and k=1 so abab and k=0 thus abab is a universal string. can anyone point out the mistakes in my logic? and if s[0]>s[n-1] we will just swap s[0] with s[n-1] and need 1 operation and no operations are needed if s[0]<s[n-1]

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

    The logic is correct; I don't see the issue.

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

      but it gives wrong answer on testcase 2 ;(

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

        If k>0 and the string does not consist of the same character, then the answer is always YES.

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

          yes, also if the string is abcd and k=0 then the answer will also be YES right?because it is already universal. and if its dbca and k=1 i will just simply interchange s[0] with s[n-1] -abcd,so i dont exactly find any mistakes in my code.

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

            Answer is YES unless: k=0 and r is not universal, or k>0 and r has only one distinct character

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

            thats how I coded my solution look at my sol

             int n, k;
                cin >> n >> k;
                string s;
                cin >> s;
                string t = s;
                reverse(all(t));
                map<char,int> mp;
                rep(i,0,n) mp[s[i]]++;
                if(mp.size()>1 && k){
                    yes();
                    return;
                }
                if (s < t) {
                    yes();
                    return;
                }
                else if(k==0){
                    no();
                    return;
                }
                else{
                    no();
                    return;
                }
            
»
13 months ago, hide # |
 
Vote: I like it 0 Vote: I do not like it

Solution for C says "sufficient large" instead of "sufficiently large"

»
13 months ago, hide # |
Rev. 2  
Vote: I like it -15 Vote: I do not like it

I absolutely hate these kinds of editorials, where the solution to a problem is written in 4 lines like — oh we can do this for the solution, then do that, then do this greedy approach which we won't explain how it is correct, and then finally find answer with some random data structure which we won't bother to implement. You might as well don't write the editorial please, it will make no difference.

You guys need to understand that a person who comes to the editorial, is expecting a detailed solution with good explanation, so that he/she can learn the idea behind it and use in future problems, and not a 4 line paragraph where half of the things are just written without any proofs or anything. Random people posting explanation in discussion thread are far better than these stupid editorials.

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

    I feel you and I will suggest doing some competitions on codechef, they have really detailed editorials

    also try to follow some YouTubers who analyze questions after the contest as they cover lower rated problems quite well. and then you can try to read editorials

    That is what I do when I am not able to make sense of editorial, or I comment on the editorial post asking people for help and questions which are solved by large number of people generally get some explanation. so we can help each other and write different explanations in the comments

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

Can someone explain C again pls ?

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

    $$$a$$$ ^ $$$b$$$ = $$$a$$$ + $$$b$$$ - $$$2$$$ * ($$$a$$$ & $$$b$$$) so the equation is $$$a$$$ + $$$b$$$ = $$$a$$$ + $$$b$$$ - $$$2$$$ * ($$$a$$$ & $$$b$$$) That means that ($$$a$$$ & $$$b$$$) = $$$0$$$ — ($$$x$$$ + $$$k$$$) & ($$$y$$$ + $$$k$$$) = $$$0$$$

    We observe that a power of two is represented like that $$$10000...$$$ and any number before it doesn't have the most significant bit on so their ANDing will always be $$$0$$$. We want to add $$$k$$$ to both numbers such that the greater of them becomes a power of $$$2$$$ and the other one becomes less than it.

    If both are equal then their ANDing after addition will never be 0. Otherwise, we can find the first $$$n$$$ such that $$$2^n$$$ >= $$$max(x, y)$$$ and make $$$k$$$ equal to $$$2^n$$$ - $$$max(x, y)$$$

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

      Great explanation, thanks.

      Repeating his explanation, we want the two final numbers to have bitwise AND AS 0. So, one way is, make one number as a power of 2, and the other number somewhat less than the first. Thus, they will have no set bit common.

      Method: Add just enough to the greater of the two (max(x, y)), to make it a power of two. Since the two numebrs are different, the smaller number will also be smaller after adding k.

      Code: 312044416

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

E was too much beautiful for proving me so dumb :(

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

Funnily enough I solved F1 without realizing you need $$$k/2$$$... and reading that hint was enough to solve F2. The data structure was not that heavy, you need a multiset for maintaining the smallest $$$k/2$$$ elements and another one for maintaining the remaining $$$k-k/2$$$.

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

check(0xc0ffee) Lol nice way to check for a random large mod in Solution for E

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

Problem A with unclear meaning.

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

surprising problem C solution, i was not expecting O(t).

I was trying to do bitmasks but failed

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

Down vote me pls

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

2085D - Serval and Kaitenzushi Buffet Could someone please help me understanding what is wrong with my approach or code.

Approach:

While processing the time(0-index based) from n — 2(second last) to 0, I am trying to find out if I take the plate at time = i, then what is maximum sum of deliciousness of plates I can take at time > i, such that taking and consuming those plates (k + 1 for each plate) takes time <= n — 1 — (i + k), as this is the time left when I take plate i -> i + k time consumed. For that i am using a segment tree, in which I am storing for time = 0 to n, the (maximum sum of deliciousness, time) for each range processed by far.

Code:

#include <iostream>
#include <vector>

using namespace std;

struct Node {
    long long val;
    int len;
    Node () : val(0), len(0) {};
    Node (const long long& val, const int& len) : val(val), len(len) {};
    void output () {
        cout << val << ' ' << len << ' ';
    }
    const bool operator< (const Node& oth) const {
        return val == oth.val ? len > oth.len : val < oth.val;
    }
};

template <typename T>
struct SegTree {
    int n;
    vector<T> segTree;

    SegTree (const int& n) : n(n), segTree(n) {}

    T comp (const T& a, const T& b) {
        return max(a, b);
    }

    void update (int node, int l1, int r1, T& val, int tl, int tr) {
        if (l1 > r1) return;
        if (l1 == tl && r1 == tr) {
            segTree[node] = max(segTree[node], val);
            return;
        }
        int m = tl + (tr - tl) / 2, n1 = node + 1, n2 = node + 2 * (m + 1 - tl);
        update(n1, l1, min(r1, m), val, tl, m);
        update(n2, max(l1, m + 1), r1, val, m + 1, tr);
        segTree[node] = comp(segTree[n1], segTree[n2]);
    }

    T query (int node, int l1, int r1, int tl, int tr) {
        if (l1 > r1) return Node();
        if (l1 == tl && r1 == tr) return segTree[node];
        int m = tl + (tr - tl) / 2, n1 = node + 1, n2 = node + 2 * (m + 1 - tl);
        return comp(query(n1, l1, min(r1, m), tl, m), query(n2, max(l1, m + 1), r1, m + 1, tr));
    }

    void output () {
        for (int i = 0; i < n; i++) cout << segTree[i].val << ' '; // cout << segTree[i].output() << ' ';
        cout << "\n";
    }
};

int main () {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);

    short t;
    cin >> t;
    for (short tc = 1; tc <= t; tc++) {
        int n, m, k;
        cin >> n >> k;
        int d[n];
        m = 2 * (n + 1) - 1;
        SegTree<Node> segTree(m);
        for (int i = 0; i < n; i++) cin >> d[i];
        for (int i = n - 2; i >= 0; i--) {
            if (i + k < n) {
                int j1 = n - 1 - i - k, j2 = n - 1 - i - 2 * k - 1;
                Node val1 = segTree.query(0, 0, j1, 0, n), val2;
                if (i + 2 * k + 1 < n) val2 = segTree.query(0, 0, j2, 0, n);
                val1.val += d[i];
                val1.len += k + 1;
                segTree.update(0, val1.len, val1.len, val1, 0, n);
                if (i + 2 * k + 1 < n) {
                    val2.val += d[i];
                    val2.len += k + 1;
                    segTree.update(0, val2.len, val2.len, val2, 0, n);
                }
            }
        }
        cout << segTree.query(0, 0, n, 0, n).val << "\n";
    }

    return 0;
}

Submission link: 311909974

Thanks in advance.

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

the editorial with hints are way better than others

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

Why account named AnotherAccount was banned?

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

Does any $$$O(NK)$$$ solution for D exist? I have been thinking a lot on how to solve it and trying to see if anyone does to solve it, but it's either $$$O(N^2/K)$$$ or $$$O(N^2)$$$ (both of which I have did) or $$$O(N\log{N})$$$ (intended sol)

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

For Problem D, I understand DP won't work because of the data range. But if I want to write a DP solution, how to do it? What's the transition function?

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

Error_Yuan , Serval ; for problem F , it might be that $$$l[x] = r[x]$$$ at some positions , so how do we really decide which way they shall go ? I observed that if I push them to a side where they are in majority , would be optimal for a given fixed position p , but I submitted few submissions , and perhaps in the optimal answer it doesn't matter where we push them to . Can we formally prove that $$$l[x] = r[x]$$$ case won't happen at some optimal positions or it doesn't matter whether it happens or not ?

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

    It doesn't matter. You just need to know that an optimal strategy always use $$$k/2$$$ l-s and $$$k/2$$$ r-s. Thus, if we encounter the global optimal middle point, it does not matter which we choose if $$$l=r$$$ — the answer will keep the same.

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

In Problem D why can't we just take the floor(n/k+1) max deliciousness plates from start to n-k th index?

Edit — NVM I got it. I am super dumb.

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

C using digit DP: 312651328. Mostly to practice technique. States dp[carry_x][carry_y]. For every digit try all values and see if we can make sum equal xor for first digits of k.

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

Can someone tell me what's wrong in my solution for E? It's almost similar to the Editorial approach except I don't have that Check thing for s = 0 case. I don't think it's needed either because due to the modulo property, the elements of B can never exceed the corresponding elements of A. So, the sum of B must be <= The sum of A. In case of equality where s == 0, B must be a permutation of A so max_element of A + 1 should always be a valid k.

Rest of the code is almost same as editorial. What's the issue then?

Code — 312319353

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

Could someone prove the optimality of part D? I don't think that the fact this is the best way to go about is it obvious. I get why you must have a = floor(n/(k+1)) sushis but how does this way of picking a sushi's at least as good as any other way of picking a sushis.

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

Problem E: 327326244

can someone please tell me why does this give tle, it runs in $$$nlogn$$$ which should be fine according to the problem constraints.

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

i just wonder why the code of problem F2 works , can anyone explain ?

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

Problem B I solved like this take consecutive element if any one is zero add {i, i+1} to ans as mex of that will be greater than zero and then if array is odd check last element is 0 or not if so add {second last, last} to ans after that all element are greater than 0 so at last add {1,n}. Maintain index as we delete elements also;

Accepted solution :- 364717400