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

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

I hope you liked problems!

Sorry for incorrect placement of problems. I had to do swap(E, F).

Tutorial is loading...
Solution C++
Tutorial is loading...
Solution C++
Tutorial is loading...
Solution C++
Tutorial is loading...
Solution C++
Tutorial is loading...
Solution C++
Tutorial is loading...
Solution C++
Разбор задач Codeforces Round 632 (Div. 2)
  • Проголосовать: нравится
  • +149
  • Проголосовать: не нравится

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

Auto comment: topic has been updated by PuRpLe_FoReVeR (previous revision, new revision, compare).

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

Finally !! thanks for the editorial.

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

Can anyone help why i am getting wrong answer on testcase 7 -76004123

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

I guess many people print something like chess-board on the problem A, so do I lol

Anyway, the editorial of the problem A is really amazing!

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

Another way to explain F is

GCD will be 1 when all the numbers are prime. So let x be the number of primes till n. Therefore for each i in [2, x] answer will be 1. After that one can observe we should include the smallest composite number every time.

Proof: If we include y before x such that y > x. Let pf(k) denotes the maximum prime factor of number k. So since we know that all prime numbers are included in the set, this condition will simply hold pf(y)>=pf(x). So it is optimal to select x before y to minimize imperfection.

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

    Consider n = 9

    Of course, we first take all the prime number from 2 to 9 and 1 as well.

    1 2 3 5 7

    Hence, for k = 2 to 5, the minimal imperfection is 1 because the gcd is always 1.

    For k = 6, we pick 4 so minimal imperfection is now 2.

    For k = 7, we pick 6 so minimal imperfection is now 3.

    For k = 8, we pick 9 so minimum imperfection is still 3.

    For k = 9, we pick 8 so minimum imperfection is now 4.

    In this example, it doesn't hold that we take the smallest composite number every time. Correct me if im wrong.

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

The problems were very good but the tutorial is too damn late bro still you really did a good job thanks!

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

Can someone please explain why my solution for problem A is failing. 76024216
Maybe, there's an issue with my declaration of the 2d array, as the output to some of the subcases has 'W' in the top left as well as right corner which is not what I intend to do.

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

Do anyone know how to prove minimum bound for k in problem.D?

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

can anyone help me? It says i failed on test2 token 175: 76028180

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

Is it possile to solve C using segment tree.If yes then please give your code or explain it.

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

Somebody want to explain F? I do not get it at all. What is the question, what is the idea to solve it?

There is obviously something with divisors because the imperfection is defined using gcd. And then?

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

    The problem: Given the set of integers from $$$1$$$ to $$$n$$$, $$$\forall$$$ $$$i$$$ $$$\epsilon$$$ $$$[2..n]$$$, find a set, $$$M$$$ of size $$$i$$$ such that max value of $$$\gcd (p,q)$$$ $$$\forall$$$ $$$p,q$$$ $$$\epsilon$$$ $$$M$$$ is minimized.

    The solution: Firstly, note that $$$1$$$ must always be included in the optimal set. Next, note that if there are $$$P$$$ primes from $$$[2..n]$$$, then max value of $$$\gcd (p,q)$$$ $$$\forall$$$ $$$p,q$$$ $$$\epsilon$$$ $$$M$$$ is always $$$1$$$. Hence, for the first $$$P$$$ numbers, answer will always be $$$1$$$. Now from the $$$p+1$$$ th number onwards, if we include an integer $$$x$$$, the optimal set always contains at least $$$1$$$ divisor $$$d$$$ $$$(1\le d \lt x)$$$ of $$$x$$$. Why? Since we have already included all the primes from $$$[2..n]$$$, all the numbers we are left with are composite. Now if we add $$$x$$$ to the set $$$M$$$, max value of $$$\gcd (p,q)$$$ $$$\forall$$$ $$$p,q$$$ $$$\epsilon$$$ $$$M$$$ is the largest divisor of $$$x$$$, since $$$\gcd (\texttt {largest divisor of } x, x) = \texttt {largest divisor of } x$$$. Hence, we have to add elements to set greedily such that the largest divisor of $$$x$$$ $$$\forall$$$ $$$x$$$ $$$\epsilon$$$ $$$M$$$ is minimized. For this, we can find out the largest divisor, $$$d$$$ $$$\forall$$$ $$$i$$$ $$$\epsilon$$$ $$$[2..n]$$$, and store it in a list $$$v$$$. The answer is the sorted list $$$v$$$.

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

In Second problem In this test case may be editorial's answer is wrong in

1

4

0 2 4 6

0 2 8 10

I have mad b from a. Like, 3rd element of b is made from 2 time add 2nd element of a, etc. I have also checked others code some of them gave answer as Yes, Other gave No .

If I am wrong, please reply me.

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

Can someone explain why in problem A, for testcase 2 BWB WBW BWB is not an accepted solution

https://mirror.codeforces.com/contest/1333/submission/75973141

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

Here is how I up-solved D. Spoiler, it is long to be newbie friendly.

Note: if you are getting TLE in this problem. Use '\n' instead of endl to enabled output buffering because endl will flush the output. You don't want to flush the output everytime you need to output a newline because that means doing IO. The TL is very tight.

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

The problem A says that B should be equal to W+1 but in the tutorial number of W is always 1 then how it is a good coloring what am i missing??Please help..

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

What is the time complexity of this solution for problem C, by tmwilliamlin168?

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

Can anyone help me to understand error in my code for (133C) my code

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

Explain C in simple language and example.

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

    In C we were asked to count number of subarrays that were good (i.e their subarrays must not have zero sum)

    Now few observations before starting : 1. If a subarry is good that means all of 'its' subarrays are also good. 2. If a subarray is not good then all subarrays which will contain this subarray is also not good. example: 1 -1 3 4 now sum(1 -1) = 0 so it is a bad subarray so if you include (1 -1 3) this is also bad for same reason and so is (1 -1 3 4)

    now main problem is to find subarrays with 0 sum in efficient manner for which we use idea of prefix sum. what we try to do is find sum of all elements coming before it. for example 1 2 3 4 is the array then it's prefix sum is 1 3 6 10. now we can say that there exist a subarray with 0 sum if : 1. prefix itself is 0. 2. prefix in some index is seen before.

    let's say 1 2 -3 4 -1 it's prefix sum is 1 3 0 4 3 so there are two cases where subarray is 0 one is (1 2 -3) as prefix index was 0 here and second is (-3 4 -1) as prefix 3 in last index was seen before in 3rd index. we can use maps to find subarray with 0 sum to efficiently. Hope that helps.

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

I'm a bit confused right now. So you advise us to use set/map instead of unordered_map/unordered_set so we don't get TLE, because some "adorable community colleagues" added test cases to make it non-viable.

Does this recommendation stand only for this particular problem or we will have to use it from now on? Won't we get TLE on other problems for using an ordered data structure when it isn't generally needed?

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

I solved C using recursion. 75900316

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

    can u explain your code

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

      Basically we can't include 2 indices the difference of whose prefix sums is 0. Let's say during the first call of the function we find difference of prefix sum of indices 4 and 11 is 0. So we return f(first_index, 10) + f(5, last_index).So that these two indices are not considered together. Here first_index and last_index are the values for that particular call of the function intially the function is called as f(0, n-1) where n is the length of the string.

      Note that we also have to substract f(5, 10) as it has been counted twice. The value the function returns is n(n+1)/2 where n is the number of included elements.

      The code is a bit untidy as I submitted it in a hurry during the contest . Hope this helps :)

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

In this tutorial,for problem C,Isn't the loop is amortize?Why not complexity is O(n+n)? Please anyone explain.

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

include <bits/stdc++.h>

using namespace std;

long long n; map<long long,long long> ls;

int main() { long long i,k,sm=0,mx=0,z=0;

scanf("%lld",&n);
ls[0]=1;
for(i=1;i<=n;i++)
{
    scanf("%lld",&k);
    sm+=k;
    mx=max(mx,ls[sm]);
    z+=i-mx;
    ls[sm]=i+1;
}
printf("%lld\n",z);

}

This works for problem C but what I don't understand is why do we have

mx=max(mx,ls[sm]); Is this not equivalent to if(ls.find(sm)!=ls.end())mx = ls[sm];

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

In problem C what should be the output of 1 2 0? for me it should be output-> 5 (1),(2),(1,2)(1,2,0),(2,0) but editorial solution gives 3

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

Plz, write editorial in a more descriptive way. Write by assuming we don't know, not just write by assuming you are revising something, although this is for only last question's editorial.

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

Немного другое решение задачи C: Давайте для начала посчитаем префиксные суммы $$$p_i=a_0+a_1+...+a_i$$$, после чего заполним массив $$$next$$$, где $$$next_i=j$$$, такое, что $$$p_i=p_j, i \lt j$$$. Что нам остаётся сделать? Для каждого индекса найти самый длинный непрерывный подмассив, который содержит только различные $$$p_i$$$. Более формально, надо проитерироваться:

j = i + 1, mx = next[i];
while (j < n && j < mx) mx = max(mx, next[j]), j++;

Тогда отрезок $$$i .. j-1$$$ и будет являться ответом. Грубо говоря, мы просто ищем суффиксный минимум для каждого $$$i$$$: $$$s_i=min(next_i, next_{i+1}, ..., next_{n-1})$$$. Тогда $$$s_i-1$$$ будет правой границей нашего отрезка. Добавим длину отрезка в ответ. Готово. Решение: 76041511

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

I tried to implement C before check author's code, but my implementation is wrong, and I don't see difference between my and author's code, can u help me find where is my solution going wrong

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

Hello, I got WA on pretest 9 for problem Div2C. Can anyone please tell me what is pretest 9? My submission. Any help will be greatly appreciated.

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

Can anyone please explain the editorial of C, specially the first solution with O(n^2 logn) solution.Here it is said that if a subarray with [ai.....aj] is good then [ai....aj-1] is also good, i'm not clear how it works.if i take an array of {1, 2, -3, 1, 0} then a subarray {1, 2, -3, 1} is good but {1, 2, -3(aj-1)} is not good, or i misunderstood ? Plz help anyone

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

can anyone explain problem A?

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

    Take an NxM board, it is allowed to color a cell as black or white. If a black cell has atleast one adjacent white cell, lets increment variable B. Same holds for W.

    The task asks to color such that, B = W + 1. Now, the immediate way that strikes is to color like a chess board. This has one corner case to handle: when value of NxM is even, then we have B == W. So, we have to color one more white spot as black, preferably the position: 0,0 [Since I've assumed coloring all odd sum cells as black. My soln. for reference: link

    The editorial provides a much simpler implementation, as in, color only the top-left cell as white and the rest as black. Why is this correct? Well, we have W = 1, since there's only one white cell, also, it has 2 neighboring black cells. We also have B = 2 always, this is because only two cells: (0,1) and (1,0) have neighboring white cell (other blacks only have black neighbors). Thus, the constraint: W = B + 1, always holds. I would recommend visualising/drawing out and seeing.

    Bonus: As the editorial mentions, the problem gets a little tricky if 1 <= N,M! I realised why only after typing out this explanation.

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

Can someone explain eugene one. I am unable to get it. I was relating this to count arrays with sum zero and using that approach. and then subtracting n(n+1)/2-count.

But I am not able to figure out how to include in count those subarrays of the given array whose subarrays are also not good.

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

The spiral path idea in E is so nice!!

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

For given constraits we do not need a sieve in problem F. We can find smallest prime divisor with naive brute force and get AC, 187 ms

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

Just want to suggest that an O(n) solution of problem C is possible.

Python solution: http://mirror.codeforces.com/contest/1333/submission/76077853

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

PuRpLe_FoReVeR In your solutions for 1333D - Досуг в школе №41 there is a small mistake in computing mini(minimum possible k).

It's giving wrong answer for the case-

6 2

RLLRRR

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

Auto comment: topic has been updated by PuRpLe_FoReVeR (previous revision, new revision, compare).

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

Hello, Can someone please explain C question duplicate prefix sum part of the tutorial? I didn't understand why that should be true?

Thanks in advance!

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

    just a simple thing you have to maintain prefix array and then if any value repeats that show that the sum of elements including this indices is zero than you can easily calculate how much subarrays contains this subarrays after counting all subarrays which contain subarray with sum=0 subtract it from total number of subarrays that can be found by n*(n+1)/2

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

can anyone help me please, actually I think my code doesn't have a mistake but it fails on the test case 3 how can I improve this code https://mirror.codeforces.com/contest/1333/submission/76197328

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

can anyone identify what is wrong with my logic for div2 problem B- https://mirror.codeforces.com/contest/1333/submission/76225752

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

To make the code shorter for 1333E, I filled the spiral differently. https://mirror.codeforces.com/contest/1333/submission/77185747

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

I believe this is an O(N^2) solution: submission

But i'm getting TLE on 1333D - Досуг в школе №41 on test case number 10. Can anyone help me?

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

My solution 77420751 for problem C uses the idea of two pointers. Broadly we can keep two pointers in the array denoting the start and endpoint of a valid subarray and iterate them to get the number of good subarrays. First, we store prefix sums in an array then we iterate the endpoint from 1 to an index such that the sum hasn't been seen before. The number of segments is end-pt- start-pt+1. Once we encounter an already found sum, we know that sum from startpt+1 to end-pt is 0, and any subarray starting before or equal to startpt+1 and ending after end-pt will contain this subarray. We thus increment our start-pt by 2 and repeat this process till we cover all array. We need to keep care of the fact that start-pt never decreases because this would mean over counting.

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

    hey ... in your submission .... can you pls explain these lines: else { if(ind1<ma[sum[ind2]]+2) ind1=ma[sum[ind2]]+2; if(ind1>ind2) { ma[sum[ind2]]=ind2; ind2++; continue; } ans+=ind2-ind1+1; ma[sum[ind2]]=ind2; } i hope you will answer.... badly in need of that

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

      Suppose you find a sum at some index j which you have already seen at some index i, this means that sum from i+1 to j (inclusive) is 0. Thus, you need to increase your pointer1 (ind1) (denotes the starting point of subarray in consideration) to i+2, as pointer2 (ind2) is at j and sum from i+1 to j is 0, so we need to go to i+2. The map stores the latest index at which this sum has been obtained. So, if ind1>ind2 by increase, we just assign map[sum[ind2]]=ind2 and continue with our loop. There will be no effect on ans in this case.

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

Can someone please tell me why this solution exceeds time limit? 77867414

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

Can somebody help me out for ques C why this is getting wrong answer on test 8? https://mirror.codeforces.com/contest/1333/submission/78603617 is link to my submission. Thanks in advance.

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

I m Getting wrong answer at test 7 of problem D. Link to my submission https://mirror.codeforces.com/contest/1333/submission/78657016 Somebody plz tell me my mistake. Thanks in advance.

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

In Editorial of Problem D

Can anyone just explain this statement ,that we came to the point that we should now flip (U-k+1) pairs

Otherwise, we roll back to the previous iteration and use U−k+1 pairs in this move

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

7 0 0 1 2 0 -2 -1 In problem C I think monotonicity part is wrong. Consider above test case ( 0 based indexing in array) for which editorial's R = { 0 5 5 4 6 6 6} which is monotonic. Please correct me if I am wrong. Thank You.

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

PuRpLe_FoReVeR можешь пожалуйста объяснить почему мы можем сортировать массив D и первые k элементов будет ответом для I[k]?

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

I did problem F using linear sieve and some observations in $$$O(NlogN)$$$.
code: 210753600