Harbour.Space's blog

By Harbour.Space, history, 3 years ago, In English

Hope you all enjoyed the problemset! Editorials to problems H and I are not ready yet, we will add them as soon as possible. We apologize for weak pretests and easy problems for top participants.

By the way, solution authors did not necessarily prepare the problems. The solutions were chosen at random.

UPD. Editorials for H and I are ready! Wish you all productive upsolving and high ratings!

1553A - Digits Sum

Idea: bthero Preparation: 244mhq

Tutorial
Solution (kefaa2)

1553B - Reverse String

Idea: Errichto Preparation: Will not be revealed for now because we care for his/their safety.

Tutorial
Solution (BledDest)

1553C - Penalty

Idea: Errichto Preparation: BledDest

Tutorial
Solution (BledDest)

1553D - Backspace

Idea: Adel_SaadEddin, Zaher Preparation: Will not be revealed for now because we care for his/their safety.

Tutorial
Solution (Um_nik)

1553E - Permutation Shift

Idea: bthero, 244mhq Preparation: BledDest

Tutorial
Solution (BledDest)

1553F - Pairwise Modulo

Idea: 244mhq, Errichto Preparation: bthero

Tutorial
Solution (bthero)
Segment tree solution (bthero)

1553G - Common Divisor Graph

Idea: Adel_SaadEddin, Zaher, Errichto Preparation: Errichto

Tutorial
Solution (Errichto)

1553H - XOR and Distance

Idea: 244mhq Preparation: BledDest, mnaeraxr, 244mhq

Tutorial
Solution (BledDest)
Alternative solution (BledDest)

1553I - Stairs

Idea: bthero, 244mhq, BledDest Preparation: BledDest, 244mhq

Tutorial
Solution (BledDest)
  • Vote: I like it
  • +134
  • Vote: I do not like it

| Write comment?
»
3 years ago, # |
  Vote: I like it +94 Vote: I do not like it

Same in video format: https://youtu.be/NqgObtKz_jM

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

    This is what you want with coffee in the morning.Big Thanks

    • »
      »
      »
      3 years ago, # ^ |
      Rev. 2   Vote: I like it -40 Vote: I do not like it

      https://codeforces.me/contest/1553/submission/123329166

      here is a solution for problem D. since mr um_nik was so fast in solving that he forgot to add editorial

      The code is written in python and should be pretty self explanatory

      EDIT: THANKS FOR REPLY ERRICHTO. I didn't pay attention to the fact that he was a tester

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

        As you can read in the main blog, um_nik was a tester. He has nothing to do with the editorial.

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

          No offence, but I am curious: do you personally like problems B & C? It's very surprising to me that these are the best ideas an LGM, who's also involved in a number of CP activities, can come up with.

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

            No worries, I'm happy to explain my/our reasoning.

            I certainly like B & C more than problem A. In the discussion below some CF blogs, I've already argued that math puzzles aren't that good for easy problems. But yeah, you can also say that I'm getting out of shape and my taste is getting worse. I lost my hopes after feedback from my AGC 47.

            I suggested three easy problems: something rejected as A, penalties without question marks as B, and Reverse String with $$$n \leq 2000$$$ as C or with $$$n \leq 200$$$ as B. I would use those three in my round and I can defend that choice. In particular, I like Penalties most as it's a natural problem that I came up with during Euro Cup. "Given a binary string like 1011100000, say when the winner is already decided".

            Reverse String was rejected because it would be adjacent to the similar-styled Backspace. Penalties were too easy for the second slot (or so we thought) so somebody suggested question marks. Sadly, that change modifies the problem from cute to artificial. Testers then provided feedback that the round is too difficult for beginners. To fit the slot between Digits Sum and Penalties, we used Reverse String with $$$n \leq 200$$$.

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

what is the 85th token of 19th test case of problem D??

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

Hey, I am an author of problem F. We actually did not intend any $$$O(N \sqrt{M} \log M)$$$ solutions to pass at the beginning.

The thing is, my Fenwick tree solution works in ~400 ms, but my segment tree solution works in ~2.5 s. Segment tree with pushes does not even pass the time limit because of a high constant factor. Also we had to care about Java solutions, so I decided to raise the TL from 3s to 4s, letting $$$O(N \sqrt{M} \log M)$$$ to pass.

I know I had to fix this problem in a more adequate way by experimenting with constraints. Will certainly do that in future rounds. Sorry about that.

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

    I think you mean $$$\mathcal{O}(N\sqrt{M*\log{M}})$$$ solutions, as I am not aware of any $$$\mathcal{O}(N\sqrt{M}*\log{M})$$$ solution. The thing is, mine was nowhere close even with pragmas and the 64-bit compiler.

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

      My $$$\mathcal O(N \sqrt{M} \log M)$$$ passes pretty comfortably within the TL. Unfortunately, I was not able to debug this quickly enough during the contest.

      Submission

      • »
        »
        »
        »
        3 years ago, # ^ |
        Rev. 2   Vote: I like it +55 Vote: I do not like it

        My solution with a time complexity of $$$\mathcal{O}(N\sqrt M)$$$ passed all the pretests using $$$3400\space\text{ms}$$$ and then passed all the system tests using $$$3416\space\text{ms}$$$.

        Note: this is not a comment talking about creative solutions, but a comment talking about how to use some ordinary data structure to compensate stupidity, so please forgive me if this sounds not so inspiring...... QwQ

        Just sharing some thoughts...... QwQ

        If you use enumerate all of the possible results of $$$\lfloor\dfrac{a_i}{x}\rfloor$$$, you will end up with a condition in which you need to maintain a sequence allowing you to perform to operations:

        • Updating one single element in the sequence(we will simply call this "update").
        • Query the sum of a continuous prefix of the sequence(we will simply call this "query").

        And the length of the sequence is $$$\mathcal{O}(M)$$$, the total number of "update"s is $$$\mathcal{O}(N)$$$, while the total number of "query"s is $$$\mathcal{O}(N\sqrt M)$$$.

        You choose a power of $$$2$$$ near $$$\sqrt M$$$, in my case I chose $$$2^9=512$$$, and divide the sequence into several blocks, each have a length of $$$512$$$.

        You maintain two sequences, $$$C_i$$$ and $$$\Delta_i$$$, in which $$$C_i$$$ means the sum of elements in the $$$i-1$$$-th, $$$i-2$$$-th, ......, and the $$$1$$$-st block, while $$$\Delta_i$$$ means the sum of elements in the $$$i$$$-th position, $$$i-1$$$-th position, $$$i-2$$$-th position, ......, and the earliest position that still belongs in block $$$i$$$.

        It's not hard to see the answer to any queries to position $$$i$$$ is $$$C_{\lfloor\dfrac{i}{2^9}\rfloor}+\Delta_i$$$, meanwhile $$$\lfloor\dfrac{i}{2^9}\rfloor$$$ can be calculated quickly enough using the bitwise >> operation.

        When an update occurs, if the update is a plus $$$x$$$ at position $$$i$$$, you just enumerate all the blocks $$${\lfloor\dfrac{i}{2^9}\rfloor}+1,{\lfloor\dfrac{i}{2^9}\rfloor}+2,{\lfloor\dfrac{i}{2^9}\rfloor}+3$$$,...... and add $$$x$$$ to the $$$C$$$ of those; and within block $$${\lfloor\dfrac{i}{2^9}\rfloor}$$$, you just enumerate all the positions and add $$$x$$$ to the $$$D$$$ of those.

        This data structure handles each update in $$$\mathcal{O}(\sqrt M)$$$ time, each query in $$$\mathcal{O}(1)$$$ time, or constant time, and it also has a pretty excellent constant factor.

        My implementation(may be very ugly QAQ) is here: Submission #123330049 — Codeforces

        Thank you very much for your time reading this! I really appreciate it.

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

          It is amusing that this is kind of a "two-layer" segment tree, and if you change it into three layers, the time complexity handling updates will reduce to $$$\sqrt[3]M$$$ while not changing the time complexity of queries(since its adds up $$$3$$$ pieces of information instead of $$$2$$$).

          That means that you can actually chose ANY constant number $$$\lambda$$$ and make the time complexity $$$\mathcal{O}(\sqrt[\lambda]M)$$$ — $$$\mathcal{O}(1)$$$......

          But in practice I've only seen choosing $$$\lambda=2,3,4$$$ and a large proportion of them $$$2$$$...... QwQ

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

            Shouldn't the complexity be $$$O(\sqrt[\lambda]{M} + M)$$$?

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

              Thanks for your correction, but I meant to say that It costs $$$\mathcal{O}(\sqrt[\lambda]M)$$$ time to use the data structure to handle EACH update operation, and $$$\mathcal{O}(1)$$$ time to use the data structure to handle EACH query operation QwQ.

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

                $$$\lambda = \log n$$$ requires $$$O(\log n)$$$ for both updates and queries.

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

Regarding problem E, could someone elaborate on transforming a permutation into another one? Why does the provided algorithm work? Could you provide some online resource about this?

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

    Suppose you have a connected component of size $$$k$$$. It will surely be a cycle. You can easily show that this cycle can be fixed using $$$k-1$$$ swaps, but not less. In total this will sum up to $$$n - cycles$$$ operations.

    Also you could try mixing elements between different cycles, but you will see it does not help either. This is not a formal proof, just a simple explanation built on pure intuition and logic.

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

      Thanks for the intuition. I derived the contradiction of minimality for the case of swapping vertices between two components. And as for the $$$k - 1$$$ swaps, I guess proof is redundant, seems logical to me. :)

      Also thanks to Errichto for his comment.

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

    You need $$$k-1$$$ swaps to transform $$$[1, 2, 3, 4, \ldots, k]$$$ into $$$[2, 3, 4, \ldots, k, 1]$$$. Swapping two values (e.g. the first two) puts one element in proper position. This happens for $$$k-2$$$ swaps and then the last swap puts the last two elements in proper positions.

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

F in chat for person who lost scholarship due to hacks

»
3 years ago, # |
Rev. 2   Vote: I like it +17 Vote: I do not like it

I used a sledgehammer to crack a nut. Used KMP algorithm in B.
My submission: 123364212
Works in $$$\mathcal O(n^2)$$$ probably.

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

    Could U give a brief solution on your idea?

    • »
      »
      »
      3 years ago, # ^ |
      Rev. 2   Vote: I like it +1 Vote: I do not like it

      In short, I'm searching for all prefixes of $$$t$$$ and reverse of the remaining part in $$$s$$$.

      Spoiler

      Edit:
      1. $$$n$$$ is the length of $$$t$$$.
      2. All searches are done in $$$\mathcal O(n)$$$ as the two substrings to be searched add up to a length $$$n$$$.
      3. $$$0$$$-based indexing is used.

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

My solution to H is different from the first one provided in the editorial, and the second one provided in the editorial is too vague, so I’ll describe mine here.

Alternate Solution for 1553H - XOR and Distance

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

I think it should be $$$2^{10}$$$ in the editorial of C, instead of $$$2^10$$$.

How to solve B in $$$O(n^2)$$$, I thought a lot but couldn't come up with a solution.

  • »
    »
    3 years ago, # ^ |
    Rev. 2   Vote: I like it +1 Vote: I do not like it

    Suppose x is the starting position, y is the rightmost position you covered and z is the leftmost position you covered. For every pair of x and y, z is unique (if it exists)(because length of t is fixed). Now you can easily compare by hashing.

  • »
    »
    3 years ago, # ^ |
    Rev. 2   Vote: I like it 0 Vote: I do not like it

    here : 123296482

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

    solution i think my dp solution is n2 .correct me if i am wrong.

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

      123306389 my dfs solution is truly similar to yours.I think that the dp array in your solution isn't necessarry and it may not work because we keep going right and then keep going left.So we cannot get repetitive (i,j,flag),right? of course i think our solutions are O(n²) and it may work even faster in tests.

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

TerriblePretestsForces

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

Preparation : will not be revealed for now cuz we care for their safety ...lol .. Btw fstforces

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

Wow, problem B was really harder than many people thought.

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

    Not so hard, as $$$\Theta(n^3)$$$ is really easy to think and code.

    Many people thought they can work out $$$\Theta(n^2)$$$ ,but they failed. That's no need.

    • »
      »
      »
      3 years ago, # ^ |
      Rev. 4   Vote: I like it -15 Vote: I do not like it

      But why $$$O(n^{3})$$$ passes as it is $$$500^{3} = 1.25 * 10^{8}$$$ operations but it should be maximum of around $$$10^{7}$$$ per second, right?

      Edit — Found this

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

        it's around $$$2 * 10^8$$$ I think, CF's judging machines are fast, and I think $$$2 * 10^8$$$ is also standard for ICPC

      • »
        »
        »
        »
        3 years ago, # ^ |
        Rev. 2   Vote: I like it 0 Vote: I do not like it

        it's not strict n^3 it's lesser than n^3 / 2 and even n^3 alone runs easily under a second

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

        I think it's $$$O(N^3)$$$ because $$$std::string.find$$$ is quadratic in worst case

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

      Ya, same happened with me!

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

I had totally ignored bound on m, got that cyclic swaps part during the contest, but screwed all the time by thinking about how can I optimize my solution. Now I have learned from this mistake to read questions carefully :)

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

I think tests on H are weak. I passed them with solution with some stupid approaches + random, but after I looked at the tests I noticed that all big tests have all 1's as answers. Here is the solution that takes random pair i, j and calculates for all x and if n>200 writes all 1's.

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

the pre testcases was weak and made a round not fair for some people i solved a b c d and hacked 6 subbmisions so i was in 1000 place but after main test found d get wrong answer and become in 2200 place it was big shock for me

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

https://codeforces.me/contest/1553/submission/123309155 in this submission i used dp with size 600 but lenth of t maybe equal to 999 why it didnt get run run time error can any one answer me??

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

Where is the D?

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

    read um_nik's code and dry run on a few cases u will understand.

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

used brute O(n^3) solution in B. Accepted in PyPy in only 358ms
123296399

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

In problem E, can someone provide me a proof why for each cycle of size $$$k$$$ the minimum number of swaps is $$$k-1$$$ ? couldn't it be less than $$$k-1$$$ swap?

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

    Proof works with induction.

    The base case is that k==2, then one swap fixes both elements. Else k>2, and swapping two elements fixes at most one of them, leaving us with a cycle of size k-1. It is easy to see that swapping two elements without fixing one does not give a better solution.

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

H can be implemented in a similar style to FFT/FWHT. I don't know if there is a way to interpret this in terms of polynomials.

The code is quite short (shortest submission at time of writing): 123356112. It uses $$$O(k2^k)$$$ time and $$$O(2^k)$$$ memory.

»
3 years ago, # |
  Vote: I like it -23 Vote: I do not like it

actually,i dont think that weak pretest make contest somehow worse.becouse it teach u to be more attentive and re-read code even if it got AC on pretest.And every solution tests on same set of pretests so every one is on equal terms.

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

Why can not I see the tutorial of the problem D

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

    Because tutorial of D is not available.

  • »
    »
    3 years ago, # ^ |
    Rev. 6   Vote: I like it +1 Vote: I do not like it
    Solution for D

    The tutorial for D is most likely not written yet or its written and is undergoing some edits. Whatever the case, the tutorial is still not out and available yet.

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

      thanks

    • »
      »
      »
      3 years ago, # ^ |
      Rev. 2   Vote: I like it 0 Vote: I do not like it

      why we have to move right to left ,i tried an approach where i am are moving from left to right and if difference between some part is even we can delete it

      but it is giving wrong answer my code

      can you please tell which case i am missing?

      or can you tell me how i can think of approach of moving pointer from right to left?

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

        You can just reverse strings right after reading them from the input and then do left to right processing in the rest of your code. That's what I did myself during the contest. If the standard C++ library doesn't offer a native way to reverse strings, then it makes sense to have such function somewhere in your collection of code templates.

        The main idea is that in many cases it is possible to do some cheap pre-processing of the input data to make the problem easier. Sometimes it's sorting of the input data, sometimes it's reversing the order of elements, etc.

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

    Why can not I see the tutorial of the problem D

    My random guess is that maybe the official solution for D was not originally intended to be greedy?

    I first tried to implement my solution as a recursive DP with a two-dimensional state array of 1-bit boolean states, but this is obviously not a good fit for the problem constraints and needed improvements. Still I used this code as a reference implementation to generate a lot of testcases. I thought that maybe a single-dimensional DP state array (storing something like the best index or whatever) could do the job. But then noticed that just a simple greedy modification doesn't seem to fail any of the generated testcases. This was a facepalm moment for me. Spent so much time on something that turned out to have just a greedy solution! The letter D was really deceiving. However observing many system tests failures and hacks after the contest made me change my mind to some extent. The time spent on thoroughly testing solutions locally was not a completely wasted effort after all.

»
3 years ago, # |
Rev. 4   Vote: I like it +8 Vote: I do not like it

I wake up and see result problem D Time limit on test 99 , very sad!!!!!!

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

hello, can you tell me the logic of penalty and why in last test case the answer is 9. It would be helpful if you give me some competitive programming tips. thanks

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

    Hello, which problem are you talking about here?

    As for the penalty, For every submission you get a non-AC verdict on tc != 1 during the contest, you get a penalty of 10mins, that is your solution is considered to be 10mins late after the actual AC verdict.

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

      I am asking about problem C. Can you help me why is the answer 9 in last test case of problem

      • »
        »
        »
        »
        3 years ago, # ^ |
        Rev. 5   Vote: I like it 0 Vote: I do not like it
        Explaination
»
3 years ago, # |
  Vote: I like it -24 Vote: I do not like it

Looks like whoever prepared the problem, provided the solution.

Does that mean problem $$$B$$$ is prepared by BledDest and $$$D$$$ by Um_nik ?

(Sorry for the mentions)

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

An Easy to understand Recursive Solution of Problem B

bool got(string s, string t, int i, int j, bool goingRight) {
    if (j == t.size()) return 1;
    if (i < 0 || i == s.size()) return 0;
    if (s[i] == t[j]) {
        if (goingRight)
            // then right and left both can be explored
            return got(s, t, i + 1, j + 1, 1) || got(s, t, i - 1, j + 1, 0);
        // otherwise you can go left only
        return  got(s, t, i - 1, j + 1, 0);
    }
    return 0;
}
void argon17()
{
    string s, t;
    cin >> s >> t;
    for (int i = 0; i < s.size(); ++i) {
        // try at every index
        if (got(s, t, i, 0, 1)) {
            cout << "YES\n";
            return;
        }
    }
    cout << "NO\n";
}
»
3 years ago, # |
  Vote: I like it +6 Vote: I do not like it

If you have noticed in editorial or not but I found this funny :- Preparation: Will not be revealed for now because we care for his/their safety. :)

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

    i noticed that too.haha i wonder if they will be in danger if their handles are shown?

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

When I tried to see the tutorial of problem D, I saw "Tutorial is not available". However, someone has asked a problem about the problem D's tutorial, so if anyone can tell me what's wrong?

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

I tried solving B using two pointers, getting WA on TC 14. Can anyone point out what's wrong? 123384175

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

In D,many codes failed for test 19 ,you guys just have looked the things from back like say you have two strings str1 abd str2 with length n and m respectively. All you need to do is keep a pointer(j) for str2 starting from m-1 coming back to 0 and start iterating for str1 from n-1 (i) if you find such i such that str1[i]=str2[j] and elements which didinot match their count is even then just decrement j and iterate from i-1 .No edge case to be handled in it. You can see my submission,its simple to understand.

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

    my solution was even simpler but had almost the same idea 123325031

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

      This is the solution for D, I am asking for B. I got the idea of O(n^3) approach given in the tutorial, just curious what's wrong with my approach.

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

    what s the difference from left to right or right to left ?

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

Oops, sorry about that!! Thanks to Nezzar and Rossoneri_Forever!!

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

    Your output is lowercase, I think the checker converted it to uppercase.

»
3 years ago, # |
Rev. 5   Vote: I like it -11 Vote: I do not like it

I have easier solution for problem D , if needed I will explain the code as well

int t ; 
cin >> t ;
while( t -- ){
    string s ;
    string t ; 
    cin >> s >> t ;
    int indx = s.size() - 1 ;
    int test = 1 ;  
    string sk ; 
    for( int j = t.size() - 1 ; j >= 0 ; j -- ){
        for( int i = indx ; i >= 0 ; i -- ){
         if( s[ i ] == t[ j ] && ( indx - i ) % 2 == 0 ){
             sk = sk + s[ i ] ;
          indx = i - 1 ;
          break ;  
          }
        }
    }
    reverse( sk.begin() , sk.end() ) ; 
    //cout << sk << endl ; 
    if( sk == t ) cout << "YES\n" ; 
    else cout << "NO\n" ;
}
»
3 years ago, # |
  Vote: I like it 0 Vote: I do not like it

Great contest from which I learned a lot

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

Because I have missed this multiple times in the past and I missed it in E again. So, as a reminder to myself and also for it may help others. These are two other problems which use Constraint Exploitation.

First

Second.

If you know of more such Problems, it would be nice if you could comment them down here.

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

For Problem B, just enumerate all possible turing point (where you change direction), then you will get $$$|a|$$$ strings, which are $$$a'$$$s. Use KMP to query whether string $$$b$$$ is a substring of them, which costs $$$O(|a|(|b| + |a'|))$$$.

Since first step cost $$$O(n^2)$$$, all the problem can be solved in $$$O(n^2)$$$

Code is here: https://codeforces.me/contest/1553/submission/123304298

»
3 years ago, # |
Rev. 2   Vote: I like it +17 Vote: I do not like it

Deleted. 1001 way to overcomplicate your solution, lol.

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

    Lol.I did the same thing. I initially did 200 times and got an unnecessary TLE because that.

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

"Preparation: Will not be revealed for now because we care for his/their safety."

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

Can someone provide tutorial for Um_nik solution of problem D. It would be really helpful.

Thanks in advanced :)

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

    We first observe that if we type backspace, 2 characters in the origin text will be deleted. If the difference of the lengths between the origin text and the expected text is odd, then we must delete at least one character at the beginning of the origin text (which is what this int p = (n - m) & 1; and for (int i = p;...) does). Then, if the character at s[i] == t[q], we can go and check the next. If not, then we must delete this mismatch. At mentioned at the beginning, we have to delete the both characters starting from the mismatch, which is what the variable k does. Now, at the end, if the index q == the length of expected text m, we have found a way. Otherwise, we cannot transform the origin text to the expected.

    And just saw the official editorial is also avaliable now.

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

in E's tutorial, what does — n/(n — 2n/3) signifies. I count cnt > n — 2m = n/3 why only 3 such counts are possible??

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

    If the sum of many numbers is n, only three of them can be greater than n/3.

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

      Thanks, I will read tutorial again, but can you explain why these 3 value of k are turned out to be only 0, 1, 2, 3?

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

        I don't understand your question. Are you asking about some particular test? You might want to watch the video explanation too, it's in the first comment below the blog.

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

          I got the solution completely, thank you so much, man...I don't know whether it will benefit me in the future or not to get problems solved using editorials and looking at other's code.

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

            It will benefit you. Upsolving is much more important than solving problems during the contest.

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

Despite several comments mentioning KMP/Z-function as applicable to problem B, I don't see any mentioning that it can actually be solved in linear time $$$O(|s| + |t|)$$$ with that tool.

The basic idea is that the 'turning point' where the chip goes from moving right in $$$s$$$ to moving left must happen in the middle of a palindromic suffix or prefix of $$$t$$$ with some odd length $$$2k+1$$$. The substring of $$$s$$$ visited by the chip in this case is then either the substring of $$$t$$$ without its last $$$k$$$ characters (for palindromic suffixes) or the reversal of the substring of $$$t$$$ without its first $$$k$$$ characters (for palindromic prefixes).

In either case, it is clearly optimal to take $$$k$$$ as large as possible, i.e. use the longest odd-length palindromic prefix or likewise suffix of $$$t$$$. These can easily be found in $$$O(|t|)$$$ with KMP/Z-function, and then the problem is reduced to testing $$$s$$$ for the presence of either of two substrings.

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

I felt like sharing my $$$\mathcal{O}(N \sqrt M + (M + N) \sqrt N)$$$ solution for F.

The basic idea of using the identity $$$a \text{ mod } b = a - b \lfloor \frac{a}{b} \rfloor$$$ and grouping queries by the same values of $$$\lfloor \frac{a}{b} \rfloor$$$ is the same as the tutorial. What is different is that when we are checking the $$$k$$$-th element $$$a_k$$$ we separate in two cases (1) $$$a_k <\sqrt M$$$ and (2) $$$a_k \geq \sqrt M$$$.

We also have an array $$$B$$$ of size $$$\sqrt M$$$ which is updated with the value of $$$x \lfloor \frac{a_k}{x} \rfloor$$$ for all $$$x \leq \sqrt M$$$ on each $$$a_k$$$.

To calculate the sum of $$$a_i \lfloor \frac{a_k}{a_i} \rfloor$$$ for $$$i < k$$$: If (1) $$$a_k <\sqrt M$$$, we just iterate over all $$$a_i < a_k$$$ (as simple optimization, not really needed), and if (2) $$$a_k \geq M$$$, we iterate over values of $$$\lfloor \frac{a_k}{a_i} \rfloor$$$, of which there are at most $$$2\times\lceil\sqrt a_k\rceil$$$.

To calculate the sum of $$$a_k \lfloor \frac{a_i}{a_k} \rfloor$$$ for $$$i < k$$$: If (1) $$$a_k <\sqrt M$$$, we get the value from array $$$B$$$, and if (2) $$$a_k \geq \sqrt M$$$ we query over at most $$$\sqrt M$$$ ranges and sum.

So far, it is not exactly clear how to avoid either having $$$\log M$$$ queries or $$$\log M$$$ updates. What we do now is to separate the array in $$$\sqrt N$$$ groups and update all auxiliary arrays only once for each group. The queries only account for the values of the previous groups, which means that the values missing are of $$$a_i \text{ mod } a_j$$$, for pairs that are in the same group. These values can be recovered offline by checking $$$(\sqrt N)^2$$$ pairs in each of the $$$\sqrt N$$$ groups.

Cool thing about this strategy is that (to my knowledge) it still works if there are repeated values in A.

code

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

For problem B, we can fix the segment [i,j] which goes from left to right, then calculate k, in that way we can fix the chip's route and then we can use hash to check. The time complexity is $$$O(n^2)$$$.

But I wonder whether we can solve it in an $$$O(n \log n)$$$ or quicker way.

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

the solution of Problem F may be wrong because it won't print any integer on test 26. Please check your standard solution, thanks.

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

What's wrong with test #26 of problem F? I don't know why the jury says the length of answer should be 0. I tried some AC codes, but they all get WA on test 26.

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

Is there a problem with the 26th test point of question f? The code of the person I have used is still wrong. STD can't pass

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

Unfortunately, the testing on test 26 of problem F was malfunctioning for the last several hours (I don't know the exact reason, probably something with Codeforces cache). The problem is fixed by now, all submissions that failed test 26 will be rejudged in a few minutes.

»
3 years ago, # |
  Vote: I like it -10 Vote: I do not like it

In problem E I think you mean the sum of cnt[k]= n, not n! This is why mathematicians shouldn't get overly excited (:

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

Can we use submask enumeration technique to generate all possibilities for problem C?

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

Will preparation for B and D ever be revealed?

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

I tried using DP, but my solution gives the wrong answer. Can someone please tell me what I did wrong

#include<bits/stdc++.h>
#define ios                       \
    ios_base::sync_with_stdio(0); \
    cin.tie(0);                   \
    cout.tie(0);
#define endl "\n"
#define int long long
#define YY cout<<"YES"<<endl
#define NN cout<<"NO"<<endl
#define yy cout<<"Yes"<<endl
#define nn cout<<"No"<<endl
using namespace std;
vector<vector<int>>mem;
bool solve(string &s,string &t);
bool solver(string &s,string &t,int i,int j);
signed main(){
    ios;
    int q;
    cin>>q;
    while(q--){
        string s,t;
        cin>>s>>t;
        bool ans=solve(s,t);
        (ans)?YY:NN;
    }
    return 0;
}
bool solve(string &s,string &t){
    mem=vector<vector<int>>(s.size(),vector<int>(t.size(),-1));
    for(int i=0;i<s.size();i++){
        if(solver(s,t,i,0)){
            return true;
        }
    }
    return false;
}
bool solver(string &s,string &t,int i,int j){
    if(j==t.size()){
        return true;
    } else if(i==s.size() or i<0){
        return false;
    } else if(mem[i][j]!=-1){
        return mem[i][j];
    } else {
        return mem[i][j]=(s[i]==t[j]) and (solver(s,t,i+1,j+1) or solver(s,t,i-1,j+1)); 
    }
}
»
8 months ago, # |
  Vote: I like it 0 Vote: I do not like it

does anybody has a dp solution for D?