By Vladosiya, history, 3 months ago, translation, 1759A - Yes-Yes?

Idea: MikeMirzayanov

Tutorial
Solution

1759B - Lost Permutation

Idea: MikeMirzayanov

Tutorial
Solution

1759C - Thermostat

Tutorial
Solution

1759D - Make It Round

Idea: MikeMirzayanov

Tutorial
Solution

1759E - The Humanoid

Idea: Gornak40

Tutorial
Solution

1759F - All Possible Digits

Idea: DmitriyOwlet

Tutorial
Solution

1759G - Restore the Permutation

Idea: MikeMirzayanov

Tutorial
Solution Tutorial of Codeforces Round #834 (Div. 3) Comments (35)
 » 3 months ago, # | ← Rev. 2 →   I solved A by checking if there is a letter that is not in the word "Yes", and checking that each Y has s before it, each e has Y before it, and each s has e before it. Also I solved D using lcm, for each i such that 1<=i<=18 check if lcm(n,10^i)/n<=m. If it is, multiply the answer by m/answer
 » 3 months ago, # | ← Rev. 8 →   Great round! Hope to see another Div.3 soon. Div.3 almost always meant +ve delta for me, so I am always hyped for them.To point out, F didn't need a very complex data structure. A single set (a sorted set would be preferrableI just checked, an unordered set worked equally as fast) was enough. If you look at the constraints, you will notice that $n \leq 100$. Therefore, if you save digits that did exist in the original number and the number after a carry, the size of the set is guaranteed to be less than 200. Now if we follow the process I explained in that another comment, we know we can manage $\mathbf{pp}$ and $\mathbf{pk}$ linearly starting from $p$ and the number of the last digit, and the loop won't take over 200 iterations. This is more than enough to fit in the TL. (I have not tested using an array here though, but the process will be the same anyways.) One great thing to note is that this solution's time complexity only relies on $n$. Maybe for this reason (and due to some additional constants), my solution turns out to be 5x faster than the jury solution. Good to know!Here is a submission based on this approach — 181506873.upd: The TeX for problem D's tutorial seems to be broken. Maybe you forgot come curly braces?
•  » » yeah true, I also did a similar approachSubmission: 187409654 Codevoid solve(){ lli n , p; cin >> n >> p; vl a(n); lli ans = 0; map frq; for(int i = 0 ; i < n ; i ++) { cin >> a[i]; frq[a[i]]++; } lli last_not_seen = -1; for(lli x = p-1 ; x >= 0 ; x--) { if(frq[x] == 0) { last_not_seen = x; break; } } if(last_not_seen == -1) { cout << 0 << endl; } else { //cout << last_not_seen << endl; if(last_not_seen > a[n-1]) { ans += last_not_seen - a[n-1]; last_not_seen = -1; for(lli x = a[n-1]-1 ; x >= 0 ; x --) { if(frq[x] == 0) { last_not_seen = x; break; } } } if (last_not_seen == -1) { cout << ans << endl; } else { lli range_low = 1 , range_high = a[n-1] - 1; ans = p - a[n-1]; a[n-1] = p; for(int i = n-1; i >= 0 ; i --) { if(a[i] >= p){ a[i] -= p; if(i == 0) { frq++; } else { a[i-1]++; } } } for(int i = n-1 ; i >= 0 ; i --) { frq[a[i]]++; } last_not_seen = -1; for(lli x = range_high ; x >= 0 ; x --) { if(frq[x] == 0) { last_not_seen = x; break; } } if(last_not_seen == -1) { cout << ans << endl; } else { ans += last_not_seen; cout << ans << endl; } } } } 
 » 1759D - Make It RoundHere's my clear and concise code for D #include #define int long long int signed main() { std::ios::sync_with_stdio(false); std::cin.tie(0); int t = 1; std::cin >> t; while(t--){ int n, m; std::cin >> n >> m; int x = 10, ans = n*m; while(1){ int g = std::__gcd(x, n); int k = x/g; if(k <= m){ ans = k*(m/k)*n; } else break; x *= 10; } std::cout << ans << "\n"; } } Greedy approach : We will try to append as many 0's at the end within given constraint How to find it ?Let's take input examples : 6 11 We can have 30 60 90 120 ... But only 30 and 60 falls within constraint 6*5 = 30, k = 5 6*10 = 60, k = 10 6*15 = 90, k = 15 not possible Now we will build our answer by checking if I append zeros can I get some k such that it falls under constraint, once we can't append 0's we will break loopHow to do it? How to find possible values of k? Well it's easy, we can use some mathematics We will check if I can have some possible number as z*10 z*100 z*1000 and so on... z [1, 9] z should be minimum possible as we are using greedy  we have our n as 6 so to append 0 at the end I can have z*10 n = 2*3 ans = z*10 so we have ans = n*k, 1 <= k <= m n = 6 k = 5 z = 3 ans = 30 n = 6 k = 10 z = 6 ans = 60  How to find k ? Let g = gcd(x, n) = (10, 6) = 2 then k = x/g = 10/2 = 5, k can take minimum value as 5 within given constraints But as we have to find max answer if we append some zeros so we will find k as k = (11/5)*k k = 10 Thanks :), You can ask doubt
 » I solved D with binary search for answer. I think it would be better to add binary search tag to D.
 » 3 months ago, # | ← Rev. 2 →   I almost copy-pasted solution for problem E from the editorial and it's giving WA on test 1?!Submission: 181810855 Code#include using namespace std; int g, b; int solve(vector &a, int n, int i, long long h, int green, int blue) { if(i==n) return 0; if(h>a[i]) return solve(a, n, i+1, h+a[i]/2, green, blue)+1; g=(green?solve(a, n, i, h*2, green-1, blue):0); b=(blue?solve(a, n, i, h*3, green, blue-1):0); return max(g, b); } int main() { ios::sync_with_stdio(0); cin.tie(0); int T; cin >> T; while(T--) { int n; long long h; cin >> n >> h; vector a(n); for(int i=0; i> a[i]; sort(a.begin(), a.end()); cout << solve(a, n, 0, h, 2, 1) << '\n'; } } 
•  » » Those global variables are the issue. Try declaring them inside the function.
•  » » » Thank you, it worked, but why?
•  » » » » Not quite sure aswell
•  » » g=(green?solve(a, n, i, h*2, green-1, blue):0); b=(blue?solve(a, n, i, h*3, green, blue-1):0);how this work i don't understand
•  » » » I hope these few lines will make it clearer.$\text{green}$ is the number of green serums left and $\text{blue}$ is the number of blue serums left.$\text{g}$ is the answer if we use green serum at the $\text{i}$ -th step and $\text{b}$ is the answer if we use blue serum at the $\text{i}$ -th step.When writing boolean valus, x is same as writing x!=0 where x is a number.
 » For E, shouldn't it be $O(NlogN)$ instead of $O(N)$ since you need to sort the astronauts?
•  » » Good remark, thank you
•  » » 2 months ago, # ^ | ← Rev. 8 →   Gornak40 harryzhengipsum Gornak40 can you help me calculate the TC of E? how I Calculate (No. states * transition) So, According to me, TC comes out to be n*h*s1*s2*2. Which is too high. What I am doing Wrong? I thought if I memories it will give MLE with 4 states.
 » Can somebody explain why the solution to D works, or what is the idea about it?Obviously it is somehow about the number of divisiors 2 and 5 in n, but how? And how/why does the sol find "If there are several possible variants, output the one in which the new price (value n⋅k) is maximal."?
•  » » The input number $n$ number can be factored as $x 2^a5^b$, $a, b \geq 0$The number of zeroes at the end of this number is equal to $min(a,b)$, so our goal is to maximize $min(a,b)$We start with $k=1$.while $k \leq m$, do:if $a < b$, multiply $k$ by $2$ and increment $a$.if $a > b$, multiply $k$ by $5$ and increment $b$.if $a=b$, multiply it by $10$. and increment $a$ and $b$. This guarantees that $k \cdot n$ has the largest amount of zeroes at the end, this does not guarantee that $k$ is the biggest possible number, to do this, we try to make $k$ as close to $m$ as possible.$k \cdot q \leq m$$q \leq \frac{m}{k}$$q = \frac{m}{k}$So the answer is $q \cdot k \cdot n$
 » Problem E: Please someone explain to me why we use solve(0, h, 2, 1) in the initial call, I don't understand why we use 2,1.? what's the logic behind it.?
•  » » oh, my bad, I missed that part — "the humanoid took with him two green serums and one blue serum".
•  » » » I am getting no idea what we are doing in que E. why are we recursively calling that function and what that function is doing?
•  » » » » @hemantpatil10125 it's just an observation. Try to figure out how it works, recursion is always hard, but very easy if you can catch the basic and base cases, sometimes tricky logic. Keep trying at least 20 easy recursion problems, understand how they work, then it will be much easier. I am also a newbie, so It may sound inappropriate to make a statement about that. Hope you will expert one day if you solve problems daily.
•  » » » » » thank u @survo_datta. What resources did you follow to learn recursion? How's striver's playlist?
•  » » » » » » @hemantpatil10125 you can follow tutorial from Luv(@iamluv) in youtube. Follow his DP series. I tried different tutorials and find his tutorial more accurate and beginner friendly though he covers many advanced problems too. Firstly you should solve problems from this: https://codeforces.com/group/MWSDmqGsZm/contest/223339 after solving all of the problems from this, follow his tutorials on youtube. you will be an expert in it.
•  » » » » » » » thank you...
•  » » » » The point is simple using recursion you will always know which operation will allow the humanoid to consume the cosmonauts in the minimum attempts possible. And will try to maximize cosmonaut consumption by the humanoid i.g. that is what the question is asking to max out the cosmonauts.
 » Can someone explain the segment tree solution for problem G?
•  » » 6 weeks ago, # ^ | ← Rev. 2 →   Did you got any segment Tree approach for this problem?
•  » » » I hope it's not too late, please see my comment below.
•  » » » » I got the approach btw. Thanks for replying
•  » » hey have you got the approach for segment tree, i could not come up with the solution of G i was trying using segment tree but i got stuck somewhere, if you got it please could you share the code.
•  » » » I think I finally got it. Imagine there's a bracket sequence $s$ of length n, for each $i$, we set $s_{b_i}$ to a right bracket and set the rest to left brackets (e.g. the first example input is (())()). So our target would became for each right bracket we want to match it to a left bracket to its left, so the solution exists only if it's a valid bracket sequence, i.e. all prefix sums are greater than or equal to 0.Because we want the lexicographically minimal permutation, so for each $b_i$ we want to find the smallest $j < b_i$ such that if we remove $s_j$ and $s_{b_i}$, the remaining bracket sequence is still valid. We claim that we should choose the largest $j$ such that $pref_{j - 1} = 0$ (I'm too lazy to explain it so try to come up with it by yourself). In order to find such $j$, we use a segment tree by maintaining the sum, min prefix, and the position of the min prefix. See jiangly's code or my code 188831632 for implementation.
 » Problem #D Solution https://codeforces.com/contest/1759/submission/182206818 May be helpful to better understand the tutorial solution approach of problem D with code
 » For the problem G, can we binary search on the value we put before each element of the input array? For each element, the search space will be the elements in the unused set which are smaller than this element. And to check if a value is feasible, we will check that we still have sufficient options left for rest of the elements if we put this value before the current element in the final array. This can be done using ordered set (refer my solution). We will choose the least among all feasible values, to maintain the lexicographic condition. Also note that the search space will follow monotonicity.I used the above approach, but got WA on TC3. Solution: LINK
•  » » Take a look at Ticket 16479 from CF Stress for a counter example.
•  » » » Thanks man
 » Tutorials last line in problem B needs to be n < cnt than n <= cnt