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

Автор shorya1835, история, 12 месяцев назад, По-английски

2140A - Shift Sort

Idea: shorya1835 Preparation: wakanda-forever

Solution
Code

2140B - Another Divisibility Problem

Idea: wakanda-forever Preparation: wakanda-forever

Solution
Code

2140C - Ultimate Value

Idea: shorya1835 Preparation: wakanda-forever

Solution
Code

2140D - A Cruel Segment's Thesis

Idea: shorya1835 Preparation: shorya1835

Solution
Code

2140E1 - Prime Gaming (Easy Version)

Idea: Divine_Spark Preparation: shorya1835

Solution
Code

2140E2 - Prime Gaming (Hard Version)

Idea: Divine_Spark Preparation:wakanda-forever, shorya1835

Solution
Code
Bonus

2140F - Sum Minimisation

Idea: shorya1835 Preparation: wakanda-forever, shorya1835

Solution
Code

Thanks to satyam343 for his help throughout the round.

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

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

noooo I want yesterday's rating please this one killed me:(

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

A was very tricky for me to prove that such strategy gives minimum moves.

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

For the case 110100, we can take i = 1, j = 3, k = 4 and the string will become 111000 which is sorted but the answer given in test case is 2 steps

»
12 месяцев назад, скрыть # |
 
Проголосовать: нравится +10 Проголосовать: не нравится
E2 bonus
»
12 месяцев назад, скрыть # |
 
Проголосовать: нравится +1 Проголосовать: не нравится

Got clapped but round was nice, thanks !

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

Hey can anyone tell me what is wrong in my approach. And please while pointing out my mistake, tell me how to think in the way to get to the approach mentioned in the editorial. It would be of great help to me. 337853452

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

What's case 1 in editorial for F? Doesn't [3, 0, 1] fall into case 1, but allow only 1 single operation?

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

337862621

any clue why my code keeps wrong? give me hint plz

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

    If you look at the failing 26th test case, it says:

    3 2 2 1

    You can see where your logic has fault. Hint: If you do mx-mn then you are also doing index(mx) — index(mn). This will be correct when mx lies to the right of mn, but that's not the case here.

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

I solved B by observing the pattern and got y = 10^9 - 1 - x always works, can anyone prove this

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

    Yes! let we have chosen y ...d=(length of y****)+1,,and mod=x+y. Then (x#y) = x*(10^d)+y.Then x#y is divisable by x+y;it will hold iff (x*(10^d)+y)%mod=0 i.e (x*(10^d)%mod+y%mod=0 as mod>x then y%mod=y so (x*(10^d))%mod must be x to hold the criteria of (x#y)%mod=0; we already know mod>x and mod>y; so if we can make 10^d %mod==1 we can fillup the criteria....so it will become 1 when mid=10^d-1 i.e x+y=10^d-1;.....thus it is ovbious for every d<i<=9; max value of d=9,,,,we can choose choose y=999999999-x everytime and it will pass every testcase!**____**

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

2140B - Another Divisibility Problem can be solved with other values of $$$y$$$ as well, for example $$$y = 999... - x$$$ depending on $$$x$$$. Because,

$$$x \text{#} y = x \cdot 10^d + y = x \cdot (10^d − 1) + (x + y)$$$

So, to make the $$$10^d - 1$$$, which is basically $$$9999...$$$ for $$$d$$$ times, to be divisible by $$$x + y$$$, we can make $$$x + y$$$ itself be of this shape. For $$$x$$$ to be any $$$k$$$-digit number, $$$x + y$$$ can be of shape with any $$$k$$$ or more digits of $$$9$$$ s. Since the constraints state that $$$x \lt 10^8$$$ and $$$y \lt 10^9$$$, we can set $$$x + y$$$ to be $$$999,999,999$$$ i.e. $$$y = 10^9 - 1 - x$$$.

Also, obviously, $$$y = 8 \cdot x$$$ works for the same reason as tutorial.

Upd: can anybody explain why the downvotes? Cause I'm pretty sure there's nothing wrong in terms of logic cause I literally got AC with this approach. 337798106

»
12 месяцев назад, скрыть # |
Rev. 2  
Проголосовать: нравится +11 Проголосовать: не нравится
E2 Bonus Solution:
Spoiler
»
12 месяцев назад, скрыть # |
Rev. 2  
Проголосовать: нравится +3 Проголосовать: не нравится
another way to look at D
»
12 месяцев назад, скрыть # |
 
Проголосовать: нравится +17 Проголосовать: не нравится

My solution to Problem D works as follows: the original problem is equivalent to finding a matching among the intervals $$$1, \cdots, n$$$ that maximizes the value

$$$ \displaystyle\sum_{(j,k) \text{ are matched}} \max(r_j - l_k, r_k - l_j). $$$

Using the identity $$$\max(A, B) = \frac{A + B}{2} + \frac{|A - B|}{2}$$$, this expression can be rewritten as

$$$ \displaystyle\sum_{(j,k)} \max(r_j - l_k, r_k - l_j) = \sum_{(j,k)} \frac{(r_j - l_j) + (r_k - l_k)}{2} + \sum_{(j,k)} \frac{|(r_j + l_j) - (r_k + l_k)|}{2} \quad (\star). $$$

For even $$$n$$$, the first term is constant (equal to half the total sum of the lengths of the intervals), and the second term is maximized by partitioning the array $$$a[j] = r_j + l_j$$$ into two halves and subtracting the sum of the smaller half from the sum of the larger half. This can be computed efficiently using prefix sums in $$$\mathcal{O}(1)$$$ time per query after preprocessing.

For odd $$$n$$$, one interval (say $$$i$$$) remains unmatched. We enumerate each $$$i = 1, \cdots, n$$$ as the unmatched interval, and the value in equation $$$(\star)$$$ can be easily evaluated in $$$\mathcal{O}(1)$$$ time using a similar prefix sum approach with minor modifications, resulting to an $$$\mathcal{O}(n)$$$ solution.

This method is basically identical to the one in the editorial, but more clear to me.

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

the second problem was a humbling experience for me

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

can someone explain this part This can be solved by maintain prefix minimum over odd and even indexes.

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

for problem E, dp[i][mask][1] is actually the same as dp[i][~mask][0]; you can just maintain one

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

For problem C, I used a DP approach. Surprisingly, when I implemented it with a regular array (initialized using memset), it resulted in TLE. However, switching to a map for memoization led to an AC. Could someone explain why this happens?

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

    You initialize the whole DP table with $$$8 \cdot 10^5$$$ elements for every test case. Since there are $$$10^4$$$ test cases at most, you're overwriting $$$8 \cdot 10^9$$$ elements in sum, which is too slow.

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

      Oh, I see now. This means my array-based solution ends up doing about $$$2 \cdot 10^5$$$ operations for each test case, which ignores the fact that "the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$". In contrast, the map-based solution only stores and processes the states that are actually needed, so its complexity is closer to $$$O(n)$$$ overall. Is that correct?

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

    Hello Osama, i want to show you a trick you can use when you come across dp problems that involve test cases, as someone previously replied clearing your dp each case is too slow, but also the map memo isn't the best practice when it comes to this sort of problems.

    There are two main ways to to this better, first is to give each test case an id, and to distinguish if you have been in this state before in the same test case you check if vis[state] equals the current id. 338980284

    Another way is to clear exactly the size you need for this test case, after taking input n, loop and set it to the value you want.

    338981249

    Hope this helps.

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

      Thanks a lot, Adham — that was really helpful. I like how these approaches let us stick with arrays instead of maps and still avoid TLE. There’s just one thing I’m not fully clear on: in the first solution, the vis array isn’t initialized. Is it safe to compare its values like that? If not, then initializing it once like int vis[(int)(2e5 + 5)][2][2]{}; should be enough, right?

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

where balanced paranthesis come from in d editorial??? (*cry emoji)

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

How can u directly find out left out segment in O(1) in D editorial ??

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

Oh WOW, that's a fast editorial.

Loved the problems, and I FINALLY became a specialist.

I got the idea of C, but for those who didn't, I think the change in the statement of C gave it away. B was a little hard tho

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

2140B extended The editorial was prepared together with my coach, after he tried during the contest to brute-force all divisors of $$$10^k-1$$$ and $$$x$$$ and ran into TLE. Submission.

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

What is wrong in this solution for D:

First we initially compute the sum of all segments , Initial_sum = $$$S = \sum_{i=1}^{n} y_i - x_i$$$ then we want to compute the additional segments length formed by marking these segments, if we sort the $$$l_i$$$'s and $$$r_i$$$'s and then take the sum of (highest $$$r_i$$$ — least $$$l_i$$$) + (2nd highest $$$r_i$$$ — 2nd least $$$l_i$$$) + ..... and add this sum to the Initial_sum

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

    You probably mean the highest r_i — least l_j for distinct i and j. The problem arises in the case when a segment with the largest r_i contains all other segments. In that case we have a choice between r_i of largest segment — second highest l_i and second highest r_i — l_i of largest segment.

    Consider these segments: [1, 100], [5, 90], [98, 99]. You select the first two, but the optimal way is to take the first and the third segments.

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

Can someone please explain the solution of E2 after calculating the dp values. How is it calculating the original answer from binary(0, 1)?

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

    For a configuration where alice wins in easy version with the bits that are 1 here are values that are >= k (for some k we are looping through) and other(0) values are < k then Alice can Force Bob into making the final Answer >=k as it forced Bob into making the final value 1 for the corresponding 0-1 mask. so we just need to count for every k the sum of masks * no of ways to generate this mask (1 filled by >=k value and 0 filled by <k values)

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

      (k−1)^n−r⋅(m−k+1)^r⋅cnt[r]

      isn't this line just the number of ways to create a mask with (n-r) places with < k and r places with >=k, cnt[r] times. So then how how are we accounting for the sum of the actual values left at last? What am i missing here?

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

For problem D, a very "neat" way of reaching the greedy conclusion is by expanding the formula of the chosen segments' sum. Let's name the of chosen segments $$$C$$$ and the rest be $$$R$$$.

Our formula is :

$$$ \sum_{i \in C} r_i - \sum_{j \in R} l_j $$$

If we manage to eliminate contribution of group $$$R$$$ from this equation it will be easy to select group $$$C$$$ to maximize this expression. To do so we can expand the second term, based on this substitution :

$$$\sum_{j \in R} l_j = \sum_{i=1}^n l_i - \sum_{k \in C} l_k $$$

Where $$$n$$$ is the input size, by substitution our formula becomes :

$$$ \sum_{i \in C} r_i - (\sum_{i=1}^n l_i - \sum_{i \in C} l_i )$$$
$$$ \sum_{i \in C} r_i + \sum_{i \in C} l_i - \sum_{i=1}^n l_i $$$
$$$ \sum_{i \in C} (r_i + l_i ) - \sum_{i=1}^n l_i $$$

Now it's clear to see that maximizing this expression is equivalent to maximizing sum of right and left of a segment, since summation of all lefts is constant.

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

"In Problem D editorial it says you can directly find the left out segment in O(1) — can someone explain how to actually do that part?"

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

Here's a brainf*ck solution of 2140B - Another Divisibility Problem for no reason

(Assuming all cells are 32-bit instead of 8-bit as normal, if someone can implement an 8-bit solution I'm more than excited to see it)

[-]>[-]+[[-]>[-],[+[-----------[>[-]++++++[<------>-]<--<<[->>++++++++++<<]>>[-<<+>>]<+>]]]<]<
// Inputs the number of testcases
[>
[-]>[-]+[[-]>[-],[+[-----------[>[-]++++++[<------>-]<--<<[->>++++++++++<<]>>[-<<+>>]<+>]]]<]<
// Inputs n
[->++<]>
// Calculate 2*n
>[-]>[-]+>[-]+<[>[-<-<<[->+>+<<]>[-<+>]>>]++++++++++>[-]+>[-]>[-]>[-]<<<<<
[->-[>+>>]>[[-<+>]+>+>>]<<<<<]>>-[-<<+>>]<[-]++++++++[-<++++++>]>>[-<<+>>]<<]
<[.[-]<]<
// Output 2*n
[-]++++++++++.[-]
// Output newline character
<<-
// Decrement testcase number
]
»
12 месяцев назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

I don't understand the complexity in E1, isn't is supposed to be $$$O(n^2 \cdot 2^n)$$$, because of the for loop over "good" vector?

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

Similar problem for B from Project Euler. Editorial

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

In the editorial for problem D, I dont understand:

this part of editorial

My question being

the question

Thanks a lot in advance!

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

    I am not sure, what your question is, can you elaborate?

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

      I understand that part of the editorial

      as follows.

      Sorry if I misunderstood the editorial. And please excuse my English as well.

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

For problem F, is y taken to be the absolute value of x — floor(x/k)*k? I'm not sure if it's ever possible to get infinitely decreasing sum otherwise.

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

I think there's a slight logical error in Case 2 in the editorial for F. The editorial asserts that if any operation can be performed on the last n-1 elements of the array, then performing that operation will take us to Case 1. This is not true when the operation only modifies one element and that element is equal to the minimum of the array. For example, consider the array [1, 1, 3, 3]. We can perform an operation on the three larger elements, which makes the array [0, 1, 3, 3], which does not fall under Case 1.

The reason the model solution still works is that if this happens, then we can perform the same operation again, replacing the element that was decreased with the original minimum element of the array. At this point, we have two occurrences of the original minimum — 1, so we have an element other than $$$a_1$$$ with the opposite parity of the elements of the original array, putting us into Case 1.

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

I don't understand this :

for (int r = 1; r <= n; r++)
{   
    for(int k=1;k<=m;k++){
        finans += (((binpow(k-1,n-r)*binpow(m-k+1,r))%MOD) * (cnt[pl][n][r]))%MOD;
    }
}
cout << finans%MOD << endl;

Why is the result calculated this way?

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

what is wrong with this approach for C-

import sys
input = sys.stdin.readline
MOD = 1_000_000_007

def ans():
  n = int(input())
  l = list(map(int, input().split()))
  s = x = y = 0
  m = 1_000_000_007
  ma = -1_000_000_007

  for j in range(n):
    if j % 2 == 0:
      s += l[j]
      if (2*l[j] + j) < m:
        m, y = (2*l[j] + j), j
    else:
      s -= l[j]
      if (2*l[j] + j) > ma:
        ma, x = (2*l[j] + j), j

  xa = ya = 0
  mr = 1_000_000_007
  maa = -1_000_000_007

  for j in range(1, n+1):
    if (n - j) % 2 == 0:
      if (2*l[-j] + j - 1) < mr:
        mr, ya = (2*l[-j] + j - 1), n - j
    else:
      if (2*l[-j] + j - 1) > maa:
        maa, xa = (2*l[-j] + j - 1), n - j

  u1 = ma - m
  u2 = maa - mr  
  u = max(u1, u2)
  return s + max(u, (n - 1 - (n + 1) % 2))

for _ in range(int(input())):
  print(ans())
»
2 месяца назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

C is a nice problem. I almost solved it, but didn't complete the code.