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

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

1902A - Двоичный дисбаланс

Идея: BledDest

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

1902B - Зарабатывание баллов

Идея: adedalic

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

1902C - Вставь и приравняй

Идея: Roms и BledDest

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

1902D - Запросы робота

Идея: BledDest

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

1902E - Коллапс строк

Идея: Roms

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

1902F - Снова деревья и XOR-запросы

Идея: BledDest

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

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

E was awesome. Thanks for great problems

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

When will you update the rating change for this round?

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

C was the coolest

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

    can you please explain the sol of c.

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

      Wow, I remember that problem. I thought that it was a big pain to code, but it actually is a decent problem, especially if you don't split it into separate cases (there is no need to). Looking back, the best problem was definitely D, though. Well I haven't solved E or F so I really can't judge those, but out of the first 4, D was the best. At least C was the best of the first three.

      Anyway, this problem starts off with a pretty typical thing that appears in gcd problems. So it is good if you memorize this part. It goes like this: imagine you're given an array $$$a_1, a_2, a_3, \dots, a_n$$$ and you want to find the maximum $$$x$$$ value that makes it so each $$$a_i$$$ can be expressed as $$$nx + a_j$$$ $$$\forall \,1 \le j \le n$$$ where $$$n \in \mathbf{Z}$$$. Well it turns out that you can create a new array $$$a_2 - a_1, a_3 - a_2, \dots, a_n - a_{n - 1}$$$, and the largest $$$x$$$ will be the gcd of all the elements in that difference array. No one has found any cases where this doesn't work, so you can trust that it will work in all cases.

      It is no coincidence that this same $$$x$$$ value is the optimal $$$x$$$ value to add to each array after you insert your new element. Again, it is hard to prove exactly why that works, but you will be able to determine when to use this strategy after solving a few similar problems.

      So, assume you didn't insert any element, and you calculate $$$x$$$. Notice that, if you insert an element that makes $$$x$$$ smaller, it will change $$$x$$$ to $$$\frac{x}{2}$$$ or lower. Because of this, if you make $$$x$$$ smaller with your new inserted element, you $$$at\,\,least$$$ double your number of operations. But with your inserted element, you can double your operations (or do better) each time by first of all taking the max of the array $$$a_{max}$$$, and subtracting $$$x$$$ from it until you find an element that doesn't exist in the array. In the worst case, you can have something like this ($$$x = 2$$$).

      $$$0, 2, 4, 6, 8$$$. Notice that it takes $$$4 + 3 + 2 + 1 = 10$$$ operations without the inserted element. Now, we try $$$a_{n + 1} = 8$$$, $$$a_{n + 1} = 6$$$, $$$a_{n + 1} = 4$$$, $$$a_{n + 1} = 2$$$, $$$a_{n + 1} = 0$$$, but none of them work because all those values are in the array, so we have to do $$$a_{n + 1} = -2$$$. But when we do that, we can go $$$-2 \to 0 \to 2 \to 4 \to 6 \to 8$$$, which takes $$$5$$$ operations, which is less than doubling. The worst case might actually be something like this:

      $$$4, 6$$$ and $$$a_{n + 1} = 5$$$, we go from $$$1$$$ to $$$3$$$ operations. But we could also choose $$$a_{n + 1} = 2$$$, and that would also be $$$3$$$, so it is always bad to decrease $$$x$$$.

      And it is never good to put $$$a_{n + 1}$$$ to $$$a_{max} + x$$$, because then all of the sudden, you add $$$n$$$ operations to your answer (each $$$a_i$$$ has to go up one more now). But we can achieve the same sort of additions to our operations if you look at the $$$0, 2, 4, 6, 8$$$ example, you also add $$$n$$$ when $$$a_{n + 1} = -2$$$.

      So, in summary, find $$$x$$$, then just assign $$$a_{n + 1}$$$ to the first value of the form $$$a_{max} - xn$$$ where $$$1 \le n \le \infty$$$ and you will get your answer.

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

        Why it is never good to put the new value as max+x ? it does give same answer for your example 0,2,4,6,8. Adding 10, gives the answer 15 which is same as adding -2.

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

          If you add $$$a_{\max} + x$$$, you basically change the max to $$$a_{\max} + x$$$, meaning that you have to add $$$x$$$ to the last $$$a_{\max}$$$, and add an additional $$$x$$$ to every element, which is $$$n$$$ total turns. And you can get something equal to or better than adding $$$n$$$ elements by just finding $$$a_{\max} - xn$$$ for the minimum positive integer $$$n$$$ where $$$a_{\max} - xn$$$ is not in the array.

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

Thank for contest! But where is rating?

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

It's amazing to see how much implementation matters in Question D — the solution by Neon is so concise and pretty.

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

awoo, I think there is a typo in the editorial for Problem 1902E - Collapsing Strings. It should be $$$|C(a,b)|=|a|+|b|− \textbf{2} \times |LCP(a′,b)|$$$.

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

F was cute :)

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

For Problem D

How Δi,j is found over there in editorial? Is there a simpler way?

I did this in the below given way

Found the pattern of how reversing changes the path(about what lines the pattern is reflected) and accordingly what distance each point is moving with respect to reflections, there by finding the Δi,j.

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

I feel like tests for F are not really strong or otherwise, I am not sure why my $$$O(n \log n B^2 + q B^2)$$$ solution 235594622 is so fast. I didn't try to optimize anything.

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

LOL, I feel so stupid after looking at the editorial for B. but I am happy to have learnt these "new"(for me) techniques (especially, generating files to find failing testcases, use of ceil() function, proving a greedy approach) and I believe that is one of the main objective of the EDU rounds! I tried to apply greedy approach, was not able to figure out on which cases the code was failing, after like 5 submissions I realized that first ,I should find those days where 2 tasks can be completed like on day 8, 22, 36 etc and add (l+2*t) to points. Then if still points are not enough, add (l+t) to answer if left with any task, and then finally add any remaining points in the form of l. this is my messy solution 235754545. looking forward to better performances in the future.

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

The Trie answer for E is accepted by PyPy3 but not PyPy3-64. :(

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

What is the difference between https://mirror.codeforces.com/contest/1902/submission/235770255 and https://mirror.codeforces.com/contest/1902/submission/235600034 ? Why does it work when I change beginning to 2n*sum(1->n) then subtract and not when i continuously add to a starting val of 0 length*n+sum-subtracted value . of lengths? When I use summation rules the outcome is the same.

(totallength+(sum of all individual length(which is total length)))*n = 2n sum of all length, then I did not change the way I calculated what I need to subtract subtract.

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

My first time solving every problem before editorial))))

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

So, it means that the points that the robot visits can be represented as follows: for an integer k such that l≤k≤r, perform all commands from 1 to r, except for the commands from l to k

Could someone explain this part of the tutorial for D please?

For example if a take k = l, the this means that I'll make all the commands from 1 to l-1 and then from l+1 to r? I don´t get it :'(

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

    It means you can check the path for the commands from 1 to l-1 and then from r to the end.

    Notice that when you flip the instructions from l to r, the path from 1 to l-1 is unaffected (because nothing has changed for those parts).

    Now notice that when you flip the instructions, you are just changing the order of instructions, the instructions that are carried out are still the same (in other words, if there are 5U and 5L before flipping, there will still be 5U and 5L after flipping, the order is just different).

    Let us take the change in coordinates caused by flipped instructions as {dx, dy}.

    Notice that dx = number of r — number of l, and dy = number of u — number of d. Since the number of instructions for each direction remains unchanged, we can conclude that dx and dy remains the same.

    All this means is that before flipping, we start at the same place, and after flipping, we end at the same place as compared to our original path. Since there are no changes to the instructions after flipping, we can also conclude that the path before and after flipping is the same.

    Hence, we can check the original path for 1 to l-1 and r to the end, then check the reversed path (with some simple math) from l to r.

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

      Thank you ArtemiszenN!! Actually it was not the answer I was lookinfor but you made me notice something important to understand what I was asking and that is the instructions remain the same just in other order.

      Literally I couldn't notice that when taking k and process instructions from 1 to l-1 and then from k+1 to r leads to the same coordinate when processing instructions from 1 to l-1 and then the first k instructions in the flipped string.

      Thank you ^-^

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

The solution of problem F in $$$O(n\log^2 V)$$$ is awesome! I just came up with a simple $$$O(n\log^3 V)$$$ solution:

Through the definition of XOR base (mentioned in the official tutorial) we can conclude that for two integer set $$$A$$$ and $$$B$$$, $$$X(A\cup B) = X(X(A)\cup X(B))$$$, where $$$X(S)$$$ means the XOR base of an integer set $$$S$$$. Through, we can merge two XOR base in $$$O(\log^2 V)$$$ time: Simply insert all element of $$$X(A)$$$ and $$$X(B)$$$ into the resulting XOR base.

Therefore, using binary lifting algorithm on tree just like solving LCA, we get an $$$O[(n + q)\log^3 V]$$$ solution.

Usually the $$$O[(n + q)\log^3 V]$$$ complexity with no optimization can't pass all tests (TLE on test [70, 93]). So here's a trick to improve your constant factor: When adding integers into a XOR base $$$B$$$, if $$$B$$$ has exactly $$$20$$$ element, then do noting, because it will not change any element of $$$B$$$. Hope this will help you.

Here is an example: (C++)

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

Can B be solved with just mathematical expressions, without binary search?

I tried finding an expression for minimum number of working days, but it gave me wrong answer. I am unable to figure out what went wrong. Can someone please point out my mistake?

My approach was to find the number of days on which (1 lesson + 2 tasks) can be done, and total number of points that can be gained from such days. Let's call such days as tripleTaskDays.

If the points possible to be gained from tripleTaskDays are less than P, we need extra (1 lesson) days to reach P. Otherwise, if tripleTaskDays are sufficient to obtain P, we can just find the number of such days required to obtain P.

The number of tripleTaskDays will be ((n-8)/14) + 1. Because, on first 8 days we get 2 tasks, and then for every next 14 days we get 2 tasks. The +1 at the end is for the first 8 days. The points that can be obtained from such days is thus (((n-8)/14) + 1)*(l+t+t). Lets call this tripleTaskDaysPoints.

If this is less than P, we need to add extra (1 lesson) days. The remaining points are P-tripleTaskDaysPoints. This can be obtained in (remPoints/l) + (remPoints%l == 0 ? 0 : 1) days with (1 lesson) each.

If this is greater than P, we need (P/(l+t+t)) + ((P%(l+t+t)) == 0 ? 0 : 1) tripleTaskDays to obtain P, and that is the answer.

What is wrong with this approach?

Code

(P.S.: It passed the sample test cases. Failed on 883rd test case, which I couldn't view...)

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

For B i submitted the following code and the verdict i got was wrong answer on test 3, now when i submit the same code it got accepted, wtf is this scam codeforces??

ll n, l, t;
ll P;
cin >> n >> P >> l >> t;
ll tasks = (n % 7 == 0) ? n / 7 : n / 7 + 1;
ll tmp = tasks / 2;
ll rem_task = tasks % 2;
ll days = 0;

long long sum = t * 2 + l;
ll rem = (P % sum == 0) ? P / sum : P / sum + 1;
// cout << rem << " ";
rem = min(tmp, rem);
// cout << rem << " ";
days += rem;
P -= rem * sum;

if (P > 0 && rem_task) {
    P -= l;
    P -= t;
    days++;
}

if (P > 0) {
    if (P % l == 0)
       days += P / l;
    else
       days += P / l + 1;
}

cout << n - days << "\n";
»
2 года назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

Can someone please explain how do we arrive at $$$2*n \sum_{i=1}^{n} Si - 2* \sum_{i=1}^{n} \sum_{i=1}^{n} LCP(Si', Sj)$$$.

I wanna know specifically how do we get $$$2*n \sum_{i=1}^{n} si$$$. Thankyou!

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

Problem C: Can someone please help me out to understand the limitation of using int[] compared to long long[] for storing all the values in an array.

using long long: https://mirror.codeforces.com/contest/1902/submission/235786184

using int: https://mirror.codeforces.com/contest/1902/submission/235786137

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

why does an $$$O(n \ log \ n)$$$ hash based solution for E TLE :/ slow modulo?

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

I have problem that a lgm can't answer,and if someone is able to, everyone will admire him and he will be legend

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

For D, i suppose offline queries ( sorted by r) method would be more easier. Then no need of binary search also. submission

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

My code for problem D is giving tle at test 41. Here is the code https://mirror.codeforces.com/contest/1902/submission/241087945 Can anyone suggest any optimization that can be done here. Thanks

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

I tried to solve problem E using Trie, but it repeatedly gives Memory Limit Exceeded on test case 16. I don't see any mistake in the construction of the Trie and looks like it used O(S*26) memory. Any sort of discussion will help a lot. This is the submission link : https://mirror.codeforces.com/contest/1902/submission/244942580

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

Reverse the operation sequence, and the new path is equivalent to the original path being symmetrical about the midpoint of the starting and ending line.

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

could anyone tell me why i am getting tle on test case 41? my solution is of o(nlogn) {if i am not wrong} . and by seeing constraints it should pass the testcases?. anyone?

sumbission link :- https://mirror.codeforces.com/contest/1902/submission/256209886

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

Sorry for necroposting but I found a slightly different solution to D which I think is interesting.

Solve for the 1D case (ie, only X coordinates). This is easily solved with prefix sums if we consider L as -1 and R as +1.

Well, it turns out you can convert the 2D case to the 1D case by considering U as +2e5 and D as -2e5, because the magnitude is large enough not to interfere with the L/R.

Solution