Mahdi_Jfri's blog

By Mahdi_Jfri, 7 years ago, In English
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...
  • Vote: I like it
  • +58
  • Vote: I do not like it

| Write comment?
»
7 years ago, # |
Rev. 9   Vote: I like it +5 Vote: I do not like it

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 years ago, # |
  Vote: I like it +1 Vote: I do not like it

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

  • »
    »
    7 years ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    Thanks it's edited now

    • »
      »
      »
      7 years ago, # ^ |
        Vote: I like it 0 Vote: I do not like it

      could you explain to me Div2-D problem Input 5 1 3 4 5 2 Participant's output 3 4 5 2 1

      why WA?

      • »
        »
        »
        »
        7 years ago, # ^ |
          Vote: I like it 0 Vote: I do not like it

        Consider the index subset [1,4] The sum for the given array is 3+2=5 The sum for your array is 4+1=5

»
7 years ago, # |
  Vote: I like it 0 Vote: I do not like it

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

»
7 years ago, # |
Rev. 5   Vote: I like it 0 Vote: I do not like it

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 years ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    Run second loop(nested one) from i. Not i+1.

    • »
      »
      »
      7 years ago, # ^ |
        Vote: I like it 0 Vote: I do not like it

      still not clear

      • »
        »
        »
        »
        7 years ago, # ^ |
          Vote: I like it 0 Vote: I do not like it

        If you can find the minimum length where gcd is 1 you can make a 1 in len-1 move. As now you find a 1 in the array you can make whole array 1 in n-1 move..total move n-1+len-1

»
7 years ago, # |
  Vote: I like it +1 Vote: I do not like it

This contest was organized well.. well done!

»
7 years ago, # |
  Vote: I like it +1 Vote: I do not like it

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 years ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    we could solve in n^2*log(n) by tutorial method

  • »
    »
    7 years ago, # ^ |
      Vote: I like it +6 Vote: I do not like it

    We can solve this in nlog^2(n) with sparse table + binary search or segment tree.

»
7 years ago, # |
  Vote: I like it +9 Vote: I do not like it

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

»
7 years ago, # |
  Vote: I like it 0 Vote: I do not like it

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

  • »
    »
    7 years ago, # ^ |
    Rev. 2   Vote: I like it 0 Vote: I do not like it

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

    • »
      »
      »
      7 years ago, # ^ |
        Vote: I like it 0 Vote: I do not like it

      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 years ago, # ^ |
          Vote: I like it +4 Vote: I do not like it

        (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 years ago, # ^ |
            Vote: I like it 0 Vote: I do not like it

          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 years ago, # ^ |
              Vote: I like it 0 Vote: I do not like it

            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 years ago, # ^ |
            Rev. 2   Vote: I like it 0 Vote: I do not like it

            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 years ago, # |
  Vote: I like it +6 Vote: I do not like it

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

  • »
    »
    7 years ago, # ^ |
    Rev. 2   Vote: I like it +129 Vote: I do not like it

    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 years ago, # ^ |
    Rev. 2   Vote: I like it +5 Vote: I do not like it

    It's solution without generating function is really nice!

    :D

    • »
      »
      »
      7 years ago, # ^ |
        Vote: I like it +5 Vote: I do not like it

      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 years ago, # ^ |
          Vote: I like it +25 Vote: I do not like it

        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 years ago, # ^ |
            Vote: I like it 0 Vote: I do not like it

          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 years ago, # |
  Vote: I like it +4 Vote: I do not like it

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 years ago, # ^ |
      Vote: I like it +20 Vote: I do not like it

    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 years ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    I finally understand. Thank you so much.

»
7 years ago, # |
  Vote: I like it 0 Vote: I do not like it

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

  • »
    »
    7 years ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    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 years ago, # ^ |
        Vote: I like it +1 Vote: I do not like it

      What do those do? Is having cout.flush() at the end enough?

      • »
        »
        »
        »
        7 years ago, # ^ |
          Vote: I like it +3 Vote: I do not like it

        Explicitly, I don't know.

        What I know is that those three instructions become useful for fast I/O when they come together :D.

»
7 years ago, # |
Rev. 2   Vote: I like it 0 Vote: I do not like it

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

»
7 years ago, # |
Rev. 2   Vote: I like it +27 Vote: I do not like it

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

  • »
    »
    7 years ago, # ^ |
    Rev. 4   Vote: I like it +17 Vote: I do not like it

    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 years ago, # ^ |
      Rev. 2   Vote: I like it +5 Vote: I do not like it

      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 years ago, # ^ |
        Rev. 7   Vote: I like it 0 Vote: I do not like it

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

»
7 years ago, # |
  Vote: I like it +5 Vote: I do not like it

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

»
7 years ago, # |
  Vote: I like it 0 Vote: I do not like it

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

  • »
    »
    7 years ago, # ^ |
      Vote: I like it +4 Vote: I do not like it

    We can solve using segment tree with binary search in O(nlogn*logn). And also we can solve it in O(nlogn).

  • »
    »
    7 years ago, # ^ |
      Vote: I like it +3 Vote: I do not like it

    actually the original problem was with limitation n < 1e5 and a[i] < 1e9 but we decided to change it. You can see O(nlogn) solution here by omidazadi

»
7 years ago, # |
  Vote: I like it +36 Vote: I do not like it

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

»
7 years ago, # |
  Vote: I like it +9 Vote: I do not like it

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 years ago, # |
  Vote: I like it 0 Vote: I do not like it

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

»
7 years ago, # |
Rev. 2   Vote: I like it 0 Vote: I do not like it

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

»
7 years ago, # |
  Vote: I like it 0 Vote: I do not like it

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

»
7 years ago, # |
  Vote: I like it 0 Vote: I do not like it

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 years ago, # |
  Vote: I like it 0 Vote: I do not like it

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 years ago, # ^ |
      Vote: I like it +1 Vote: I do not like it

    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 years ago, # |
  Vote: I like it +15 Vote: I do not like it

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 years ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    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 years ago, # ^ |
        Vote: I like it 0 Vote: I do not like it

      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 years ago, # ^ |
          Vote: I like it 0 Vote: I do not like it

        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 years ago, # |
  Vote: I like it +4 Vote: I do not like it

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 years ago, # |
  Vote: I like it +7 Vote: I do not like it

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 years ago, # ^ |
      Vote: I like it +1 Vote: I do not like it

    Reason is here.

    • »
      »
      »
      7 years ago, # ^ |
        Vote: I like it +11 Vote: I do not like it

      Ah, that kinda makes sense. Interesting to see problems in which checking the correctness of an answer is (exponentially) harder than generating a correct answer.

»
7 years ago, # |
  Vote: I like it 0 Vote: I do not like it

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

»
7 years ago, # |
  Vote: I like it 0 Vote: I do not like it

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 years ago, # |
  Vote: I like it 0 Vote: I do not like it

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 years ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    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 years ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    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 years ago, # |
  Vote: I like it 0 Vote: I do not like it

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

»
7 years ago, # |
  Vote: I like it 0 Vote: I do not like it

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 years ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    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 years ago, # |
  Vote: I like it 0 Vote: I do not like it

Sorry for my bad English. I solved the problem 891C - Зависть 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 years ago, # |
  Vote: I like it 0 Vote: I do not like it

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

»
5 years ago, # |
Rev. 2   Vote: I like it 0 Vote: I do not like it

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 years ago, # |
  Vote: I like it 0 Vote: I do not like it

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 years ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    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 years ago, # |
Rev. 2   Vote: I like it -13 Vote: I do not like it

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. :(