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

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

1582A - Лунтик и концерты

Идея: 74TrAkToR

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

1582B - Лунтик и подпоследовательности

Идея: AlFlen

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

1582C - Баба Капа вяжет шарф

Идея: AlFlen

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

1582D - Вупсень, Пупсень и 0

Идея: AlFlen

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

1582E - Пчеленок и подотрезки

Идея: 74TrAkToR

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

1582F2 - Корней Корнеевич и XOR (сложная версия)

Идея: 74TrAkToR

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

1582G - Кузя и домашнее задание

Идея: 74TrAkToR

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

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

Lighting Fast Editorial and Thanks for the wonderful problems.

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

Thanks for the contest and the lightning fast editorial, but on my computer the tutorial doesn't work, it says


[problem:] Tutorial is not available

it would be great if you could fix

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

The editorial is currently not working though it's already written, we're trying to find out why.

UPD: I've added ABC by hand

UPD2: It's working now

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

Can someone tell why my logic is failing in B?? I am getting WA in B. ~~~~~ //My short logic cnt = count of 1; cnt0=count of 0 ; final answer will be - cout<<(cnt*pow(2,cnto))<<"\n"; ~~~~~

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

    pow return answer in floating point format. (so, maybe precision error) I also used it and got a WA. Use a loop or self-defined function to calculate power.

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

      I also used the power function which works in O(logy) but this also failed . Can you tell why ?? ~~~~~

      ll power(long long x, long long y) { int res = 1; // Initialize result

      // Update x if it is more than or // equal to p

      if (x == 0) return 0; // In case x is divisible by p;
      
      while (y > 0)
      {
          // If y is odd, multiply x with result
          if (y & 1)
              res = (res*x) ;
      
          // y must be even now
          y = y>>1; // y = y/2
          x = (x*x) ;
      }
      return res;

      }

      ~~~~~

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

    just use the bit operator << for powers of 2 in general.

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

    A usual double uses only 56 bits of precision, but we can have up to 59 zeros. Consider to use 64-bit integers and shift operation instead of pow().

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

    dont forget that you must use long long

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

    2^cnto can go beyond Integer max, maybe use 2ll?

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

    if only has 1,the answer is 1.

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

AlFlen for D for the case where all elememnts are 10000 and n=100000 then the sum of abs(bi) = 1000010000, which is >10^9 Acc to editorial.

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

    it will only happen for odd numbers(when n is odd) since 10^5 is even it is possible to make it in <=10^9. The highest odd number it can go is 10^5 -1. so the sum will go till 10^4*(10^5-1)+10^4... which is 10^9.

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

    When n=1e5, since n is even the overall sum will be sum(abs(ai))<=1e9. And the largest odd value of n is 1e5-1, in that case one of abs(bi)>=1e4 and hence the total sum still remains less than 1e9.

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

Problem B cpp failed sol | Problem B python passed sol any idea why cpp solution failed while python passed? they were the same pseudo-code :/

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

Problem — B

Left shift
Logarithimc power method
Pow

Is'nt left shift and logarithimic power method better than pow??

why did my solution with left shift failed and how it is different from one in the editorial

anyone can explain why??

thanks in advance.

»
3 года назад, # |
  Проголосовать: нравится 0 Проголосовать: не нравится
> (действительно, если просто действовать по жадному алгоритму и брать 
> трехминутные треки пока можем, затем двухминутные, затем одноминутные,
> то мы сможем получить любую длительность).
> 
> Тогда ответом на задачу будет являться остаток по модулю 2 числа S

Кто-нибудь, может привести доказательство, что это так? Я не могу понять, почему это так?

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

    Когда мы наберем длительность с помощью трехминутных песен, останется какой-то остаток при делении на $$$3$$$ (если трехминутные песни не кончатся раньше, если кончатся, то дальше работаем с песнями длительностью $$$2$$$ и остается в итоге остаток при делении на $$$2$$$, который всегда не больше $$$1$$$), теперь любой остаток при делении на $$$3$$$ мы умеем добавлять (остается хотя бы одна двухминутная и хотя бы одна одноминутная).

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

For Problem B,

The suggested solution returns an answer of 4 subsequences for the testcase below:

1

3

0 0 1

When I counted I could only get 3 subsequences:

[empty set]

0

0 0

Could somebody tell me if I had misunderstood? Sorry for bad formatting and thanks in advance.

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

    There are two sets with a single '0', because it is two different positions.

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

      I see... perhaps the question could have been more clear about the definition of a distinct subsequence. It appeared to me that subsequences would be equivalent if they had the same elements in the same order. Thanks for the clarification!

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

    Both 0s are considered different hence total number of sequences will be 4. "0" sequence will be twice. Basically ans=pow(2,zeros)*ones

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

    There are two subsequences which consist of just 0

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

    The subsequences are considered different if they containt elements with different indices, so $$$0$$$ (index $$$1$$$) and $$$0$$$ (index $$$0$$$) are two different subsequences.

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

      Стоит сказать, что при буквальном прочтении русского условия это неправда.

      Напоминаем, что последовательность x является подпоследовательностью y, если x может быть получена из y удалением нескольких (возможно, ни одного или всех) элементов.
      

      Здесь ничего про индексы не говорится, и исходя из условия, у последовательности не может быть две различных подпоследовательности вида [0]

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

        It's unfortunate that the statement missed the definition of different subsequences, but samples could clear up the misunderstandings, since there was a test with equal in elements but different in indices subsequences. My bad there was no clearer explanation in the legend part.

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

weak pretests!!! rnm tq!

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

The editorial is working now

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

problem A was really misleading and harder than most div2 A

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

In D, if odd n you can make MAXA * (MAXN + 1)

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

    No, because MAXN is even. So you get MAXA*(MAXN-1+1). Cause biggest odd number is MAXN-1

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

I probably overkilled it, but i tried a fenwick tree solution to F1 but it gives WA on test 7. Could someone help?

https://codeforces.com/contest/1582/submission/132929945

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

Why does my code fail on D?

Spoiler

Please don't downvote, I might be completely wrong

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

    I had the same approach — consider the vector (1,2,2), i.e. where two elements are the same. Your answer outputs a zero for the first coefficient, which is not allowed.

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

D is slightly harder if sum of absolute values of $$$b$$$ is restricted to $$$n * max(|a_i|) + 1$$$. I accidentally solved this problem. Anybody interested can try too.

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

    yes, we can let $$$b_i = {-1, 1}$$$ for $$$1 \le i \le n-1$$$, then we've got $$$s = \sum\limits_{i=1}^{n-1}a_i \cdot b_i \le \max(a_i)$$$ and of course $$$a_n \le \max(a_i)$$$. Then we let $$$b_{n} = -s$$$ and multiply every $$$b_i, 1 \le i \le n-1$$$ with $$$a_{n}$$$

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

    How do you solve it?

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

    In fact, we can improve this bound on $$$\sum |b|$$$ to $$$O(n+\sum\limits_{x\in S}|x|)$$$, where $$$S$$$ is the set of values in $$$a$$$ without duplicates. We first process values of $$$a$$$ that are the same by a construction of $$$[1,1,1,\dots,-1,-1,-1,\dots]$$$ or $$$[2,1,1,\dots,-1,-1,-1\dots]$$$, and then process values of $$$a$$$ that only occur once with the original solution.

    I spent so long implementing this before realizing that when $$$n$$$ is odd it is at most $$$10^5-1$$$ :/

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

      its atmost 10^5 know?

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

      also if i use editorial approach when $$$ n=10^5 $$$ and all $$$a[i]= 10^4 $$$,wont the sum of $$$ b[i] $$$ becomes $$$ 10^9+10^4 $$$ ? or am i doing wrong ? :( please anyone clarify

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

    D can be solved using the pigeon hole principle as there exist significantly many repetitions in the extreme case where $$$n=10^5$$$ and $$$a_i$$$ takes at most $$$2\times10^4$$$ values, we can sort and eliminate adjacent identical elements using $$$(-1, 1)$$$ which significantly reduces the absolute sum of $$$b_i$$$'s, remaining cases can be handled similar to the editorial.

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

Actually Could someone pls tell me why my approach on D failed ? consider a[1] * x + a[2] * y = a[3] + a[4] + ...

let t be the gcd(a[1], a[2]) let v1 = a[1] / t, v2 = a[2] / t

so t1 * x + t2 * y = sum, where sum = a[3] + a[4] + ...

we can solve for x and y using the extended eucledian method for finding the inverse and handling some corner cases. (when t2 * y = sum, ... )

Then we assign b[3] = b[4] = ... = t and a[1] = -x, a[2] = -y

Let g be gcd(-x, -y, t) divide the whole b array by g and print the answer.

But wrong answer on test 3 submission. Any help would be appreciated.

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

EDIT : Ignore this. The hypothesis was wrong.

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

Let's notice that since a,b,c≥1, it is possible to make a concert of any duration from 0 to S

Seems like you are solving a diophantine equation. But can I get more explanation on that statement please?

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

In problem A, Can someone explain why we can make every value from 0 to S?

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

    The way I see it is: for numbers that require less than all the segments of length 3, you can always write as 3k + r, where k is how many 3 length segments you take, and if r is 1 or 2, you use the corresponding length (there's at least one of each). If you need all the ones of length 3, just repeat this for the remaining length with those of length 2 and 1, namely, if it's even you use the 2's and you're done, and if it's odd, just add one of length 1.

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

      Thanks a lot. I got it

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

      If you need all the ones of length 3, just repeat this for the remaining length with those of length 2 and 1, namely, if it's even you use the 2's and you're done, and if it's odd, just add one of length 1 The above statement is not clear .can you please explain

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

        You're right. The idea is correct but my last statement was not. Suppose you had to use all length 3 segments. Then proceed like before for the new sum $$$x-3c$$$, where $$$x$$$ was the sum you initially wanted, but you now ran out of length $$$3$$$ segments.

        For numbers that require less than all the segments of length $$$2$$$, you can always write as $$$2k + r$$$, where $$$k$$$ is how many $$$2$$$ length segments you take, and if $$$r$$$ is $$$1$$$, you use one $$$1$$$ length segment (there's at least one).

        Note that I kind of copypasted my message from before, changing $$$3$$$ by $$$2$$$, to show the idea is analogous.

        Now if you had to also use all segments of length $$$2$$$, you now only have segments of length $$$1$$$, so you just need to add these until you get the desired sum trivially (you can get up to the maximum sum $$$S$$$).

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

My approach to F2 seems a bit different from the editorial's:

Say $$$dp_i$$$ contains all values $$$(ind_j, v_j)$$$, sorted by $$$ind_j$$$, such that it is possible to get an increasing subsequence with an xor of $$$v_j$$$ and last element at index $$$ind_j$$$, using only elements in the array less than or equal to $$$i$$$. If there are multiple possible values for one $$$v_j$$$, only store the one with the minimum $$$ind_j$$$.

To transition from $$$dp_{i - 1}$$$ to $$$dp_{i}$$$, first, add all elements already in $$$dp_{i-1}$$$ to $$$dp_i$$$. Then, for each $$$(ind_j, v_j)$$$ in $$$dp_{i-1}$$$, take the minimum index $$$k$$$ such that $$$a[k] = i$$$ and $$$k > ind_j$$$. Then, add $$$(k, v_j \oplus i)$$$ to $$$dp_i$$$ (if this $$$k$$$ does not exist, don't do anything). This can be done in linear time with two pointers if you store the occurrences of each $$$i$$$.

Two important details:

  1. To make sure that each $$$dp_i$$$ remains sorted, while transitioning, you can store the values $$$(k, v_j \oplus i)$$$ and $$$(ind_j, v_j)$$$ in two separate vectors, since each of these separately will be sorted. Then, combine the two in linear time (same method used in mergesort).
  2. To make sure you only store the minimum $$$ind_j$$$ for each $$$v_j$$$, you can keep track of the minimum value $$$ind_j$$$ for each $$$v_j$$$ with an array $$$minindex$$$. Then, only add a pair $$$(ind_j, v_j)$$$ to the dp if $$$ind_j = {minindex}_{v_j}$$$.

My code: 132932565

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

Korney Korneevich sounded like gennady korotkevich to me :-)

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

132925114, this is my solution for E, i am getting RE on test 7 with exit code 2147483647, can someone please help.

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

I wonder whether it is possible to solve F2 with python? (python 3.8 or pypy or pypy-64) indeed python is somehow disadvantaged...but indeed it is easier to write-.-

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

Where is the editorial for F1?

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

Are there any other solutions for D?

except editorial

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

Can you somehow solve D with a Diophantine equation? I tried to, but I failed on the third test.

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

    not needed just make pairs if n is even else make pairs with n-3 elements and for first three give a[2] value for result[0] = a[2] and result[1] = a[2] then calculate result[2] = -( a[0]*result[0] + a[1]*result[1] ) / a[2] if a[2] is zero make result[0] = 2a[2] and then calculate result[2]

    code

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

Could someone plz share the tutorial of Problem F1, which seems to have an easier solution. Thx!

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

Could someone plz share the tutorial of problem F1 which has a relatively easier solution? Thx!

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

    Solution for 1582F1 - Korney Korneevich and XOR (easy version):
    We want to find all $$$x$$$ such that there exists a strictly increasing subsequence with xor sum equal to $$$x$$$ in the given array. Since $$$0 \le x \le 500$$$, there are about $$$512$$$ possible values of $$$x$$$, and they form a contiguous range — $$$x \in [0, 512]$$$. So let's do some dynamic programming where $$$\it{dp}_x$$$ stores the smallest possible ending value of a subsequence which has a xor sum of $$$x$$$. We store the smallest ending value of a subsequence with xor sum $$$x$$$ because when we want to form a particular xor sum by including an element, our strictly increasing condition must be satisfied and storing the smallest value allows for a greater range of new ending values. We can initially set $$$dp_x$$$ to $$$0$$$ and all the other values to $$$\infty$$$. To transition between states, iterate over the elements in the array and for each value of $$$x$$$ try to find a subsequence which makes that $$$x$$$ possible. Minimise $$$dp_x$$$ with $$$A_i$$$ if $$$A_i > dp_{x \oplus A_i}$$$. Finally, we can form xor sum $$$x$$$ for all $$$x$$$ where $$$dp_x < \infty$$$. This works in $$$O(N\times\it{max}(A_i))$$$ which is $$$10^5 \times 500 = 5 \times 10^7$$$ operations in the worst case.

    Take a look at my submission for more clarity: 132945454.

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

C reference code is written very well!!!It's better than what I wrote.

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

I have a simmilar solution for C that should have better complexity:

First notice that the center of the palindrome can vary, but the edges don't. Because of this we can check the first with last, second with second to last, ... and stop when we find 2 different chars or we checked all pairs(answer is 0).

When we find some i such that S[i]!=S[N-1-i](with 0 based indexing) there are 2 possibilities:

  1. Deleting either one leaves a palindrome in which case the answer is the minimum of the required removals in the case that they produce a palindrome.
  2. Removing neither one of them will produce a palindrome so the answer is -1.

The reason why there are only 2 cases is that if we remove some other character then we will still have those 2 characters mismatched. So we need one of them to go away.

The complexity would be O(N) with at most 3 passes through the string. The solution presented in the tutorial does a similar thing but doesn't stop when it finds the different characters and so it loses time. With some tighter time limit(Or if we expand the alphabet, maybe with upper case letters or diggits) it might not fit.

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

As always, here are the video solutions for the first 6 problems(A to F1) :

solutions

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

There is an easy solution for F2. Let dp[i] be the first index in which its possible to make an increasing subsequence of xor i. Lets also maintaing a vector of vectors V such that V[i] stores all the occurences of i in the array in order.

We can now iterate through the values x in increasing order, and for we to update dp[i] we do dp[i] = min(dp[i],V[x].upperbound(dp[i^x])).

This works in $$$O(n+maxa^2*log(n))$$$: https://codeforces.com/contest/1582/submission/132952152

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

    could you explain how this solution ensure subsequence is increasing ?

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

      You are iterating through the values in increasing order, so you just worry about the index. If youre worried about repeating one of the values in the answer, notice that in that case you cant lower the value of the dp when we try to do that, so it wont count in the answer

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

    the concept is still not clear. Could you please elaborate more?

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

      What exactly wasnt clear?

      Also try checking the code and locate where im doing what im saying.

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

    Hey so (i^x) might be larger than i or smaller than i.... So how do u decide order of traversal of states

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

      We are iterating through the values of $$$x$$$ in increasing order so that every recurrence of the dp preserves that the sequence is increasing (and the search on the vector of ocurrences preserves the order of the indexes)

      Also notice that even though choosing the same number twice is invalid it doesnt actually change the dp because its always unoptmimal.

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

Proof for problem A solution

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

Can someone explain if this solution to problem G works? I was confident that I had a simple $$$\Theta(n)$$$ solution, but it's failing on test case 18 for some reason so now I'm feeling doubtful. I don't know if the problem is my code or my proof though.

Here is the proof:

We scan the array $$$a$$$ of size $$$n$$$ from left to right. For any integer $$$r$$$, we define $$$S_r$$$ to be the set of possible starting points $$$l$$$ such that the segment $$$[l;r]$$$ is simple. Then it follows from the definition that the solution to the problem is $$$\sum_{r\in [0,n)} |S_r|.$$$ Now, we consider how to compute $$$S_r$$$ from $$$S_{r-1}$$$. If $$$b_r$$$ is '*' or $$$a_r$$$ is 1, then $$$S_{r} = S_{r-1}\cup {r}$$$, since any path that worked before will still work, and the path starting at $$$r$$$ will also work.

Now, if $$$b_r$$$ is '/' and $$$a_r$$$ is not 1, then to get $$$S_r$$$, it suffices to take $$$S_{r-1}$$$ and remove invalid starting positions. (Namely, we remove any element $$$l\in S_{r-1}$$$ such that the segment $$$[l;r-1]$$$ ends with a number that does not divide $$$a_{r}$$$.) Observe that for any two elements $$$l_1 < l_2 \in S_{r-1}$$$, if the segment $$$[l_2;r]$$$ is simple, then the segment $$$[l_1;r]$$$ is also simple. To see this, first note that the segment $$$[l_1, l_2]$$$ must be simple, since otherwise, $$$[l_1, r-1]$$$ would not be simple, contradicting the fact that $$$l_1\in S_{r-1}.$$$ Thus we just need to remove the largest few elements of $$$S_{r-1}$$$ to compute $$$S_r$$$.

For any two consecutive elements $$$l_i, l_{i+1} \in sorted(S_{r-1} \cup r)$$$, we can say that the value of the path $$$[l_i;l_{i+1}]$$$ is the value remaining after traversing this path. To compute $$$S_r$$$, we continuously pop the largest-indexed element from $$$S_{r-1}$$$ and divide $$$a[r]$$$ by that element's value until $$$a[r]$$$ is 1. Some caution is needed to maintain the invariant — you need to update the last value you popped out from being the value of a path $$$[l_i;l_{i+1}]$$$ to the value of the path $$$[l_i, r]$$$.

Here is the code: https://codeforces.com/contest/1582/submission/132953817

In the code, V is a stack of the value of paths $$$l_i, l_{i+1} \in sorted(S_{r-1} \cup r)$$$.

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

    It doesn't work, check this simple counter example:

    4
    3 2 3 2
    **//
    

    I think you will be able to understand why it doesn't work by yourself.

    *I tried the same :)

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

Maybe maintests of F2 are a bit weak.
My sulotion to F2 has only considered the first and last three times of occurrences for each different elements.
So can somebody hack me or prove that it's correct.
Here it is

Anyway,There is also a sulotion has passed maintests but hacked by me. this one
Perhaps we need stronger maintests

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

I want to understand F1!

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

I have another solution for E that works in $$$O(NlogN)$$$. Maybe it is hackable because I am not good at proving.
Here is the submission: 132984246
Reverse the array first. Let $$$dp_{i,j}$$$ be the maximum sum on the segment of length $$$j$$$ considering all elements on the prefix $$$i$$$.
Then the transition should be like:

for(int i = 1; i <= n; i++) {
	for(int j = 1; j <= n; j++) {
		dp[i][j] = max(dp[i][j], dp[i - 1][j]);
	}
	long long sum = 0;
	for(int j = 1; i + j <= n; j++) {
		sum += a[i + j - 1];
		if(sum >= dp[i - 1][j] && sum < dp[i - 1][j - 1])
			dp[i + j - 1][j] = max(dp[i + j - 1][j], sum);
	}
}

I added sum >=dp[i-1][j] to the condition because if sum<dp[i-1][j] then sum>=dp[i+j-1][j] will not holds.
It can be noticed that $$$dp_{i,j} \ge dp_{i,k}$$$ if $$$j \le k$$$ (Like LIS) and sum is increasing, so the condition sum>=dp[i-1][j] holds for monotonicity. Also, sum<dp[i-1][j-1] must hold, too.
Therefore, the if condition will be triggered at most once for every $$$i$$$: when the first $$$j$$$ such that $$$sum(i..(i+j-1))\ge dp_{i-1,j}$$$. And we can use Binary Search to find such $$$j$$$.

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

I request proof of 1582C - Grandma Capa Knits a Scarf. Why this approach indeed find optimal removal for fixed letter? AlFlen can you explain?

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

    Think of the greedy algorithm as of a forming a palindrome from the beginning and from the end. If the symbols are equal, there is no need to erase them, and if they are not, the palindrome can't be formed without the erasure (we can't choose which symbol to erase since only one can be equal to the chosen letter).

    Why us not erasing one of equal symbols will not lead to a wrong answer: since they are equal, it's either we can erase both or we can't erase any, and we won't break any future pairs in palindrome (both current symbols are equal to $$$c$$$) because if one of them could be paired (in palindrome) with another symbol (equal to $$$c$$$ since we have the ability to erase current symbols) but it didn't, it's no matter what to erase: current symbol or the future one, but with not erasing the current one we don't break our palindrome sequence and don't disturb further formation.

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

      it's no matter what to erase: current symbol or the future one, but with not erasing the current one we don't break our palindrome sequence and don't disturb further formation.

      It's easy to say, but why it doesn't break? I'm starting to understand. If we remove pair — obviously better to keep pair. But if we remove one when we could keep pair, then $$$c$$$ will remain at one side, and we either finally keep it, then it will be paired with other $$$c$$$, so we could pair it in the first place, or it will be removed, so we could remove it in the first place. Not sure if it's rigorous.

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

Can anyone Explain point 1 of 1582F2 — Korney Korneevich and XOR Tutorial using an example: like test case: 12 4 2 6 5 2 4 5 6 4 2 5 6??

[user:74TrAkToR][user:AlFlen]

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

Can Anyone Explain or prove point 1 of 1582F2 — Korney Korneevich and XOR (hard version) Tutorial (using a test case would be very helpful) . example test case is:

12

4 2 6 5 2 4 5 6 4 2 5 6

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

Hello guys,

So, in problem E I tried implementations with 2D array as well as vector<vector >. However, only vector implementation passes and the array implementation gives runtime error on test 8. Can somebody explain this? Besides, the vector implementation needed 1900 ms to pass which either means that the 2s bound was tight or I have implementation issues. Can anyone suggest improvements in vector implementation?

vector impl: 133108745 array impl: 133108661

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

Hello guys,

So, in problem E I tried implementations with 2D array as well as vector<vector >. However, only vector implementation passes and the array implementation gives runtime error on test 8. Can somebody explain this? Besides, the vector implementation needed 1900 ms to pass which either means that the 2s bound was tight or I have implementation issues. Can anyone suggest improvements for vector implementation?

vector impl: 133108745 array impl: 133108661

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

can anyone pls explain the last line of editorial of B cout << (1ll << cnt0) * (ll)cnt1 << '\n';

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

    (1ll << cnt0) * (ll)cnt1 =( 2^(cnt0) ) * cnt1, where cnt0 = # of 0's and cnt = # of 1's in the array a.

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

I am getting TLE for Problem E but I think my solution is O(nk), k = sqrt(n). Not sure why. could anybody please help me with this. Solution link : https://codeforces.com/contest/1582/submission/133125904.

Although I am looping for j=1 to k and then calling memo(), but according to me, the total dp state calculation time is O(nk) only.

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

In problem D in the case of n being odd

isn't the sum of the array b going to be 1e9+1e4 which exceeds the required constraints?

Can someone please explain it to me?

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

    TL;DR No because the max value for $$$n$$$ where $$$n$$$ is odd is $$$n=10^5-1$$$.

    The worst case where $$$n$$$ is odd is $$$n=10^5-1$$$ and $$$a_i=10^4$$$ for all $$$i$$$. So our $$$b$$$ array has $$$10^5-2$$$ elements where $$$|b_i|=10^4$$$ and one where $$$|b_i|=2*10^4$$$. And the sum is $$$(10^5-2)*10^4+2*10^4=10^5*10^4=10^9$$$.

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

In editorial for G, can someone please explain what next_i is really calculating?

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

For problem C, the answer can probably be obtained by using either the first or the last character in the original string. There is no need to run the algo for all the 26 characters :p

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

Can anyone explain how is The sum |b1|+|b2|+…+|bn−3| does not exceed MAXA⋅(MAXN−1−3).

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

F1 can be done using segment tree + bitset optimization

Submission here: 155885705

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

Extremely simple $$$O(n + (max(a[i])^2 + max(a[i]).\log{max(a[i])})$$$ solution for F-2: link

(Basic idea is to consider all elements in increasing order of value, as then we dont have to care about ensuring subsequence is increasing, we just have to care about indices of consecutive elements in the subsequence.)