NotMoreThanANoob's blog

By NotMoreThanANoob, 3 weeks 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  

»
3 weeks 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?

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

    Yes that works but it's just easier to code if you sort it.

    • »
      »
      »
      3 weeks ago, # ^ |
        Vote: I like it 0 Vote: I do not like it

      Kaneki_04 methode : O(n) NotMoreThanANoob methode (n*logn) if you use quicksort/sort from alogirthm etc; They could have made the time limit shorter so you cant sort and get max points, but it works both ways

»
3 weeks ago, # |
  Vote: I like it +1 Vote: I do not like it

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

  • »
    »
    3 weeks ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    Thanks it's edited now

    • »
      »
      »
      14 hours 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?

      • »
        »
        »
        »
        8 hours 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

»
3 weeks ago, # |
  Vote: I like it 0 Vote: I do not like it

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

»
3 weeks 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)

  • »
    »
    3 weeks ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

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

    • »
      »
      »
      3 weeks ago, # ^ |
        Vote: I like it 0 Vote: I do not like it

      still not clear

      • »
        »
        »
        »
        3 weeks 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

»
3 weeks ago, # |
  Vote: I like it +1 Vote: I do not like it

This contest was organized well.. well done!

»
3 weeks 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

  • »
    »
    3 weeks ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

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

  • »
    »
    3 weeks 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.

»
3 weeks 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

»
3 weeks ago, # |
  Vote: I like it 0 Vote: I do not like it

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

  • »
    »
    3 weeks 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?

    • »
      »
      »
      3 weeks 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

      • »
        »
        »
        »
        3 weeks 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 :)

        • »
          »
          »
          »
          »
          3 weeks 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.

          • »
            »
            »
            »
            »
            »
            3 weeks 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).

          • »
            »
            »
            »
            »
            »
            3 weeks 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.

»
3 weeks ago, # |
  Vote: I like it +6 Vote: I do not like it

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

  • »
    »
    3 weeks 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

  • »
    »
    3 weeks ago, # ^ |
    Rev. 2   Vote: I like it +5 Vote: I do not like it

    It's solution without generating function is really nice!

    :D

    • »
      »
      »
      3 weeks 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.

      • »
        »
        »
        »
        3 weeks 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.

»
3 weeks 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?

  • »
    »
    3 weeks 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.

    • »
      »
      »
      3 weeks ago, # ^ |
        Vote: I like it 0 Vote: I do not like it

      thanks for the explanation, but can you suggest me,how to approach these type of problems. It will be beneficial for future contests XD

  • »
    »
    3 weeks ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    a[1] = b[1] summations should differ

  • »
    »
    3 weeks ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    I finally understand. Thank you so much.

»
3 weeks 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://codeforces.com/contest/892/submission/32410117

  • »
    »
    3 weeks 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.

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

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

      • »
        »
        »
        »
        3 weeks 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.

»
3 weeks 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?

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

    I did the following in B:

    1. We know the last person in line will be alive no matter what, because they are attacking only people, who have lesser indexes;

    2. The amount of alive people (int alive) is n before the bell rings;

    3. We know, that Li people, who go right before the last one are guaranteed to be dead, let's denote that value as int kill.

    4. Starting from the last person, let's iterate over the array with j = n-1 to 0 like this:

    • if kill > 0 then alive--; kill--;
    • kill = max (kill, L[j])

    So, on each step we kill somebody, but also the amount of people decreases with each person, because we are getting farther and farther away from the killer. Also, we don't really care who the killer is, the most important is to maximize the length of the nail.

    32387222

»
3 weeks ago, # |
  Vote: I like it 0 Vote: I do not like it

892B-Warth why my code is getting TLE it is in O(n) http://codeforces.com/contest/892/submission/32400111

  • »
    »
    3 weeks ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    Use Faster i/o. Include Following Line Into Your Code: ios::synch with stdio(false); cin.tie(0); Else Use print&scanf for taking input.

  • »
    »
    3 weeks ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    bhpra : try fast I/O, since cin and cout is slow.. use these lines at beginning,- inside main() : ios_base::sync_with_stdio(false); cin.tie(NULL);

    hope it helps ! :)

    • »
      »
      »
      3 weeks ago, # ^ |
        Vote: I like it 0 Vote: I do not like it

      Thanks Till now i have use codechef for cp this was never a problem their

»
3 weeks 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 ?

  • »
    »
    3 weeks 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.

    • »
      »
      »
      3 weeks 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?

      • »
        »
        »
        »
        3 weeks 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.

»
3 weeks 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)"

»
3 weeks ago, # |
  Vote: I like it 0 Vote: I do not like it

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

  • »
    »
    3 weeks 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).

  • »
    »
    3 weeks 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

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

      A brief explanation please, thanks!.

      • »
        »
        »
        »
        3 weeks ago, # ^ |
          Vote: I like it 0 Vote: I do not like it

        you can solve it with binary search and sparse table..

        why binary search, if theres exist a segment with length = l,where gcd of that segment is 1, then it is true to say that there also exist a segment with length = l+1, l+2 ... whose gcd is 1.

        if you don't know about spare table, the HERE competitive programming handbook has a good explanation about it. check it.

»
3 weeks 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.

»
3 weeks 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.

»
3 weeks 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?

»
3 weeks 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"?

»
3 weeks 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

»
3 weeks 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.

»
3 weeks 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) ?

  • »
    »
    3 weeks ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    You can maintain two array start[] and end[] and initialise them with 0, and let diff = i-L[i], so whenever diff>0 && diff start[diff]++ and end[i-1]++. Now diff<=0 is also possible, in this case you do like this--> start[1]++ and end[i-1]++. Now in last you can see if there is any start left then ans++ else continue..

    Here is the code snippet-->> ~~~~~

    for(int i=1;i<=n;i++)
    {
        scanf("%lld",&L[i]);
        if(i==1 || L[i]==0)
           continue;
        ll diff = i-L[i];
        if(diff<=0)
        {
           st[1]++;
           en[i-1]++;
        }
        else if(diff>0 && diff<i)
        {
           st[diff]++;
            en[i-1]++;
        }
    }
    ll diff = 0;
    int ct=0;
    for(int i=1;i<=n;i++)
    {
        diff += st[i];
        if(diff>0)
        {
           flag[i]=0;
        }
        diff -= en[i];
        //cout<<flag[i]<<" ";
        if(flag[i]) ct++;
    }
        printf("%d",ct);
    

    ~~~~~ But i think we can also solve it without maintaining those two arrays and simply keeping variables, did not implemented though ;)

  • »
    »
    3 weeks ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    you can solve it in this way.. for each index i check if there exist a index j(j > i) such that i >= j-arr[j].

    this is same way as finding the next minimum element(j-arr[i]) for each i. you can use stacks for that.HERE.

    check geeksforgeeks tutorial HERE on how to find next greater or smallest element.

  • »
    »
    3 weeks 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.

»
3 weeks 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?

  • »
    »
    3 weeks 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.

    • »
      »
      »
      3 weeks 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!

      • »
        »
        »
        »
        3 weeks 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?

»
3 weeks 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.

»
3 weeks ago, # |
  Vote: I like it 0 Vote: I do not like it

according to above explaination my code should give correct answer but it is giving wrong answer. Anyone can explain plzz. This is my code

include <bits/stdc++.h>

using namespace std;

int main(){ ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); long t,num,res=0; cin >> t; vector b(t); for(int i=0;i<t;i++){ cin >> num; res+=num; } for(int i=0;i<t;i++){ cin >> b[i]; } sort(b.begin(),b.end()); long res2=b[t-1]+b[t-2]; if(res<=res2) cout << "YES" << endl; else cout << "NO" << endl; return 0; }

  • »
    »
    3 weeks ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    You need to select highest available value from vector b. - instead of selecting smallest values from vector b. Reverse the vector or select last 2 elements from vector b.

»
3 weeks 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

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

    Reason is here.

    • »
      »
      »
      3 weeks 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.

»
3 weeks 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 !

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

div 2 c can anyone help me in finding the error for the particular test case 37 where all the 2000 values are 1870 its working fine in my compiler but not in codeforces.

»
3 weeks 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.

»
2 weeks ago, # |
  Vote: I like it 0 Vote: I do not like it

Is 891A(Pride) solvable in O(n) ? Though it does not matter. Any explanation is appreciated.

»
2 weeks 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?

  • »
    »
    2 weeks 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.

»
2 weeks ago, # |
  Vote: I like it 0 Vote: I do not like it

Good Contest

»
2 weeks ago, # |
  Vote: I like it 0 Vote: I do not like it

Good Contest

»
2 days ago, # |
  Vote: I like it 0 Vote: I do not like it

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

»
14 hours 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