# | User | Rating |
---|---|---|

1 | tourist | 3539 |

2 | V--o_o--V | 3338 |

3 | Um_nik | 3307 |

4 | Petr | 3297 |

5 | ecnerwala | 3282 |

6 | LHiC | 3266 |

7 | wxhtxdy | 3264 |

8 | Radewoosh | 3257 |

9 | Vn_nV | 3182 |

10 | dotorya | 3178 |

# | User | Contrib. |
---|---|---|

1 | Radewoosh | 207 |

2 | Errichto | 177 |

3 | neal | 159 |

4 | Ashishgup | 158 |

5 | PikMike | 157 |

6 | majk | 156 |

6 | Petr | 156 |

8 | rng_58 | 155 |

9 | Um_nik | 154 |

10 | 300iq | 153 |

Codeforces (c) Copyright 2010-2018 Mike Mirzayanov

The only programming contests Web 2.0 platform

Server time: Jan/24/2019 06:36:48 (d1).

Desktop version, switch to mobile version.

Supported by

User lists

Name |
---|

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?

Yes that works but it's just easier to code if you sort 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

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

Thanks it's edited now

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?

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

Auto comment: topic has been updated by NotMoreThanANoob (previous revision, new revision, compare).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)

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

still not clear

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

thanks, understood.

This contest was organized well.. well done!

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

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

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

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

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

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

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

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

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.

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).

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.

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

Sure. Say that we end in the state (

a_{1}-b_{1},a_{2}-b_{2}, ...,a_{n}-b_{n}), obviously with Then one can check by induction thatregardlessof the path taken to get there, we will haveTherefore, it suffices to compute

Note that in the last sum, this is precisely the [

x^{k}] coefficient of the following generating function:Now multiply out the last polynomial: say that

This is

O(n^{2}) time to do, or with FFT (unnecessary here). Then we can compute that the [x^{k}] coefficient of the last expression (by expanding oute^{nx}) isPutting this all together gives that the answer is

Very beautiful solution!

damn, this is beautiful

It's solution without generating function is really nice!

:D

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.

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

a_{i}- 1 - 1 - 1... - 1 - 1). So in every bracket we have to decide what will we take:a_{i}or - 1. Let's assume that we will take - 1ptimes. We will have to take multiply of somen- 1 -pnumbers 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 isk* (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 -pother - 1s can go anywhere, son^{k - 1 - p}.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.

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?

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.thanks for the explanation, but can you suggest me,how to approach these type of problems. It will be beneficial for future contests XD

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

I finally understand. Thank you so much.

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

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.

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

Explicitly, I don't know.

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

Lol thanks, it worked :D

Haha welcome, Congrats :D

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

I did the following in B:

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

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

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.

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

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

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

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.

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 ! :)

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

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

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.

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?

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

In A Div.1/C Div.2 you make a little mistake, i think the result should be "(

R-L) + (n- 1)"In div2c, what if

`n<=10^5 and a[i]<=10^6`

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

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

A brief explanation please, thanks!.

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.

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

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.

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

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

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

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.

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

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-->> ~~~~~

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

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.

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.

Thanks now I got 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?

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.

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!

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?

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.

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; }

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.

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

Reason is here.

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.

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

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.

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.Is 891A(Pride) solvable in O(n) ? Though it does not matter. Any explanation is appreciated.

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?

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.

I am having the same doubt.

Good Contest

Good Contest

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

http://codeforces.com/blog/entry/56000#comment-397225

Hope this helps.

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

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.

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?

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

Size of the set should be < n

thanks bro got it!

My answer to A is wrong. Please help me.

## include<bits/stdc++.h>

using namespace std; int n,s; vectora,b; int main(){ cin>>n; int x; for(int i=0;i<n;i++) cin>>x,a.push_back(x),s+=a[i]; for(int i=0;i<n;i++) cin>>x,b.push_back(x); sort(b.begin(),b.end()); reverse(b.begin(),b.end()); if(s<=b[0]+b[1]) cout<<"YES"<<endl; else cout<<"NO"<<endl; return 0; }