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

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

Нам очень жаль, что раунд пришлось сделать нерейтинговым. Мы в очередной раз приносим свои извинения и надеемся, что вам понравились задачи!

1497A - Meximization

Идея: shishyando

Разбор
Реализация

1497B - M-arrays

Идея: Artyom123

Разбор
Реализация

1497C1 - k-LCM (easy version)

Идея: shishyando

Разбор
Реализация

1497C2 - k-LCM (hard version)

Идея: isaf27

Разбор
Реализация

1497D - Genius

Идея: shishyando

Разбор
Реализация

1497E1 - Square-Free Division (easy version)

Идея: Artyom123

Разбор
Реализация

1497E2 - Square-free division (hard version)

Идея: isaf27

Разбор
Реализация
Разбор задач Codeforces Round 708 (Div. 2)
  • Проголосовать: нравится
  • +334
  • Проголосовать: не нравится

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

SpeedForces

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

Point distribution was weird imo but Questions were really good tho.

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

For C2 — legend+ary

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

Problem A — Implementation based

Problem B — Implementation | Constructive (Great Problem)

Problem C1,C2 — Intuitive | Constructive (Make Cases and you are done with both problems)

Problem E1 — Simply Math | Implementation (Prime factorization)

A great Round shishyando and Artyom123 with great and interesting problems. Hope to see your next round soon !!

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

E2 and D are interesting problems overall.

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

    I think so. I am not able solve the problems, but reading the resulotion helps me a lot in understanding usage of DP. Love this round so much!

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

Really liked the problems.

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

Though I'm a tester, I want to say that C1 is the very beautiful hint for C2! Without C1, C2 can be the hardest problem in the contest.

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

Can someone explain in detail math behind C1?

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

    It was simple first if n is odd then just print 1 n/2 n/2

    if n is even then check for if n/2 is even then print n/2 n/4 n/4 if n/2 is odd then print (n/2)-1 (n/2)-1 2

    we can extend this same logic in C2 by simply printing 1 , (k-3) times then taking n=n-(k-3) in C1

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

    n can be of the form 4*k , 4*k+1 , 4*k+2 , 4*k+3 (for some k>=0) case 1: n = 4*k ans = k ,k, 2*k LCM = 2*k

    case 2: n = 4*k+1 ans = 1 ,2k ,2k LCM = 2k

    case 3: n= 4*k+2 ans = 2 , 2k , 2k LCM = 2k

    case 4: n = 4k+3 ans = 1 , 2k+1 , 2k+1 LCM = 2k+1

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

Great Round by shishyando and Artyom123

Very sad that it got unrated , as it was first ever contest I participated, where 4 question were solvable by any programmer who have knowledge of basic math and preparing frequency array

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

I went straight to C(hard) without looking at C(easy) since I didn't want to be misled by some simple solution that only worked for simple testcases but not hard ones... instead I fumbled around for an hour looking for a general solution without realizing the k=3 case is all you need. That really backfired on me.

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

    The scoring distribution: 500 — 750 — (750 + 500) — 1750 — (1500 + 1500) It was pretty much clear, C1 was supposed to be harder

»
3 года назад, # |
Rev. 4   Проголосовать: нравится +22 Проголосовать: не нравится

At the risk of my submission soon being systested, I think E2 can be solved greedily.

Construct the greedy intervals from E1. Then, at most K times, consider all pairs of adjacent intervals. Compute the number of changes needed to merge all pairs of adjacent intervals in $$$O(n)$$$ and then just merge the best one, and update the array so that excess is put at impossible values.

It works because in the solution you will never remove a value completely from an interval. So taking suboptimal merger will never help since it isn't actually going to reduce a merging cost.

Since you always make at least one change, you repeat at most $$$k$$$ times so it is $$$O(nk)$$$. I think it can be optimised to $$$O(n)$$$.

UPD: I think it cannot be optimised to $$$O(n)$$$, sorry. I thought that maybe you could save computation by only calculating the effect of the merger rather than recalculating every cost, but that's still $$$O(n)$$$.

UPD2: WA47, I wonder if greedy is just wrong here or it's a problem with the code...

UPD3: Assuming that you are merging intervals, the solution is optimal. However, it is also possible to split up an interval, so it does not work.

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

    Can you explain a bit more in detail about splitting intervals? I had the same reasoning and (theoretical) solution as you as well as WA47 and would like to know what the logical error is.

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

      Say there are just three intervals originally. The greedy solution wants to subsume the middle interval into either the left or the right interval. However, it is also possible to subsume the left part of the middle interval into the left interval and the right part of the middle interval into the right interval. It is possible that this is less costly.

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

    what do you mean by best one? does it mean that bigger the interval, the better it is? or anything else? btw approach is nice. Thank you !!

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

Thank you for the problems!

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

even despite unrated, this was the best round ive taken in a while. clean and concise problem statements. thank you so much for the round. :D

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

Checkout this problem for a variant on problem A.

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

What are "constructive" problems? can anyone tell. how to become good at these?

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

"...it is easy to see that..."

Writing that sentence is like begging for downvotes. Remember, editorials are written for those not able to solve the problem. Telling them how easy it is is no good idea.

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

From problem D: "Then in binary form weight has its k-th bit set true if and only if j < k < i" Shouldn't it be j <= k < i ? Because we have for example 10000 — 00100 = 01100

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

From problem D: "Then in binary form weight has its k-th bit set true if and only if j < k < i" Shouldn't it be j <= k < i ? Because we have for example 10000 — 00100 = 01100

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

Seriously C2 was nice af!!

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

Thanks for the round, although I couldn't participate.

Problem D is beautiful.

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

Is it ok that i got TL in E2 with O(nk log n + max(a_i)) complexity ?

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

    Is your max(a_i) per test case, or in total? If it's per test case, then it would be O(nklogn+max(a_i)t) which is too much

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

      it's in total, I need it to precalculate prime numbers which are less than 1e7, I passed E1 that way.

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

Help needed 110230087 for problem 1497B - M-arrays

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

For me C2 strikes so fast even I hadn't thought of it...it just dropped from the sky :)

Simply, considering 1 1 1 1 ... upto k-3 and the C2 becomes C1

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

    I think if there was no C1 then people may have a hard time coming to this solution for C2.

»
3 года назад, # |
  Проголосовать: нравится +1 Проголосовать: не нравится
»
3 года назад, # |
  Проголосовать: нравится 0 Проголосовать: не нравится

What is the reason for the unusual memory limit in Problem D?

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

    I'm assuming the reason was to prevent the more obvious solution with a dp[n][n] table, which would need to store up to 25,000,000 ints = 100 megabytes.

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

Why it is necessary to use map instead of unordered_map in 1497B - M-arrays? I submitted both versions but only map got AC but unordered_map one got WA on test 3. map version-> 110262091 unordered_map version-> 110262159

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

    Using cnt[i] may create a new element (cnt[i]=0) in map. And it may cause rehashing of unordered_map, iterators are invalidated (see "Iterator invalidation" here), and for-loop continues working with invalid iterators, which causes undefined behaviour. In usual map creating new element does not invalidate iterators, so UB won't happen. But u still got lucky that these extra zeroes in your map don't change result of your program.

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

I really like how C1 is a hint for C2.

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

C1 750pts

C2 500pts

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

Problem E is similar to this problem.

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

If Only I submitted C1 after My C2 was accepted!

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

i am curious while doing C1,C2 problem how people were able to come up with the idea of for n%2 == 0 ans is n/2,n/2,2 and for n%4 == 0 ans is n/2-1,n/2-1,2 and so on.During the contest I made a pattern for 1 to 11 but i was unable to intutively guess this solution .Can anyone recommend the problems that i can do for increasing my intution for doing this type of problems thanks

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

    sorry for late response.I made a patter up to 23 before getting the right answer. My cases was if n is a multiple of 3 we done (n / 3, n / 3, n / 3), and it was difficult after that. I sais to myself it the limit is n / 2 i will try to be near to n / 2. and for each number i put n / 2 and see what can be done. I think the best is to go up to a limit (say 50) and if you don't see a pattern maybe it's not the right solution.

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

There is an $$$O(nk)$$$ dp solution in Problme E2.

At first, we can let $$$a[i]=mask(a[i])$$$. Assume $$$dp[i][j]$$$ denotes we consider the first $$$i$$$ positions, after making $$$j$$$ changes, the minimum answer we can get and the maximum position where the begining of the last segment at. So we can just maintain $$$last[i]$$$ which denotes the maximum position $$$j$$$ where $$$a_j=a_i$$$ (expecially, if $$$a_i=0,last[i]=i-1$$$) and make transfers.

The key observation in the solution is that if we change the number $$$a_i=x$$$, and for the first $$$j>i,a_j=x$$$, we should let $$$last[j]=last[i]$$$. But actually, there is no need to do that, we can still get the right answer.

See the code for details.

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

Can anyone tell me how to reach to the solution for $$$C2$$$. I am asking this because $$$C1$$$ made a path for $$$C2$$$. So, can someone give some mathematical proof or logical way to approach such problem

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

    Achieving lcm of n / 2 lead intuitively (to me) to take n / 2 everywhere. now we can see that we can put n / 2 at most 2 time and i will remain a 1 case left.(that leads to k = 3 case). you can also think i put the same number some number of time and i put 1 everywhere since 1 does not increase the lcm. But i think it's very difficult to figure out alone without a hint.

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

for the tutorial for c2,

(1+1+...+1)+(a+b+c)=(k−3)+(n−k−3)=n.

shouldn't it be (k-3) + n-(k-3) or (k-3) + n — k + 3?

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

Here is another $$$\mathcal{O}(nk)$$$ solution to 1497E2 - Square-free division (hard version).

For the first part, after normalization, instead of left[i][j] which is a bit bothering, we only need to find pre[i], which is the rightmost index to the left of the current index that shares the same value as the current index. This is very simple.

Then for the second part, for each change number j, we store best[j] denoting the minimal pair (segs,-last_start), and second_best[j] denoting the second minimal pair (segs,-last_start). Here last_start is the start index of the last open segment, and segs is the number of segments.

During the DP, if the last open segment can contain the new element, everything remains the same. Otherwise, we choose either to start a new segment (so that j does not change), or to change the current element (so that j becomes j+1).

Note that there would be cases that second_best becomes better than best, in which they should be swapped.

Code: 110279809

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

For C1 wasn't there suppose to be a case when n % 3 == 0.The answear would be then n / 3 , n / 3, n / 3?

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

    You could analyze that, but that is not needed. The solution explained in the editorial says if n is odd you do A and if n is even you do B, no need to add more cases. This is a typical bad practice from beginners.

    If n is a multiple of 13 you could answer 6*n/13, 6*n/13, n/13. But it would be silly to add such a case

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

Now let's relax dp values. When we consider an edge {i,j},tagi≠tagj we try to solve problem i after solving dpj problems ending with j, and problem i after solving dpi problems ending with i. It means that dpi=max(dpi,dpj+p), dpj=max(dpj,dpi+p) at the same time, where p=|si−sj|.

Can anyone explain this part of the editorial of problem D?

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

восемь манулов

»
3 года назад, # |
  Проголосовать: нравится -7 Проголосовать: не нравится

Guys I'm really sorry I know posting your code and asking why it's giving me an error is a sure-fire way to get downvoted but I really have no choice, I have commented neatly and explained the code for problem D, but I keep getting a run-time error on TC-2, can anyone please help me out

Here is the code : Python 3

Thank you in advance :)

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

    it's because in your code first of all you didn't memoized all the states and 2ndly recursion tree for your code does not form a DAG(because yours is cyclic) which is necessary condition for any dp/recursion problem. In simpler words you end up calling a state which initially made you call these states.

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

For a case where n = 3z ,z being an odd natural number, isn't the answear (z, z ,z), not (1, n / 2, n/ 2) as it is specified in the editorial ?

»
3 года назад, # |
  Проголосовать: нравится -8 Проголосовать: не нравится

Guys is there an algorithm involved in B problem. Or is it an only math based problem? What should be said ?

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

Hello Everyone, Can some one tell me, where I am getting wrong in problem E2? I am getting WA on 8th testcase.
My solution.
Thank you in advance !!

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

Can someone provide better explanation for D and E2?

»
3 года назад, # |
  Проголосовать: нравится -15 Проголосовать: не нравится

C1, C2 sucks :((

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

Omg. I was amazed when I read the tutorial for Problem C2. I accepted C1. After that, I tried to think and find solution for C2 for 1 hour before I gave up. That's amazing. That's unbelievable. That's incredible.

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

Can someone please help me understand why this solution for E2 works on all tests except 47?