Блог пользователя awoo

Автор awoo, история, 2 года назад, По-русски

1922A - Необычный шаблон

Идея: Roms

Разбор
Решение (awoo)

1922B - Формирование треугольников

Идея: Roms

Разбор
Решение (Roms)

1922C - Ближайшие города

Идея: BledDest

Разбор
Решение (Roms)

1922D - Безумные монстры

Идея: BledDest

Разбор
Решение (Neon)

1922E - Возрастающие подпоследовательности

Идея: Roms

Разбор
Решение (Neon)

1922F - Замена на подотрезке

Идея: Roms

Разбор
Решение (Neon)
  • Проголосовать: нравится
  • +116
  • Проголосовать: не нравится

»
2 года назад, скрыть # |
 
Проголосовать: нравится +43 Проголосовать: не нравится

If using a double linked list to implement 1922D, we can get a solution of O(n) time.

»
2 года назад, скрыть # |
Rev. 2  
Проголосовать: нравится +24 Проголосовать: не нравится

In problem E we could also construct an increasing sequence 1, 2, 3, ..., N. We could easily see that it has S = 2^N increasing subsequences (including the empty one).

If S*2 equals X, we just need to add N+1 at the end, else we need to add some values to get X-S more increasing subsequences.

Notice that if we add a value 1 <= v <= N to the end of the sequence, we would get 2^(v-1) more increasing subsequences, so we just need to check each bit from MSB to LSB.

Submission: 242459743

  • »
    »
    2 года назад, скрыть # ^ |
     
    Проголосовать: нравится 0 Проголосовать: не нравится

    Thanks!!!.

  • »
    »
    2 года назад, скрыть # ^ |
     
    Проголосовать: нравится +4 Проголосовать: не нравится

    I used this method in the contest

  • »
    »
    2 года назад, скрыть # ^ |
    Rev. 3  
    Проголосовать: нравится 0 Проголосовать: не нравится

    I thought of the same approach. But, I am getting first test case wrong on system test 6.

    For 905093722480139348, your code returns:

    1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 59 56 52 51 50 49 48 44 41 40 39 38 37 33 32 31 30 29 28 24 23 21 20 19 17 15 13 12 11 7 5 3

    The largest number is 59, so according to your logic it should be 2^59. The remaining numbers: 59 56 52 51 50 49 48 44 41 40 39 38 37 33 32 31 30 29 28 24 23 21 20 19 17 15 13 12 11 7 5 3

    Should be 2^(v-1), which when I calculate turns out to sum with 2^59 as 905093722480139264. What am I doing wrong?

    This is the code I used to test:

        ll ar[] = {59,56,52 ,51,50,49,48,44,41,40,39,38,37,33,32,31,30,29,28,24,23,21,20,19,17,15,13,12,11,7,5,3};
        ll sm = pow(2LL, 59);
        for (int i = 0; i < 32; ++i) {
            sm += pow(2LL, ar[i]-1);
        }
    
        cout << sm;
    

    My submission num (Java): 242767859

    • »
      »
      »
      2 года назад, скрыть # ^ |
       
      Проголосовать: нравится 0 Проголосовать: не нравится

      Change the line sm += pow(2LL, ar[i]-1); to sm += (ll)pow(2LL, ar[i]-1); and it should produce the correct result. In C++ the pow function return double typed value, adding double in that range may give some miscalculation, I don't know much about Java but it could also be the same issue.

»
2 года назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

For problem B, I got accepted in the contest, but then I woke up this morning and got Time Limit Exceeded on Test case 6? I'm so confused why did this happen. I was supposed to hit expert.

»
2 года назад, скрыть # |
 
Проголосовать: нравится +21 Проголосовать: не нравится

Problem F can also be solved in $$$O((n^3 \cdot x)/64))$$$ using bitsets: link

Note that since $$$x$$$ is at most $$$100$$$, we can simply use 128 bit integers instead of bitsets for an even better constant factor.

»
2 года назад, скрыть # |
Rev. 3  
Проголосовать: нравится 0 Проголосовать: не нравится

I'm very confused by the editorial for E, so I feel like sharing my own (AC) approach:

First, observe that a strictly increasing array with $$$k$$$ elements will have $$$2^k$$$ increasing subsequences (including the empty subsequence), the elements to be removed can be any subset of all the elements. So for input $$$n$$$, we can find the largest power of 2 which is $$$\leq n$$$, e.g., for integer $$$p$$$ such that $$$2^p \leq n \lt 2^{p + 1}$$$, we can form an increasing array of $$$p$$$ elements to create $$$2^p$$$ subsequences (including the empty subsequence).

But what about the remaining $$$n - 2^p$$$ subsequences? Let's say the largest power of 2 you can fit in the remaining value is $$$2^q$$$, i.e., $$$2^q \leq n - 2^p \lt 2^{q + 1}$$$. Initially, I considered creating a separate subarray of $$$q$$$ elements, but having to do this with every power in the worst-case (e.g., if $$$n = 2^{59} - 1$$$) would exceed the 200-length limit (and we also have to avoid double-counting the empty subsequences).

Instead, however, we can generate $$$2^q$$$ new increasing subsequences by adding only one element to the end of the array: an element which is greater than the first $$$q$$$ elements of the original sequence (of $$$p$$$ elements) but smaller than the rest. The new subsequences generated involve the new element with any subset of the first $$$q$$$ elements, resulting in $$$2^q$$$ new subsequences (there are no empty subsequences here, since the new element is always included in these new subsequences).

This leads to the following algorithm: for the highest power $$$p$$$ of 2 that fits (i.e., leftmost 1 bit), prepare $$$p$$$ elements in strictly increasing order. This is the primary reference sequence. Then for the next power $$$q$$$ of 2 that fits, e.g., we simply add one extra element to the end whose value is between the $$$q$$$-th and the $$$(q + 1)$$$-th element of the primary reference sequence. Repeat for each power of $$$q$$$ that fits from the remnants of the previous step.

My submission: 242314370. The variable prm denotes $$$p$$$, the length of the primary sequence. I used multiples of 1000 for the primary sequence. Some of the code details overcomplicated this by considering when we might have multiple instances of the same $$$q$$$ value, but this is unnecessary with clear-headed hindsight, since we're basically just going through all the 1-bits in the binary representation from left to right. The __builtin_clz(n) function (count leading 0s) would likely have improved this, to quickly find the leftmost 1-bit each time.

»
2 года назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

My submission to E 242474581. I got long long overflow when I had (1ll<<i) for calculating power of 2s (Specifically at i=59). However, it did not overflow when I calculated poweroftwos by arithmetic (as in submission, the dp array).

Any suggestions why there was an overflow for (1<<i) case? 242470568

»
2 года назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

We can think of Problem-B in another way, we can assume that the three sides chosen by us are $$$(2^x, 2^{x+d_1}, 2^{x+d_1 + d_2})$$$ where $$$d_1, d_2 \geq 0$$$.

As mentioned in editorial in order to make a degenerate triangle we want $$$2^x + 2^{x + d_1} \gt 2^{x +d_1 + d_2}$$$ (Sum of two minimum is greater than maximum).

After simplfying above inequality we get $$$2^{d_1} \cdot (2^{d_2} - 1) \lt 1$$$, which is only possible when $$$d_2 = 0$$$, thus for each $$$a_i$$$ we just want to find out the number of $$$a_i's$$$ after it. As for the first side length i.e., $$$2^{x}$$$ it doesn't matter what we choose so our final answer is just $$$\sum_{i = 0}^{N - 1} i \cdot \text{count of}\ a_i\ \text{in range i + 1 to n - 1}$$$. (Note 0-indexing)

Here is my submission.

»
2 года назад, скрыть # |
Rev. 2  
Проголосовать: нравится 0 Проголосовать: не нравится

why this code give wrong answer forn D question 242491110

»
2 года назад, скрыть # |
 
Проголосовать: нравится +10 Проголосовать: не нравится

Can anyone please explain how tester of E could have been written so that it checks the answer in polynomial time? I can't think of one.

  • »
    »
    2 года назад, скрыть # ^ |
     
    Проголосовать: нравится 0 Проголосовать: не нравится

    let dp[i] be number of increasing subsequences ending at position i, then it's O(n^2) to check. If we use sorting+fenwick to find dp starting from the lowest value, then it's O(nlogn).

  • »
    »
    2 года назад, скрыть # ^ |
     
    Проголосовать: нравится 0 Проголосовать: не нравится

    I've written a checker for myself while solving problem E. Here's how it works: — Iterate through the sequence from left to right, and compute how many subsequences end with this index, and we store this value in a bBST — We check which numbers are less than the current one in our bBST (so to the left), and add their values up, because for each of these subsequences we can append the current number to the end — Then we store this value + 1 (because this value itself is also a valid increasing subsequence) in the bBST — Finally, we add up all elements in the bBST and add 1, because an empty one is also valid Here's my implementation: https://pastebin.com/5RFbryrk

»
2 года назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

I feel like my D should get hacked. Can someone try? submission

»
2 года назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

why is my code for problem D 242468805 getting a time limit exceeded signal ? shouldn't it be faster than this approach using sets?

»
2 года назад, скрыть # |
Rev. 3  
Проголосовать: нравится 0 Проголосовать: не нравится

For F, I feel like we don't need to precompute that much to eliminate cyclic dependencies. Instead, we consider two cases:

  1. ifa[l]==k: we calculatedp1[l][r][k]fromdp1[l+1][r][k] first, then calculatedp2[l][r][kk]fromdp1[l][r][k], where k!=kk.
  2. ifa[l]!=k: we calculatedp2[l][r][k]fromdp2[l+1][r][k] first, then calculatedp1[l][r][k]fromdp2[l][r][k].
My Code
»
2 года назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

In the solution to D, what does prev(it) return in case "it" is the iterator pointing to the first element in the set?

»
2 года назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

can someone explain problem D. I don't understand why the problem setter's claim is valid.

»
2 года назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

Hey, could someone explain how removeing cyclic dependencies in problem F works. Is there a general approche or can we only do this for this spezific problem. Thanks!

»
2 года назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

Can somebody explain why my solution is getting TLE'D on test 15 for problem D 242631259

»
2 года назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

How elegant is the solution is for E . I feel I am so dumb :)

»
2 года назад, скрыть # |
Rev. 2  
Проголосовать: нравится 0 Проголосовать: не нравится

It was already mentioned, sorry.

»
2 года назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

Why somebody solve F so quickly?

  • »
    »
    2 года назад, скрыть # ^ |
     
    Проголосовать: нравится 0 Проголосовать: не нравится

    because it is a standard dp on ranges problem as long as you get the dp2 idea quickly. Their method of handling cyclic dependencies is very very unnecessary.

    • »
      »
      »
      2 года назад, скрыть # ^ |
       
      Проголосовать: нравится 0 Проголосовать: не нравится

      how did you handled the cyclic dependencies ?

      Also I dont think it is that that bad , this was my solution after I read the editorial : https://mirror.codeforces.com/contest/1922/submission/242787688

      As you can see the only thing I did to get rid of cyclic dependencies was adding the 2 lines of code at the beginning of the function :

      while(l < (n-1) and l < r and ((a[l] == value) ^ typ))l++;
      while(r > 0 and r > l and ((a[r] == value) ^ typ))r--;
      

      Nonetheless I am still curious about how you handled because your approach is clearly faster than mine , and I couldnt really understand your logic from your submission

»
2 года назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

can enyone help me with this problem? I am tring to solve it using Hash in c++ but can't

https://mirror.codeforces.com/contest/271/submission/243292259

»
2 года назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

Surprised that the editorial lists an $$$O(n^4)$$$ for problem F, when it can be done in $$$O(n^3)$$$, also surprised that no one has mentioned it in the comments.

243829676

»
2 года назад, скрыть # |
Rev. 2  
Проголосовать: нравится 0 Проголосовать: не нравится

for D: why if the neighbors of $$$i$$$-th monster are alive we don't have to check the $$$i$$$-th in the next round? Isn't it possible that the neighbors to kill it in the next round? UPD: I thought the defense (d) is being decreased each round