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

Автор simplelife, 3 недели назад, По-английски

Thank you for participating in our round. We apologise for C2 being too hard compared to C1.

UPD : Added an alternative solution to B, which contains the original solution with a diagram for better understanding.

2210A - A Simple Sequence

Idea: ritam1234, Preparation: ritam1234

Solution

2210B - Simply Sitting on Chairs

Idea: ritam1234, Preparation: ritam1234, Argentum47

Hint 1
Solution

You can refer to the following for a better idea of the exchange argument, credits dipamsen

Solution-2

2210C1 - A Simple GCD Problem (Easy Version)

Idea: simplelife, Preparation: Argentum47

Hint 1
Hint 2
Hint 3
Solution

2210C2 - A Simple GCD Problem (Hard Version)

Idea: simplelife, Preparation: Argentum47

Thanks to Intellegent for misreading $$$C1$$$ and Argentum47 for fully solving it.

Hint 1
Hint 2
Hint 3
Hint 4
Hint 5
Hint 6
Hint 7
Hint 8
Solution
Additional Challenge
Code(C++)

2210D - A Simple RBS Problem

Idea: simplelife, Preparation: simplelife

Thanks to awesomeguy856 for helping me in writing the editorial for this problem.

Hint 1
Hint 2
Solution
Code(C++)

2210E - Binary Strings are Simple?

Idea: Argentum47, simplelife, Preparation: ritam1234, __baozii__, Argentum47

Thanks to wuhudsm for buffing this problem.

Part 1 Hint
Part 1 Solution
Part 2 Hint 1
Part 2 Hint 2
Part 2 Solution
Code 1(C++)
Code 2(C++)

2210F - A Simple Problem

Idea: simplelife, Preparation: __baozii__

Thanks to __baozii__ for solving this problem.

For the simplicity of editorial , denote $$$\max(b_1,b_2,\ldots,b_i)$$$ as prefix-max($$$i$$$) and $$$\min(b_1,b_2,\ldots,b_i)$$$ as prefix-min($$$i$$$).

Hint 1
Hint 2
Solution
Code ( __baozii__,C++)
Code(Dominater069,C++)
Разбор задач Codeforces Round 1089 (Div. 2)
  • Проголосовать: нравится
  • +96
  • Проголосовать: не нравится

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

Quick Editorial, Thanks!

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

C2 has too big of a jump in difficulty

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

ok wow, thanks for superfast editorial

I had hard time in this contest because of C2 and D ...

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

for the whole 1hr i thought of a dp solution of the problem B, i even got the recurrence relation for that but that would have taken O(N ^ 2), later I realized it was just greedy after 2hrs :(

btw C1 was easier than B, at least for my dumb-ass

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

    In B, you can actually calculate points "if ends at chair $$$k$$$" for every $$$k$$$. (And don't need to prove greedy)

    The biggest problem to calculate count of $$$p_i \gt k$$$, where $$$i \lt k$$$. It can possible with idea like difference array. (submission: 368708366)

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

    In B I have a very simple solution: for every i from 1 to n,once we arrive at a chair,we can immediately sit on it and mark a[i] and let cnt+=1,we can use an array like int mark[n+1]; to mark it

    than if the chair was marked before,we regret what we did before,that is,we assume we didn't sit on the chair that marked chair i,but we don't need to know which chair marked chair i,we just need to cnt-=1 and go to i+1, so the code will like this:

    int n;cin >> n;
    vector<int> a(n+1);for(int i=1;i<=n;i++) cin >> a[i];
    vector<int> mark(n+1);
    int cnt=0;
    for(int i=1;i<=n;i++){
        if(mark[i]) cnt--;
        mark[a[i]]=1;cnt++;
    }
    cout << cnt << endl;
    

    you can notice that cnt won't decrease though the progress,so the correctness is trivial

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

The gap between C1 and C2 ↓

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

Good contest

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

wtf!! I submitted C2 13 sec before the end of the contest and after 13 sec it shows you can not submit now..

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

1 is not prime a number

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

    It is just there in the prime array to handle the special case where the value is unchanged, basically for ease of implementation

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

      Argentum47 Why not using primes also gets me accepted ... Submission : 368781155

      check the part where I am making the poss (possible) choices by just brute force checking some no.s from p = 1 to b[i]

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

        I think what you are doing kind of collecting a small "valid" set of numbers and running dp on them. I think it works since the valid set of number in itself will contain a lot of prime numbers and their multiples, which cover all the cases.

        There might be a deeper reason behind why it works, but it kind of involves the solution/observation for additional challenge part, which I kind of want others to figure out on their own.

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

Hydrogen Bomb vs Dying Ant ahhh c1-c2. Regardless, very cool problems.

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

Congrats to the authors for a flawless contest o/ I believe C2 being a bit harder than expected is not that much of an issue ? Like, there was still C1 before. Also, D is a very cute problem.

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

C2 is a L problem btw

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

I think the tests for C2 are weak. My submission got AC but it's wrong: https://mirror.codeforces.com/contest/2210/submission/368766437

For this test case:

1

4

1 12 2 2

1 1 6 1

My code outputs 1 which is wrong (correct answer is 0).

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

Very Good Contest by the authors. C2 and D could have been swapped in order because of the difficulty of C2.

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

how to check whether the contest have hacking phase or not ?

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

C2 was great!

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

For the additional challenge on C2, my code should run in $$$O(nL\log A)$$$ (and I think it's trivially optimizable to $$$O(n(L + \log A))$$$ if needed) which suffices to pass with $$$n = 10^6$$$; it passes the current tests in 93ms.

368744412

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

For C2 my solution should work in O(n * L * log(max(a))) which is sufficient enough to pass the additional challenge. I considered L = 26 but that can be reduced.

Link :- https://mirror.codeforces.com/contest/2210/submission/368751072

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

There is no button to rate the problem but B is a really good one! I think this idea could have been a foundation of a much more difficult problem, if the setting was a bit different. Nevertheless, I've learned some new idea (exchange principle to prove the function is optimal at the border point), so I'm happy!

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

    Exchange argument are you saying ??

    Or something new ?? would you mind sharing ?

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

      The smart solution is saying that the game is ending at index i, therefore in the prefix of 1 to i-1 you can only take chairs when p[j] <= j || p[j] >= i, 1 <=j < i (notice this solves all array p and not only a permutation)

      but we can actually show that just counting how many j there are in the whole array where p[j] <= j is enough for a perm

      why? because let's look some p[j] >= i, because p is a perm there exists a k where p[k1] = j, if k1>= i we stop, otherwise we continue it back with k1 to get k2... now we got kt, kt-1 such that kt-1 <i && kt >= i, notice k0 = j.

      therefore we can swap j with this kt cause p[kt] = kt-1 < kt.

      notice because p is a perm the functional graph it's described is a line and circle, so we get that this is a one to one matching

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

B editorial:

The maximum number of chairs which you can visit if your game ends at chair x is given by |i:1≤i<k,pi≤i or pi≥k|. I believe it should be "chair k" instead of "chair x"

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

in c2, how does one get the intuition for considering only 20 primes? Is this an advanced technique?

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

    Not an advanced thing Like maximum distinct primes that a number less than equal to 10^9 can have is 9 you can test it by multipliying 2 * 3 * 4 *.....23 and this will be less than 10^9

    so for k1 = a[i-1] / x we have 9

    k2 = a[i+1] / y we have 9.

    so total 18 distinct primes now if suppose all 18 are unusable meaning it divides k1 or k2, then you need 2 more distinct primes so 20 is the number you arrive on. Although I tested some codes and for passing the question you don't need 20. Even 16 is working, Possibly because you never reach theoritical worst case as it depends on gcd and its neighbours Also the testcases are weak in this question as pointed above by a user so that can also be a reason. This is more of a observation thing rather than some advance technique or proof.

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

    from c1 you get that there is this annoying points where you need to multiply them but you cannot ruin the gcd, so ofcourse you choose prime, now you start to think when does multiplying by p is bad, and the answer is if the previous or the next number have p as their divisor more than you. but one can only have small number of prime divisors and that the intuition

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

      Yeah I understood that logic(thanks for your comment, though).

      However the realization that you only need to consider 20 primes for the dp is what I was struggling with. the commenter above has given good reasoning but it seems to me that to even think of doing such calculations (that product of nine primes exceeds 10^9) requires some experience

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

a moment of silence for anyone with gcdmode in their C2 solution...

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

I had a feeling C2 had something to do with primes, but it made too little sense as if I was "skipping" over a lot of possible solutions. The first three were good, but the gap between C1 and C2 and then the later ones was too much in my opinion, maybe a harder C1 could've made it more even? In any case, good job to the problemsetters and contest organizers for another good contest and fast editorial.

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

Let me rephrase the solution of B. Hopefully this is easier to understand.

It's clear that you can always sit the chair $$$i$$$ if $$$i \ge p_i$$$. In other words, sitting the chair $$$i$$$ makes some other chairs unavailable if $$$i \lt p_i$$$.

Let's think of a directed graph $$$G = (V, E)$$$, where $$$V = \{ 1, 2, \ldots, n \}$$$ and $$$E = \{ (i, p_i) \; | \; i \in V, i \lt p_i \}$$$. Since $$$p$$$ is a permutation, every vertex of $$$G$$$ has at most one indegree and at most one outdegree. Thus, the graph $$$G$$$ is split to several components, where each component has the following form:

$$$\begin{align*} i_1 \to i_2 \to \cdots \to i_c \end{align*}$$$

where $$$c$$$ is the size of the component $$$(c \ge 1)$$$.

Out of the chairs in one component, you can sit only one chair (let's call it $$$i_k$$$), because:

  • Sitting $$$i_k$$$ makes all the following chairs $$$i_{k+1}, i_{k+2}, \ldots, i_c$$$ unavailable.
  • Sitting any of the preceding chairs $$$i_1, i_2, \ldots, i_{k-1}$$$ would make you unable to sit $$$i_k$$$, so you can't sit any of the preceding chairs either.

For each component, it's always advantageous to pick the last chair in the sequence $$$i_c$$$, because that's the only chair that won't make other chairs unavailable.

Therefore, the answer to the problem is the number of components, which is equal to the number of chairs satisfying $$$i \ge p_i$$$.

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

An interesting solution for C2 using a cap of 2 primes , observed that if anyone has 3 different prime factors then it will always be used so did a dp over the indexes that had less than 3 prime factors 368804443

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

One can also approach B by considering the visiting order from right to left, and seeing whether it makes sense to sit on this chair or not.

It always makes sense to sit on the last chair. Now sitting on some ith chair would disqualify you from sitting on every chair after and including p[i], including the last one. So the number of chairs sat on you gain by sitting on the ith chair is offset by the last chair that you lose. So it only makes sense to sit on the ith chair where p[i] <= i.

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

okay maybe this wasn't my first pupil contest but hey im a pupil now :D

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

Why did I get TLE when following the solution approach for Problem C2???

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

whats the proof of C1, like we can change a(i-1), a(i+1) and if we change ai to LCM(gi,gi+1), why the gcd of new a(i-1) and LCM is still g1 and new(ai+1) and LCM is still g2 ??

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

    try prime factorizing and see

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

    Given 4 numbers $$$a_1, a_2, a_3, a_4$$$.
    Let $$$g_1 = \gcd(a_1, a_2)$$$, $$$g_2 = \gcd(a_2, a_3)$$$, and $$$g_3 = \gcd(a_3, a_4)$$$.
    If we set $$$a_2^\prime = \text{lcm}(g_1, g_2)$$$ and $$$a_3^\prime = \text{lcm}(g_2, g_3)$$$, we need to prove that $$$\gcd(a_2^\prime, a_3^\prime) = g_2$$$.

    $$$g_2 \mid \text{lcm}(g_1, g_2) \implies g_2 \mid a_2^\prime$$$
    $$$g_2 \mid \text{lcm}(g_2, g_3) \implies g_2 \mid a_3^\prime$$$
    $$$\therefore g_2 \mid \gcd(a_2^\prime, a_3^\prime)$$$


    $$$g_1 \mid a_2 \text{ and } g_2 \mid a_2 \implies \text{lcm}(g_1, g_2) \mid a_2 \implies a_2^\prime \mid a_2$$$
    $$$g_2 \mid a_3 \text{ and } g_3 \mid a_3 \implies \text{lcm}(g_2, g_3) \mid a_3 \implies a_3^\prime \mid a_3$$$
    $$$\gcd(a_2^\prime, a_3^\prime) \mid a_2^\prime \text{ and } a_2^\prime \mid a_2 \implies \gcd(a_2^\prime, a_3^\prime) \mid a_2$$$
    $$$\gcd(a_2^\prime, a_3^\prime) \mid a_3^\prime \text{ and } a_3^\prime \mid a_3 \implies \gcd(a_2^\prime, a_3^\prime) \mid a_3$$$
    $$$\therefore \gcd(a_2^\prime, a_3^\prime) \mid \gcd(a_2, a_3) = g_2$$$

    Hence,

    $$$ \gcd(a_2^\prime, a_3^\prime) = g_2 $$$
»
3 недели назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

I really like the tree-invariance solution for D, great problem!

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

we can use dp in problem B dp[i][0] not pick i ,max ans dp[i][1] pick i ,max ans

dp[n - 1][0] = 0
dp[n - 1][1] = 1
for i in range(n - 2, -1, -1):
    if an[i] > i:
       dp[i][1] = 1 + max(dp[i + 1][0], dp[i + 1][1]) - dp[an[i]][1]
    else:
       dp[i][1] = 1 + max(dp[i + 1][0], dp[i + 1][1])
       dp[i][0] = max(dp[i + 1][0], dp[i + 1][1])
»
3 недели назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

A faster (time-complexity-wise) and simpler (or at least in my opinion) solution for C2 is to precompute which primes you can multiply A[i] by before doing DP. This operation is only O(N*P) where P is the number of primes. In my case, even 17 primes were able to get AC. Then, the DP becomes much easier, where dp[i][j] is the maximum number of A[x]s (0 <= x <= i) you can multiply by a prime number without collisions. You multiply A[i] by the jth prime number (you also have to include the case where you don't multiply A[i] by anything). This is at most O(N * P^2). Then, the answer is the maximum of all j from 0 to N-1 in dp[N-1][j] + the number of A[i]s that you could originally change without multiplying by a prime. If N <= 10^6, then this would still work under the 6s time limit (P <= 17, although I believe it can be lower than this).

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

C2 we can actually solve with only 10 primes

let's solve c1 and detonate indexes where the lcm is ai and 2 <= bi/lcm < 31 as hard points (if bi/lcm = 1 then there is nothing we can do, if it's bigger than 31 we an always solve, notice 31 is the 11th prime)

now lets look at continuous blocks of this hard indexes we as the editorial says will do dp[i][j] solve for the ith prefix and we put in the i index the j prime, now we can only put in i something that i-1 didn't have, and then put other prime in i-1, this is all checked just like the editorial just without the i + 1 things so I wrote it less formal.

so we can make the solution run in O(n*100*log(maxA)))

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

    if it's bigger than 31 we can always solve

    This is not true; having too few primes will fail test 7. In particular, if you have $$$a = [625045033, 1, 983752770]$$$ then you need to multiply the $$$1$$$ by a prime at least $$$53$$$.

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

      You are more than welcome to look at my submission, IDK how to add a link and it passed.

      secondly notice that in the i step I am only keeping the gcd(ai, ai-1), therefore only the first 10 prime needed and not the first 20 because bi is smaller than 1e9 so there is no point of checking more than that

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

        because if we have 10 primes for each, one can only posses 9, therefore there must be one who we can use.

        in your example the array transforms to 1,1,1 so it's not really intresting

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

          Edit: my previous comment was wrong, here's a correction.

          Consider the array $$$a = [625045033, 625045033, 1, 983752770, 983752770]$$$. Now you cannot reduce any of the elements. The third element requires a prime at least $$$53$$$.

          I see what your code is doing now; you just got lucky with the weak tests (several other comments also noted the weak tests). Indeed, your code is hackable with the following case:

          1
          5
          625045033 625045033 1 983752770 983752770
          625045033 625045033 37 983752770 983752770
          

          where your code outputs $$$1$$$ but the desired answer is $$$0$$$.

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

            OH yeah I get it now, I was wrong in my analysis.

            well good thing I got lucky :) but I think if I were to get WA in contest I will just raised the number of primes anyways but it's nice not to get WA

            but I think I do have a solution in n*log(max(A)), actually it's not_amir after a discord call

            we keep for each one all of the primes he is allowed to do up to 20 this time, and we go from left to right on this blocks, if our size is not 0, we check the prev, if it's size is 0 we can do anything so we add 1, if it's size is bigger than 1 we can also do anything, cause for everything we do he can do the one we didn't do, if it's size is 1 than ofcourse we prefer to do him, so we remove it's prime from our option, (if we can do it's prime).

            would like to get your comment if you think thats works but I am pretty sure that better simplelife

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

    Ideally this solution shouldn't pass but the reason it passes is because of weak test cases :(. In test cases 7,8,9, meant for catching solutions using less primes, the b_i is set to 1e9, which indirectly allows this incorrect solution to pass. I didn't consider this specific case while making test cases, it was an oversight on my part, sorry for that.

    Consider the test case, as mentioned by reirugan, $$$a=[625045033,625045033,1,983752770,983752770]$$$ and $$$b = [1,1,32,1,1]$$$. Your solution should give WA here

    Upd : i didn't notice reirugan had posted this already, srry for repost.

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

Here in this code block for Problem C2 in Editorial

Code

why is it safe to skip

"else dp[i][j] = max(dp[i][j], dp[i-1][k])"

after

if (gcd(val1, c[i+1]) == gcd(a[i], a[i+1])) dp[i][j] = max(dp[i][j], dp[i-1][k] + 1);

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

I thought of the following for an O(n(L + log(max(A)))) solution of C2...

So we have our C[i] where A[i] must be some x times C[i], let that x be K[i], so A[i] = C[i]K[i]. Let the original K[i] be ak[i]. I then define D[i] = B[i]/C[i], and that's the upper bound of K[i]. Now, just like in the editorial, if D[i] != 0, or similarly B[i] >= C[i], then immediately set K[i] = 1, intuitively to let each value be as unrestrictive as possible to other elements affected by it which are adjacent elements.

Spoiler

We now just need to change the indices where ak[i] = 1 and D[i] != 0. For this, we have the following constraints:

  • gcd(K[i], K[i+1]) = 1
  • gcd(K[i-1], K[i]) = 1
  • gcd(K[i], lcm(A[i-1]/gcd(A[i-1], A[i]), A[i+1]/gcd(A[i+1], A[i]))) = 1

Where those A[i-1] and A[i+1] are the possibly-modified A[i]s based on K[i]s. As in if we set K[i] = 1, then that's what we use in those constraints. We let that value on the last constraint be F[i], thus the last constraint becomes gcd(K[i], F[i]) = 1.

Spoiler

We consider these constraints for all indices at once without yet modifying anything. By doing this, we can consider the set choice[i] with nchoice[i] elements to be all prime numbers <= D[i] satisfying the constraints above, which are all of the possible choices of K[i] with the absolute least possible constraints it could ever have which is one of the key things in here. We only need to consider three of such values, because the only thing constraining K[i] are two values, K[i-1] and K[i+1], and since we can simply choose 1 prime, the constraint is simply a not-equal-to constraint. So three values is enough and is used when the left and right values are within i's set.

Spoiler

Now, consider a subarray where we are able to change everything on that subarray, in other words that subarray satisfies D[i] != 0, and ak[i] = 1. Then, if nchoice[i] is at least two for all elements of that subarray, there definitely exists a way to set every K[i] in there where adjacent elements are distinct. I'm just trying to say that in that case it wouldn't even be three anymore.

So the idea here is we simply set all indices where nchoice[i] is precisely 1. Of course, we absolutely must set that value to choice[i][1], so we do that, but then we propagate left and right to remove choice[i][1] from the left and the right, and if their nchoice[i] becomes 1, do the same there and continue propagating. Now we're left with nchoice[i] >= 2 for all indices that we can and need to change, or also of course nchoice[i] = 0 but that is within the set of indices that we can't change there. Anyways for the remaining indices i where nchoice[i] >= 2 it's already guaranteed that we can set all of those a[i] to the value we want so we let them be what they are.

Spoiler

368881815

I think this will pass the additional challenge, no? I mean, submission time is 62ms with n=5e4, and 1e6/5e4 * 62ms should be less enough than 6s too.

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

In problem C1, If I get an array: a = [1, 2, 1, 2, 1], b = [3, 3, 3, 3, 3].

I can only reduce i=1 and i=3.

But, i can also change the array completely to a' = [2, 1, 2, 1, 2]?

And the GCD of every subarray should be same (l<r)?

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

I didn't get smth in C2. In first hint you tell if you can do less than a[i] then do it, but what if b[i] is less than that value you try to set to?

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

B. Simply Sitting on Chairs

  • assume indexing starts from 1
  • assume u're at first i for which arr[i]>i
  • hence there was no j < i for which arr[j]=i
  • so, there's a j>i for which arr[j]=i
  • hence, choosing j instead of i returns same value without restriction
  • hence, choose all indexes for which arr[i]<=i
»
2 недели назад, скрыть # |
 
Проголосовать: нравится -8 Проголосовать: не нравится

A better solution for problem B is :

include <bits/stdc++.h>

using namespace std;

int main() {
	int t; cin >> t;
	while(t--){
	    int n; cin >> n;
	    int count = 0;
	    for (int i = 0; i < n; i++){
                int k; cin >> k;
	        if (k<=i+1) count++;
	    }
	    cout << count << endl;
	}
}

The logic is that we only need to sit on the good chairs (where pi is less than or equal to the actual position of the chair).

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

There is no need for DP in C2. The solution could be much simpler.

We know that it is always better to decrease $$$a_i$$$ whenever possible. Notice how decreasing any $$$a_i$$$ depends only on $$$\gcd(a_i, a_{i \pm 1})$$$ which should stay constant. Thus, we can start by applying all possible decreases and then moving on to possible increases, this is also important because increasing any $$$a_i$$$ doesn't only depend on the $$$\gcd$$$ but on adjacent values too, i.e., it is optimal when $$$a_{i \pm 1}$$$ is minimal, so decreasing first is crucial.

When increasing, we are now sure that $$$a_i = LCM(A, B)$$$. My claim is that if there are at least $$$2$$$ primes we can multiply $$$a_i$$$ by, then it is always possible to increase $$$a_i$$$ independent of other elements in the array. It is not possible when we have two adjacent elements that can only be multiplied by the same prime (let's call such an element stinky), i.e., multiplying one of them by that prime disallows us from multiplying the other by that same prime because they are adjacent.

Basically, we can iterate through the indices from left to right, and do $$$a_i := a_i \cdot onlyPrime$$$ whenever we have a stinky element so we remove the possibility of choosing this prime in the next iteration. Since, we only need to check the first $$$2$$$ primes that work, we can just use the $$$O(\sqrt n)$$$ version of the $$$\text{check_prime}$$$ algorithm.

Code: 370020187

»
3 дня назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится
Could somenoe please help me get why my sol for C1 is incorrect
»
29 часов назад, скрыть # |
 
Проголосовать: нравится +3 Проголосовать: не нравится

Stupid solution for E that has a total query cost of about $$$2.38 \cdot n$$$ (for non-trivially large $$$n$$$): 371644325

Define $$$g(l, r)$$$ to be the parity of $$$((r - l + 1)/f(l, r))$$$. One can notice that for even $$$(r - l + 1)$$$, $$$g(l, r)$$$ is equal to the parity of the number of ones in $$$s[l, r]$$$.

  • We will use this trick to maintain an xor-constraint graph on $$$n$$$ nodes. Every node $$$u$$$ has a hidden binary value $$$s_u$$$ that we initially do not know, and we will add edges to this graph of the form $$$(u, v, w)$$$ where $$$s_u \oplus s_v = w$$$. We aim to have a connected graph without contradictions at the end, as this will give us exactly two possible arrays $$$s$$$.
  • Let $$$m$$$ be the smallest even integer such that $$$2 \cdot m \geq n$$$. We query $$$f(i, i + m - 1)$$$ for all $$$i \in [1, n - m + 1]$$$. Then for every $$$i \in (1, n - m + 1]$$$, add an edge $$$(i - 1, i + m - 1, g(i - 1, i - 1 + m - 1) \oplus g(i, i + m - 1))$$$. This adds $$$\approx n/2$$$ edges to the graph at a total cost of $$$(n - m + 1) \cdot (n/m) \approx n$$$.
  • Now here's the low IQ step: iterate over all $$$(l, r)$$$ such that $$$1 \leq l \leq r \leq n$$$ and $$$2 \mid (r - l)$$$ in decreasing order of $$$s = (r - l + 1)$$$ and increasing order of $$$l$$$. If $$$l$$$ and $$$r$$$ are in different components, query $$$f(l, r - 1)$$$ and $$$f(l + 1, r)$$$, and add edge $$$(l, r, g(l, r - 1) \oplus g(l + 1, r))$$$. This ends up being pretty efficient (especially if you cache queries) because one can show that just by using $$$(l, r)$$$ with $$$r - l + 1 \geq m$$$, we can add at least $$$n - 4$$$ of the required $$$n - 1$$$ edges.
  • Once the above procedure ends, it's easy to show that we end up with exactly 2 connected components, the first containing all even indices, and the second containing all odd indices.
  • Simply query $$$f(1, 2)$$$ and add edge $$$(1, 2, g(1, 2))$$$ at a massive cost of $$$n/2$$$ to finish up.

Since the query order here depends solely on $$$n$$$ (and not $$$s$$$), one can easily check the query cost on all values of $$$n$$$ offline.

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

    As it turns out, the step with $$$m$$$ is actually unnecessary. You can just run the low-IQ procedure and then unite $$$(1, 2)$$$ to get a worst case query cost of $$$2.6 \cdot n$$$: 371648816.

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

    This used to be the intended solution, before we came up with the current solution given in the editorial.

    We didn't reduce the cost limit to kill this solution because we thought the limit would be too tight and it was not worth to do it.