This video demonstrates three competitive programming problems from Educational Codeforces Round 190 (Div. 2), showcasing problem-solving strategies including: (1) Greedy optimization for minimum cost calculation by comparing individual vs group options, (2) Mathematical reasoning for string manipulation based on divisibility rules, and (3) Combinatorial counting for circular arrangement constraints. The solutions illustrate how to break down complex problems into manageable cases, apply mathematical properties (like divisibility by 4), and develop efficient algorithms through systematic case analysis.
Inmersión profunda
Prerrequisito
- No hay datos disponibles.
Próximos pasos
- No hay datos disponibles.
Inmersión profunda
Educational Codeforces Round 190 (Div. 2) | A B C Full Solutions | C++ with ExplanationAñadido:
Hello everyone, welcome back. So, today we would be discussing the recent Codeforces contest as an educational round round. And I was able to solve ABC till now, so I was making the video right now. Struggling with D's, I would be I'm trying to solve the D. If I would be able to I would add in the solutions part as well.
So, let's start with the A problem.
So, A problem was very easy. So, it was given that you are given some an individual key and a group key, and your cost is B dollars.
Okay, and you need to access the three Okay.
So, if I explain you the problem statement, the problem statement is saying that you are given with a key uh and keys are A and B. For A, it would be like working as a single unit. It would be working as a single unit and it would be serving as three uh like at most uh three number of students, okay?
So, what you have to find is you have to find the minimum cost to actually tell that what is the what would be the minimum cost, okay? So, minimum amount you need to provide the online courses for all the N students, okay?
So, it would be quite easy. So, if Okay, so what if I say you might be thinking that if it's serving three particular students then obviously then I should go for B, but that's not the case. So, you have to take care of a case that if three times of A is less than B. Okay, if we talk about the three students, okay? A, B, and like these are the three students we talk about. So, if we take the keys for them individually, so it would be A plus A plus A. That would have been 3A or there could be a possibility that it could all be combinedly served by B, right? So, if your 3A is already less than B, then can you say that it would be better to choose that A key for each of the students, so it would simply be NA.
Right? Else, else it would be less than equal to condition. Else, if your B is greater than 3A, then there would be a possibility, then what you would be doing is you would be uh doing B by 3 into Sorry, not B by 3.
It would be N by 3.
Like you would be taking the all the possible groups of triplets.
Right? N plus which would have been minimum of either A into N modulo 3.
Like let's suppose you are left with two children or one children.
And then you have to check whether I need to give A keys to that particular students or a single B key to that particular student.
So this would simply be our simply be our answer if I show you the code as well.
Let me load the code.
In the meanwhile the code is loading, let's discuss the next problems as well.
Yeah, so if we see the code, it's been simply written in the similar fashion.
If N modulo 3 is then would simply be B.
And like there I've made up a simple check over here. If it's divisible then it would simply be here. Else it would have been this. And this could be easily omitted because here it would simply be zero. So it would simply be B only.
So like this is how we actually write this code of this problem.
Now let's jump to the B problem.
Okay, so the B problem was quite tricky in terms of understanding the problem statement. So let me first give you the idea of what the problem statement I was saying. Then we'll discuss. Okay.
So if we talk about B, so what B was telling is that you are given with this particular string. Right? So you are given with the string. And what you have to do is that you need to remove some characters.
You need to remove some characters.
Such that remaining string that remaining string is not divisible by four. Right?
It's not divisible by four.
And that particular string is known as a beautiful string, right?
If a string It's already written in the question also. The string is beautiful if it's impossible to select some of the elements and write them out to form a number that is a multiple of four. For example, this is a beautiful because these all are not multiple uh >> [snorts] >> like not divisible by four. And these are not beautiful because if I remove one and three, then four this would easily be divisible by four. For 3 1 2 3, you I would have taken 12 and it would have been divisible by four. So, you need to tell minimum number of elements you need to remove, right?
Yeah. So, this was actually also an easy problem. Just you need to make cases and you need to take care of few and here and there steps.
So, if I tell you here in the string you're given with only four type of characters, either 1 2 3 and 4.
Now, remember the divisibility test of four, right? If I tell you the divisibility test of four, what it says is last two characters should be divisible by four.
Uh it should simply be 0 0 or last should be divisible by four, right? So, 0 0 cannot be possible because our characters are only 1 2 3 4.
So, in what case can I say my last characters would be divisible by four?
If it's a 12 or if it's a 32 or if it's something like 24, right? So, this would all be the possible cases. Or it could simply be the four as well. Like if any if at any moment my uh string could land up to four, then also my string would be divisible by by four.
So, first thing and foremost, first we have to you would be able to think that let's remove all of the fours, right?
This is the bare minimum we have to reduce, right?
Because if any four is left in the string, I could delete all of the characters and this particular string would only be divisible by four, right?
So, first we'll remove all the fours.
All fours, right?
Now, what we have to deal with one three and three two. So from this we actually came to know that if we talk about some I index some I index and which A of I is equals to two where A of I equal to two then if there is any one or three before this two I have to remove that. Right?
I am don't care about this one and three before after two but if there is before two then I have to actually delete them.
Right? So if we talk about that some I index if I if I talk about some I index of an array if I talk about some I index of an array where A of I equal to two then can I say after removing like removing all of the fours can I say before this would all be twos only, right? And after this there could be one threes one threes one threes or three ones.
Right? It was something like this and before this it would something be something like this.
So this is what you have to actually compute for each I point for each I point you just need to check for the count of what is the count of one three one or three and what is the count of two?
Whichever is your minimum you'll take that as an answer.
So let me show you the code as well.
What I'm trying to say is So this is simply let's count the if I what I've done is I've simply counted how many number of fours are there and how many number of twos are there.
And my answer would be either what I've done is the answer simply would be either I remove all of the one threes before two or remove all of the twos.
Whichever would be the minimum I would do that.
But there would be some cases some cases where this would fail is where you have only one two two one like it's something like this there are no threes. So in that case you have to uh like this would be some kind of an edge case. So, that's why I've come up with this particular logic where for every ith index you need to check that before that there should be no one or three.
And after that there could be one or three possible.
So, this is what we have done. For each I what we are checking is what is the count of 1 3s and what is the count of two. Right?
If your count of 1 3 plus your count after that, like the count of 1 3 over here and the count of remaining twos, that the remaining twos you have is less than your current answer. Current answer could be remove all of the twos. If it's less than that, then just simply update your answer.
And then the final what you have done is answer plus count of fours.
So, this is how you would be solving the problem B.
This was actually an easy problem. Just you need to take care of this particular induction that this could fail at something at this point as well. So, or some of the edge cases here. So, you just need to deal with that.
So, okay. So, let's discuss the C problem as well. So, C problem was actually also a good problem.
Uh let's discuss this part.
So, what the C problem is saying is So, C problem was saying that for some C of I Okay. That C of I is the of CI is the number of cards of type one, C2 of type two, and till CN of type N.
What you have to do is you have to arrange them in a circular order, like in a circular fashion, such that three consecutive cards, if I take about this particular three consecutive cards, two of them should be same. Any two of them should have been the same value. Right? Let's discuss with the test case as well. So, if we talk about this particular test case, 1 1 1 3.
So, if I talk about 1 1 1 and 3. So, this is one, this is the value two, this is three, and there is four, four, and four.
So, what it actually means is what they want to say is so, if you arrange so, what they are saying is if you arrange this particular into some circular permutation, let's suppose we have made this particular permutation. So, if we talk about any set of three three values, right? This, this, or the circular one.
All have at least two values are same.
So, this could be other possible length of the Like this could be a one possible pair, right? So, you need to find the maximum number of cards that you can arrange.
Right? You need to find the count.
Okay. So, now let's discuss how do we actually solve this problem. Okay.
Now, if you would have solved this like or give some time on this problem, you would have come up with a simple idea of that if if your count of distinct values like whose value whose it's not one is zero.
Then, means all are ones kind of I would say. So, in this particular case your answer would simply be zero because you cannot arrange them in such a fashion that it would become a like it would satisfy this particular scenario, right? This particular condition would be failing.
Right?
Now, let's see how do we actually like let's now delve deeper into this intuition part of this.
Okay.
Let me give you a name. Let me define a weak card and a strong card, okay?
The weak card is the count whose frequency or count is one. Like whose count is one and here the strong card is whose count is greater than two.
Like greater than or equal to two. So, like it's greater than equal to two.
Okay. So, now what we'll be doing is if if I want to place a weak card, if I want to place a weak card, then I would be at least be have been placing a strong card here and a strong card here to make this particular pair as a safe pair. Like this particular triplet as safe. Then only I would be able to say that this particular triplet is safe.
But, if you zoom it a bit, like if you if you say this is a weak card and here is a strong card. If I talk about this particular triplet, here is a question mark and or this particular triplet here is a question mark. So, here also to make this triplet also safe, do I need a strong card here as well?
Yes or no? Like I would be needing a strong card here as well.
Right? So, and this strong card should all should all be of same type. Right?
Should all be of same type. Then only I would be able to make it a pair.
Right?
So, this is what the logic we would be using. So, if if this particular count, like if the count of if the count of strong is equals to zero, then your answer would simply be zero.
If your count of strong is equals to one, that there is only one type of strong card exist, then your answer would have been simply be like you what you have done is what you would have done is like it would simply be the count by two because it would simply be the total number of possible pairs that you would have been taken.
Right? Plus the minimum number of plus it uh and let's assume that this particular count uh let's uh there is a count of weak as well. Let me denote it by W. So, so there would have been CW, which would denote the count of weak.
Right? And C by 2.
Right? So, this is only we can say, right? So, this was the how many cards that you would be able to arrange. Okay, let's let's take an example as well. So, let's suppose uh Let's take this particular example only.
Let's take this particular example only.
Here your uh CW is one and your CS is actually three. Right?
So, if we talk about how many cards that you can arrange is it would have simply be uh it would not be C only it would be C.
Right?
So, here how many cards you would have arrange is C plus the minimum of this particular thing.
Right? So, here C is three plus the minimum of CW and C by 2. C by 2 is actually one and here also CW is also one so minimum is one and here four. So, this is the four four cards that you can easily arrange.
I hope you would be able to like able to think what I'm trying to say over here.
So, like for if you think it a bit like if you have a strong card strong card and a weak card over here then you have a strong so this is a particular I would say a pair or a what we call it as a bracket or something that uh that would exist. Okay?
Now, let's delve the case when you have multiple multiple strong cards.
Right? Example, let's suppose you have some fives and some eights.
Example, let's suppose take this let's take this particular case.
So, what I'm trying to say is so, let's consider this particular case we have six eights, right? Six eights.
So, if you would have been you would have played something like this W1 8 8 W2 then 8 and 8. Right?
And here you would have thought of placing some weak elements W3 and W4.
But, you cannot place this W3 and W4 over here because before W4 and after W3, there would be some another pairs.
Let's suppose five is five and five five would be starting. So, in this particular block, if you see or this particular triplet, if you see, or this particular triplet, if you see, that won't be a correct triplet or that the condition would be failing. So, in that case, that your count that you were actually dealing over here, like in when you were finding for this as well, so here your count is actually not giving you the exact picture. So, here your count should have been reduced to two.
Like to count that how many number of pairs, like how many actually count is being used.
To like how many cards are actually being used. So, here the count would actually be reduced. So, if I tell you the exact uh equation of how do this particular thing would look like, so if your count is if your S uh what we have defined in this particular thing is your count S. So, if your this particular count S is greater than equal to two, so what you have done is you heard have simply taken the sum.
Like sum of all of the counts.
Right?
Plus the minimum of plus the minimum of uh count weak, comma count weak, comma C minus 2 by 2. Like the summation of all of this. Like it will be the summation of all of this.
Like whichever would be the minimum, you would be taking that and you would be adding it to the sum. So, this is how your problem C would look like. If I show you the code as well, what I have written is So, here is the code as well. So, if you see, so what I've done is I've maintained a count one and a count two and sum and a capacity.
So, here count one is the simply count one over here, else it's count two. And the capacity I'm maintaining as well.
I'm maintaining the capacity as well and the sum is also I'm maintaining.
So if your count two is simply zero, it would be zero. And if your count two is equal equal to one, it would simply be the sum that the that we have discussed over here.
This particular C.
This would simply work as a sum.
And minimum of count one comma sum by two.
Else, what would have been? It would be sum plus minimum of count one plus this particular sum. Like this particular with a new sum or you can term it as a capacity as well. So this would act as your new new sum. So that would have been uh used as a new sum over here.
So this is how you would be solving the C problem.
I'm sure uh you would be able to uh think about how do we actually solve the C problem.
And yeah, so that's it. If you like the video, please do share, like, subscribe to the channel, and stay tuned for more such contest-related discussions. Thank you.
Videos Relacionados
Agentforce NOW AMA: Build with React and Salesforce Multi-Framework
SalesforceDevs
490 views•2026-05-28
How agent o11y differs from traditional o11y — Phil Hetzel, Braintrust
aiDotEngineer
450 views•2026-05-28
WEB TECHNOLOGIES UNIT-2 | Degree 4th sem BCOM Computers web technologies unit-2 full explanation💯✅
LearnwithSahera
1K views•2026-05-29
More tests are always better? How to use AI to identify tests that bring little value
Alliance4Qualification
335 views•2026-05-29
Search Algorithms Explained in 60 Seconds! 🤖💨
samarthtuliofficial
218 views•2026-06-01
People of Game of Thrones using JavaScript DOM
AltCampus
296 views•2026-05-30
Introduction to Problem Solving Part - 1 | Lecture 1 | Intermediate DSA
ascensionix
107 views•2026-05-29
So What's Odin Lang Even Good For
TechOverTea
131 views•2026-06-01











