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

Автор Mahdi_Jfri, 7 лет назад, По-английски
Tutorial is loading...
Tutorial is loading...
Tutorial is loading...
Tutorial is loading...
Tutorial is loading...
Tutorial is loading...

also there's another solution using dp by KAN : 32407269

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

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

Instead of sorting and finding the biggest two, isn't it faster to just find the biggest two directly?

Correct me if I'm wrong..

maxCapacity[2];

maxCapacity[0]=maxCapacity[1]=INT_MIN

if(capacity>=maxCapacity[0]){

maxCapacity[1]=maxCapacity[0]

maxCapacity[0]=capacity

}

else if(maxCapacity[1]<capacity)

maxCapacity[1]=capacity

this should work right?

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

you wrote solution of C/Div2 and A/Div1 separately !

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

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

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

if we follow editorial then answer for 6,10,15 is 5 but the answer is actually 4, where am i understanding wrong, can someone plz explain.(div2 C)

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

This contest was organized well.. well done!

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

A Div1 can be solved with dp in O(nsqrt(max(ai) + n^2 * log^3(n)) however its almost impossible to do this much operations so my solution is pretty fast 32408288

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

Thank You to all the authors and to the CF team. Great contest! I enjoyed it. :D

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

someone plz explain div2 C in detail.. with possibly proof..

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

    Which part do you not understand? When cnt1>0 (as given in the editorial) or in the other case?

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

      other case i.e how we get can make the elements 1 in R-L+1+n-1 turns and can you plz explain with example like in case when we have 3 elements i.e. 6 10 15

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

        (Considering that none of the elements are one in the given array)

        As soon as we are able to make any of the array elements to 1 the answer will be the steps till now + n-1 as now we can make all of the elements to 1.

        So if we have a case in which there are 2 consecutive elements such that their gcd is 1 then the answer will be n (one move for obtaining 1 and n-1 moves for the rest).

        If we have no such pairs then we look for triplets. In this case the answer will be 2+n-1=n+1.

        Just extend this and you'll get what the author is trying to explain.

        For your case gcd(6,10)=2 and gcd(10,15)=5 but gcd(6,10,15)=1 so answer is n+1=4

        Hope this helps :)

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

          Hello, I understand what the algorithm is and why it works, but I don't understand how the editorial writer goes about checking the pairs and triples. They basically say to evaluate gcd(L, R) and keep increasing R? How does that work, doesn't that mean you miss pairs at the end? I am probably misunderstanding something.

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

            What the editorial says is that we can fix a L (we do this n times) and by iterating through all the values of R(takes n steps so complexity=n*n) we can find the "smallest subarray" with GCD=1. (Which is what we need to do).

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

            Let's call F(L, R) the gcd of the subarray between L and R, ok?

            What is happening is that we can calculate F(L, R) recursively:

            F(L, R) = V[L], if L == R or GCD(F(L, R-1), V[R]), if L != R

            Now, look at this: --> if F(L, R) = 1, then for every R' >= R, F(L, R') = 1

            About the problem: the smartest thing to do is get a 1, as fast as possible, so that we can "carry" this 1 for all the other positions. So, if F(L, R) = 1, we can get a 1, by doing V[L+1] = gcd(V[L], V[L+1]), then V[L+2] = gcd(V[L+1], V[L+2]) and so on, until reaching V[R] = 1.

            Now we want to find the minimum R-L+1 that F(L, R) = 1 to get the answer, and we can brute force at each possible L to find the first R that solves the problem.

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

Can someone provide solution for Div1/E using generation functions?

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

    Sure. Say that we end in the state (a1 - b1, a2 - b2, ..., an - bn), obviously with Then one can check by induction that regardless of the path taken to get there, we will have

    Therefore, it suffices to compute

    Note that in the last sum, this is precisely the [xk] coefficient of the following generating function:

    Now multiply out the last polynomial: say that

    This is O(n2) time to do, or with FFT (unnecessary here). Then we can compute that the [xk] coefficient of the last expression (by expanding out enx) is

    Putting this all together gives that the answer is

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

    It's solution without generating function is really nice!

    :D

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

      How do you solve it without generating functions? I think the most beautiful part is that the sum is the same regardless of the path. I can't believe I didn't see it. It's a really nice problem anyway.

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

        I've solved it without generating functions, just with some combinatorics. Everytime, when we increase res, we add to it multiply of things like this: (ai - 1 - 1 - 1... - 1 - 1). So in every bracket we have to decide what will we take: ai or  - 1. Let's assume that we will take  - 1 p times. We will have to take multiply of some n - 1 - p numbers from input, here goes dp. So we have p+1 empty places, and we should put  - 1 in them (p + 1, cause we must calculate result while decreasing some element). Number of ways to choose them is k * (k - 1) * (k - 2)...(k - p). But we have to decide from which  - 1 will we be decreasing while increasing res this time. It appears, that we can do it in exactly one way, cause we should choose the latest  - 1 (cause all other  - 1 must be already added to numbers). k - 1 - p other  - 1s can go anywhere, so nk - 1 - p.

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

          it is the of essence generating functions.It’s just different way to get the answer.you use Dp,and tutorial use a mathematical method.

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

Could anyone explain me the solution to div2 D. I can't seem to understand why this always works. For 4 4 1 2 3 wouldn't the output be 4 1 2 3?

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

    It doesn't mean sort and then shift, even though that what it says. It means: for a sorted array we can just cyclic shift. For an un-sorted array, we do the same thing: replace the i-th smallest element with the (i + 1)-th smallest, and replace the largest with the smallest.

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

    I finally understand. Thank you so much.

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

Can anyone tell me why my (supposedly) nlogn solution for E keeps getting TLE? http://codeforces.net/contest/892/submission/32410117

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

    In the begining of main function you put "ios::sync_with_stdio(false);" without "cin.tie(0); cout.tie(0);".. This may be the reason.

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

I am unable to understand the solution for 892-B Wrath. Can anybody please explain?

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

Can someone explain Div 2E / Div 1C solution a bit more detail ?

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

    I can explain my online solution — it would be a good way to verify if it's correct.

    An edge is in some MST if and only if it is not the heaviest edge in every cycle. To check that I used Kruskal. I process all the edges with weight X at the same time. If an edge connects 2 vertices which are already in the same component, it is the heaviest in the cycle. Otherwise it will not be the heaviest in every cycle as we constructed everything we could so far, using edges with weight < X and these vertices are still in separate components. It means that every cycle in which that edge can exist would have edges of weight >= X.

    The following part has been modified — not fully verified:

    In addition to that, not all of these edges with weight X can exist within the same MST but they can exist separately in some MST's. The problem is that they can create a cycle, when we include cheaper edges. In order to detect such edges we can remember the state of each vertex (the id of components of both vertices when we process an edge in Kruskal).

    Now I process queries — I check that every edge can exist in some MST. If that's the case, there is one more thing to check: that these edges do not form cycles — I just run another Kruskal on them, using remembered component numbers.

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

      I understand your idea! But could you explain more the way you create graph G'? I mean how you do it? For example, I have some edges: ~~~~~ (1 2 1) (1 3 1) (3 4 1) (2 5 3) (5 6 3) (6 4 3) ~~~~~ Then, all the egdes with weigt 3(2-5,5-6,6-4) must be in graph G' right?

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

        Thanks for that question, after more thinking I found a bug in my solution. I provided a test which fails it above.

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

In A Div.1/C Div.2 you make a little mistake, i think the result should be "(R - L) + (n - 1)"

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

In div2c, what if n<=10^5 and a[i]<=10^6?

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

Thanks for the detailed explanation of Div-1 C. Hope your fingers didn't start cramping after writing all this.

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

Div1-C says "It is guaranteed that the sum of ki for 1 ≤ i ≤ q does not exceed 5·10^5", but test case #58 seems to violate this rule.

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

Can someone prove the validity of the solution of the 891C-Envy question?

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

In DIV1-C "If ans only if", I think it may be "If and only if"?

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

can someone explain the DIV 2-E in detail as I am not getting the tutorial solution

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

Can you tell me how to come up with solution for div 2 D? I have read the editorial, understood how it works but don't know how to deal with same problems.

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

Can someone explain in Div 2 problem B — Wrath how to maintain the min(j — Lj) for i + 1 <= j <= n in O(n) ?

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

    You can do that without additional memory. Iterate line from the end and maintain killer index. Set index to current person if current person's claw extends further than current killer's claw. See this submission for implementation example.

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

I still not clear about the problem DIV2 E/ DIV1 C.

Why is "_It can be proven that there's a MST containing these edges if ans only if there are MSTs that contain edges with same weight_" and "_if we remove all edges with weight greater than or equal to X, and consider each connected component of this graph as a vertex, the edges given in query with weight X should form a cycle in this new graph_."

Can someone explain a little bit more detail about this?

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

    I can give you my understanding of that, consider edges with weight X in query, we remove all edges with weight greater than or equal to X in graph. In this new graph, we consider if the edges with weight X in query is "neccessary". If the edge connect two nodes which have already been connected, we called this edge "unnecessary", if there is an "unnecessary" edge in query, the answer will be NO.

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

      Oh now I got it! For the edges with weight i in each query we need to check all of them to see if they form a cycle or not. Of course we need to process weight i in non-decreasing order and eliminate all remaining edges that have weight >= i.

      Thank you very much!

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

        can u please explain me a bit more! why we are considering the edges which have weight = 'i' ? why we are not checking the edges with weight < i?

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

Can anyone explain the solution idea of problem 891C — Envy (Div 2 — E / Div 1 — C) ? I have read the editorial but I can't understand anything about the idea. Please help.

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

Soooo Div1 B could be solved in O(N * log N). How come N was only 22? This really bamboozled me during the contest :D

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

can anyone please explain me DIV2-D a bit more ? I didn't understand it completely !

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

In the editorial of PRIDE, should n't the answer be (R - L) + (n - 1) instead of (R - L + 1) + (n - 1) as in (R-L) steps. We can get a 1 in the segment by taking gcd of consecutive numbers.

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

For 891B — Gluttony What if the input array is already in the form of sorted then shifted by one? Wouldn't it be the same as the submitted array, leading to WA?

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

    Agree with you (unless we wrongly understood what was mentioned in the editorial). In fact I tried to implement it the way described in the editorial, but it does not pass the tests.

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

    When you sort the input array 'a' you'll have to keep track of indices where the elements were originally present in 'a'. For this, you can use a vector of pairs having first and second 'a[i]' and 'i'.

    We shift so that all the indices (except one) the newly formed array(after sorting and shifting) will have elements greater than 'a'.

    The sorted vector 'v' from first para will help to arrange in this manner. the 'ith' element of v (i.e. v[i].first )would be placed at the position where the i-1th element was present (i.e. v[i-1].second).

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

Please explain Div. 1 C , unable to comprehend from editorial.

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

could anyone explain to me Div2-D and why Input 5 1 3 4 5 2 Participant's output 3 4 5 2 1 is WA

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

    The same problem was here: there are two sets of indexes which their corresponding elements have equal sums. S1 = {2,5}, S2 = {3,5}. But I still haven't the accepted solution.

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

Sorry for my bad English. I solved the problem 891C - Envy offline, but i think that maybe this cant be solved online, whit a persistent union find. My idea is run Kruskal Algorithm and save for the last edge of every weight the estate(persistent) of the union find and for each query sort it by weight and for each "group" of the same weight in the query try to use when the union find only contains edges of weight lower that the actual weight(simulate the Kruskal Algorithm), if every edge is used in the union find then the answer is "YES" I try to implement this with persistent array(segment tree on my case) and get MLE on test 16: 35112572 and try to use garbage collector and get TLE again, someone know a good implementation of persistent union find?

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

can anyone help? 1 10000000 why answer is not -1 in div2 D. Gluttony

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

In the solution to 891C-Envy, it says the following:

"So for each query we need to check if the edges with weight X have a MST"

How exactly is X defined? I don't quite understand the solution. Any help / pointers will be appreciated.

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

I don't understand DIV1 B solution, what if that sorted and shifted array is in the input then it's wrong. Can anyone explain it to me a bit?

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

    If you're given an array of unique elements like $$$[1,2,3,4]$$$ then it's easy to see that a shifted array like $$$[2,3,4,1]$$$ would work. So if you're given an unordered array, do the same value shifting as if it were sorted, except the indices are not necessarily sorted.

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

why should we make the test data(#58) of Div.1 C so big? I got TLE using cin and ios::sync_with_stdio(false) but AC with scanf. :(