MrPaul_TUser's blog

By MrPaul_TUser, history, 3 years ago, translation, In English

Ideas: MikeMirzayanov.

1551A - Polycarp and Coins

Tutorial
Solution

1551B1 - Wonderful Coloring - 1

Tutorial
Solution

1551B2 - Wonderful Coloring - 2

Tutorial
Solution

1551C - Interesting Story

Tutorial
Solution

1551D1 - Domino (easy version)

Tutorial
Solution

1551D2 - Domino (hard version)

Tutorial
Solution

1551E - Fixed Points

Tutorial
Solution

1551F - Equidistant Vertices

Tutorial
Solution
  • Vote: I like it
  • +73
  • Vote: I do not like it

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

good contest for "try of pen",i like it,thanks.waiting soon for your div 2 and div 1 contests.

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

Even though the DIV3 was harder than the last few DIV3's, I think the problem set was way more enjoying and educational for a lot of people. I hope you set more DIV3's MrPaul_TUser

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

    But please, make it easier, as a person with <1600 rating,I can say, that at least 4-th problem was very and very hard for actual div3 participants.MrPaul_TUser Thank you anyway. ;)

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

      actually,i dont think that this contest was harder than standart div 3

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

Transposing the table in D2 was a nice idea! Saves a lot of casework.

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

A slightly shorter branchless alternative implementation of A, which does the same job (adding 1 to n before division by 3 is the same as incrementing the result by 1 in the case if the remainder was 2):

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

Please help me in this code (problem B2).
I'm not able to figure out where I'm doing wrong.
This code is failing on tescase-185 of test-2.
My submission

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

    when ur are considering the nos having frequency less than k the order also matters suppose there are 4 colors. and 1,2,3 are having count less than 4. if the arrangement is like : 1 2 2 3 1 nos in array. 1 2 3 4 1 coloring . the no 1 is getting the same color.

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

      got it. Thanks!

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

      why this solution for b2 isgiving tle pls help ~~~~~ void _paint_the_town_red() { ll n, k; cin >> n >> k; vl v(n); fa(v) cin >> val; vl temp2 = v; vl cnt(1e6); for (int i = 0; i < n; i++) cnt[v[i]]++; vector temp(k); ll t = 0; sort(all(v)); ll i = 0; while (i < n) { ll j = min(k, cnt[v[i]]); while (j--)//this is main problem { temp[t].pb(v[i]); t = (t + 1) % k; } i += cnt[v[i]]; }

      ll maxi = 1e18;
      for (int i = 0; i < temp.size(); i++)
          maxi = min(maxi, (ll)temp[i].size());//o(n)
      for (int i = 0; i < sz(temp); i++)
      {
          while (sz(temp[i]) > maxi)
              temp[i].pp;
      }
      map<ll, vl> mp;
      for (int i = 0; i < sz(temp); i++)
      {
          for (int j = 0; j < sz(temp[i]); j++)
          {
              mp[temp[i][j]].pb(i + 1);
          }
      }
      for (int i = 0; i < n; i++)
      {
          if (mp[temp2[i]].size() > 0)
          {
              cout << mp[temp2[i]].back() << " ";
              mp[temp2[i]].pp;
          }
          else
              cout << 0 << " ";
      }
      cout << _endl;

      } ~~~~~

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

Really enjoyed A and B! Thanks for a great contest.

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

https://codeforces.net/contest/1551/submission/123610411 This is my submission for problem F. According to me the complexity of my solution is of degree 4 so how did this not give TLE?

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

    you are lucky

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

      Yeah but still the runtime is just 62ms

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

        clear $$$n^3k$$$ time for $$$n,k<=100$$$

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

        The O(n^3k) part of your code is setting the dp table to -1, which has a small constant and runs very quickly (10^8 operations). Your recursive solve function for every root and distance runs in O(n^2k) time since every edge can only be connect to 2 vertices. Iteration to find combination over each edge happens at most twice each edge.

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

What if problem C was changed a bit and one letter should have more occurence than other letters taken individually, and not in total, how should we proceed then?

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

    I misread the problem as you described during contest and was banging my head to get a solution :(.

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

    Not sure whether this is correct, but here is what came to my mind.

    We can calculate the maximum number of words we can take considering every character as the 'dominant' character.

    Let us say we want to have 'a' as the dominant character. We iterate over all the words one by one, taking all of them in our collection (for now) and keeping track of the total frequency of every character till now. We also maintain separate sets for F(s, 'a', c2), where F(s, 'a', c2) represents this — In word s, frequency('a') — frequency(c2).

    So now, suppose we encounter a word due to which 'a' is no longer our dominant character, but instead 'b' is. We refer to our set for F(s, 'b', 'a') and remove the the word s which has the largest F(s, 'b', 'a') from our collection (and also from other sets).

    The idea is, take all words as you encounter them. But if a certain words causes 'a' to lose its dominancy to some other character (say 'b'), we will remove such a word which will reduce the total frequency of 'b' as much as possible while not hurting 'a' much, thus restoring dominancy of 'a'.

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

    bro, just realized i read the problem wrong,thnks

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

The contest was great! It would be better if the editorial for C, would be more explained!

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

It is possible to solve E with a 1D dp. Details in spoiler.

Spoiler

Submission: 123519134

  • »
    »
    3 years ago, # ^ |
      Vote: I like it 0 Vote: I do not like it
    $$$ (i - a_i) - (j - a_j) \le i - j - 1$$$

    seems to be just

    $$$ a_i > a_j $$$

    right?

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

      Yeah...you can simplify the condition like that. I just thought it made more sense to present it the way I did.

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

Tutorial for D2 seems complecated.

We can reduce to two cases: Number of rows is odd or is even.

In case odd we need to place one row of horz dominoes, max k dominoes. In case even we do not.

Then we place allways two rows of dominoes at once, until k dominos are placed. Then fill all other fields with vert dominoes.

Finding a useable letter foreach dominoe is not hard, simply check the surrounding cells. 123518743

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

    In case odd we need to place one row of vert dominoes

    I think there should be horizontal dominoes instead of vert..as in one row only horizontal dominoes can come.

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

If anybody is interested and have enough time to help me, please do. I understand their are better approaches for B2 then this, but I am not able to understand why did this approach failed.

Approach :

  1. Find how the max number which we can allot colors by desired conditions , say this is x
  2. loop through k colors : 1.assign kth color to a number 2.Store that number in unordered_map, so that same color doesn't get assigned to same number 3.Maitian a count=x, which is basically the number of time to allot and decrease it everytime the color is alloted.

Here is the link for the submission — fails on tc 554 123566013

Also show no mercy if the code is bad, so that I get to learn how to code better than what I have done.

Peace out!

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

    use this tc 1 6 3 1 2 5 3 3 3 your solution will output 1 1 2 2 3 0 but one of the correct ans will be in the form 1 2 3 1 2 3 I think your solution will also give tle for larger value of n. if you till have any doubt feel free to ask me.

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

Is it possible to solve Equidistant Vertices in faster than O(N^2 K) (maybe with some optimization in the dp)?

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

    Here's what I think. If you fixed the root, and then you fixed the depth of the nodes from the root, you want to find the number of ways to pick $$$k$$$ nodes from them so that their pairwise LCA is the root. Naturally, this splits up our set into some $$$m$$$ groups, from which we can fix $$$k$$$ groups and then the number of ways would be the product of the sizes of those groups. Let's say the group sizes are $$$g_1, g_2,...,g_m$$$. If we consider the polynomial $$$(x+g_1)(x+g_2)...(x+g_m)$$$, then the value we want is the $$$k^{th}$$$ coefficient starting from the largest term ($$$0$$$-indexed). Now, this product can probably be computed in $$$\mathcal{O}(n {\log}^2 n)$$$ with divide-and-conquer+FFT/NTT for an overall complexity of $$$\mathcal{O}(n^2{\log}^2 n)$$$. I haven't implemented it myself, so please point out the error if you think I'm wrong.

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

      Oh that's a nice idea. How is Kth coefficient computed in O(N log^2(N)) though?

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

        Well, I think the following: If we split our product into two halves and compute each half, we'll get two polynomials of roughly equal degree, which we can then multiply with FFT/NTT. The recurrence relation would look like $$$T(n)=2T(\frac{n}{2})+ \mathcal{O}(n\log n)$$$, and we can use the master theorem for divide-and-conquer to get that this works out to $$$\mathcal{O}(n{\log}^2 n)$$$. We can simply extract the required coefficient from the final answer presented in the form of a list of coefficients.

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

          Ah I see. Nice solution! Could this be done faster with generating functions?

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

I am getting TLE for test case two for prob B2. Can someone tell me is my approach wrong as I am getting TLE even though tc is NlogN. My solution https://codeforces.net/contest/1551/submission/123636719

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

    When the number of test cases is equal to 10000 your vectors hsh[200005] and prev[200005] initializations take too much time. Try the size of vectors N like in your v[] and ans[] vectors.
    But your greedy idea to take the first vacant color is incorrect. Try test:
    1
    9 3
    1 2 3 4 5 6 6 7 7
    Your answer is:
    1 1 1 2 2 2 3 3 0
    The correct answer may be:
    1 2 3 1 2 3 1 2 3

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

Problem B2:

let's calculate for each of k colors the number of elements painted in the color — all calculated numbers must be equal

I am not understanding how are we managing this condition in given tutorial. Please anybody explain

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

    Take k occurrences of the numbers whose frequency is greater than or equal to k. For all other numbers, take the total sum of frequencies of these numbers (let say the sum is s), and color a*k of these occurrences s.t. (a+1)*k>s .

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

Another approach for B2 would be doing binary search on how many elements can be painted in one color.

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

    Can you please elaborate your idea of binary search because I was trying to do it through binary search but got tle on tc5.Although B1 passes due to low constraints . Here's my submission https://codeforces.net/contest/1551/submission/124039451 Can I improve my code or do I need to think of solving it through another approach ?

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

      You need to change your good() function. You should use something like set which will keep the pairs sorted. You can seelink my check() function

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

147410960 Hii! can someone check why is it failing on test case 1, even when everything is correct according to me. Thanks!

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

    If you look at the output, you have a situation where two vertical dominos, sharing a side have same color. This is not allowed, as mentioned in problem as well.

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

Could anyone check why this submission of mine get WA for this test case (wrong answer invalid table description) :

1
45 90 967

When I change the line $$$38$$$ from char ans[n][m] to vector<string> ans(n, string(m, 'x')) as in this submission I get AC. So it probably has something to do with iterate through an ans[i][j] that hasn't been initialized, but I manually checked the result and didn't see anything wrong.

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

Hey guys, did you realise that in problem B2 the example was referring to the irrational number (pi) in the last three test cases? Nice one :D