 What's up guys, my name is Michael and welcome to my YouTube channel. Today we're going to go over bowls, which is this computational geometry question on code forces. I normally go over algorithms and data structures, but I'm trying to get it more into the competitive programming side of things on my throat is actually hurting. I'm kind of sick right now. So please bear with me. So this is the problem given in bowls, each bowl is a basically a trapezoid bowls are stacked on top of each other. You're given the height, the first width, the second width, the longer width of each bowl, and you want to find the distance from the bottom to the top most bowl. I'll give you guys an example. So in the first input, here's one n is equal to two, you're given two trapezoids, first height is 40. So this height is 40. Short side is 10. Long side is 50. And then the next trapezoid is stacked on top of it. Bear with me on the my shapes are not really drawn in scale, but this is the next trapezoid and it's stacked on top of it. The next height is 60. Bottom width is 20 here. And the top width is 30. Yeah. So they want you to find the distance from the bottom length to the top most. So this distance. So how exactly are you supposed to solve this question? There's actually two things you need to know before you understand how to do this problem. One is similar triangles. And the other one is basically how trapezoids work. Okay, so I'm going to start out and just explain to you guys to the two formulas you need to know. The first one is similar triangles. So let's say I'm given this tall height, let's say h two, and I'm given this small height h one, and then I'm given our one. How do I find the large height of this? Because you know that the smallest triangle is similar to the large triangle, you know that the ratio between height and the radius are going to be the same. So h one over r one is going to be the same thing as h two over the length that we're trying to find. Let's call it r two. Okay. So this is the formula that you know about similar triangles, remember in grade school. Another formula you need to know is that, let's say I'm given a trapezoid. Let's say I'm given this trapezoid. If I were to cut the top part from the bottom part like this, right, let's say these two are parallel. If I were to take the long side, let's say we call this r r l and the bottom side are small. Okay, so radius small, radius large, or take the top and subtract from the bottom, that would give me these sides. If I would take this top, this whole length, and subtract this bottom length, so get rid of this, that would give me the sum of these two sides. So if I were to divide by two, after given those two sides, I would get the length of each individual ones. So if r l minus r s would give me the size of these two, that would give me two x to each side is x, x would equal to r l minus r s over two. That's basically the two formulas you would have to know in order to solve this problem. What's the hardest part about the problem. So back in here, the hardest part is trying to find the height from the bottom to the top and you will be like, how am I supposed to find this height? So many numbers. And I really don't want to know how to figure it out. And it's just ridiculously hard. So I was rambling on way too much. And I had to stop the video and rerecord because like, I didn't know what the heck I was saying. But okay, so pretty much we're trying to find this height, right? The height from the bottom most length of the bottom most trapezoid to the top most trapezoid. So how exactly are we going to do that? Well, if we were to find this small sliver of height, this small sliver, let's call it delta. So this is the change in height. If we were to find this small delta, and you were to add to 60, which is the height of the top most trapezoid, you would get this height, you would get the height of the final answer. That's basically how it works. If you take this, if you'd figure out this small delta, and you add by the height of the second trapezoid, you would get the height of your end result. So the main issue is how do I find this delta H? If I could find the height of the bottom most trapezoid to the bottom of the trapezoid stacked on top of it, and then add it to the height of the trapezoid above, I would get the final height. Okay. Once I find the delta H, as long as I could find the delta H plus the height above the trapezoid, if I find the maximum one for all n cases, that would be the end result. So the distance from the bottom to the top most trapezoid H max would equal to the maximum value of each individual height for each iteration H of I plus the delta for each single trapezoid. Okay. So how are we going to solve this problem? Well, we got to find this delta H, plug into this equation, you would basically get your answer. So how do you find this delta H? Okay, so I'm going to explain two trivial cases of finding the delta H. Okay, so we have to derive a mathematical equation on how to find delta H. We have to replace these numbers with variables. Bear with me because this is going to be a very, very difficult, tedious process. Here's what I'm going to do. I have to give variables. Okay, I need to differentiate which is the top bowl I'm at and which is the current bowl I'm at. Okay, because there are n bowls. And I need to differentiate which is stacked on top of each other. So to do that, what I'm going to do is I'm going to call j is going to be the bottom bowl. Okay. So I'm going to redraw this. So this bowl, this bottom bowl, I'm going to call this the jth bowl, j is going to be the bottom bowl. Okay, I'm going to refer to the top bowl as bi, this is going to be the top bowl. So bi is going to refer to as the top bowl. Bear with me guys, because this is going to be an intense explanation. Okay, bj is the bottom bowl, bi is the top bowl. Okay, I'm going to call each of these values are small to be is going to be the smaller radius, big case, r is going to be the large radius. Okay, so r is small is going to be small radius, big R is going to be large radius. Okay, so once that's, I'm going to differentiate it by okay, so the r small of the bottom bowl is going to be RJ, which is going to be the smaller side, our large with the bottom bowl is going to be RJ. And I'll call the height for each bowl, h is going to be the height height. So this height is going to be hj because it's the bottom bowl. Now for the top bowl, I'm going to call the bottom bottom side, our eye, and the large side of the top bowl, I'll call it big R I, and I'm going to call the height of top bowl, h of i. So this thing is going to be each of i. Okay, I hope you guys understand what's going on here. I'm just giving names, variable names for each of the value inside the picture. So I could derive a mathematical formula to solve this problem. Okay, the delta height, I'm going to call it delta ij. That's going to mean the change in height top bowl in the bottom bowl. And we're repeating this n time, there's n bowls. So we're going to repeat this n times. Okay, so I already labeled some variable name. Now I actually have to give you the test case trivial test cases, because there's some cases that you have to think about before you actually start doing this, all this code stuff. Alright, so there's two basic cases. First case is what if the bowls are stacked on top of each other. In that case, the delta ij is actually just going to be the height of the bottom bowl. So let's say so they're stacked on top of each other, you can't do anything at this point, they're stacked on top of each other. So this delta ij is actually just going to be the same height as hj. And let's relabel the values again. So remember, I is the top bowl, j is the bottom bowl. So rj. And remember the height of this is hmi for the top bowl. Okay, so in the case when there's the bowls are stacked on top of each other. What does that mean? In this case, if you look at this picture, that means that r i is going to be greater than or equal to rj. So r i is greater than or equal to rj. That's the only case when the bowl is on top. So in this case, rj r i, the length of the bottom one of the top bowl is actually greater than or equal to the length of the bottom bowl, the top of the bottom bowl, right? So r i is greater than equal to rj. In this case. So that if if this is the case, if r is greater than or equal to rj, your delta ij is going to equal to h of j. So that's the first basic case, bowls are on top of each other. Next case is the bowl is actually inside each other. So this is the next trivial case. If the bowls are inside each other, that means our delta, the change in height here, delta ij is actually zero bowls inside each other. Why? Because there's no difference in height from the bottom bowl to the bottom of the top bowl. See the delta change in height is zero. Okay, they're inside each other, therefore it's zero. So in what cases that let's say I just redraw this again. So r i, okay, so in the case they're inside each other, delta ij is zero. And what case is that that's the case when if big r i is smaller equal to little rj. That means that this delta of the change in height is equal to zero. And why is this the case? Because if r i is larger than rj, then it would actually get stuck between. But no, the value of the large height of the top bowl is actually smaller than the value of the bottom of the bottom bowl, smaller side of the bottom bowl. At this side is smaller than this. That's why the bowl just gets dropped immediately into each other. And that's why the delta ij is equal to zero. Okay, all right, guys, so the first hard case I have to go over is this, it's going to be the bottom of top bowl is stuck gets stuck in between between bottom bowl. Stuck between bottom bowl. And what does that mean? That basically means that let's say I have a bowl like this bottom of the top bowl, like this bowl is stuck in between. Okay, so this bottom of the top bowl, bottom of the top will get stuck in between parts of the bottom bowl. And if we were if we're supposed to label these sides, h of i, h or j. So this would be the drawing of what it would look like. Remember, i is the top bowl, j is the bottom bowl. Okay. So in order to do this, how are we going to solve this problem? Well, if you were to actually cut your similar triangles and create a triangle out of this right side, you could actually solve this problem. So this to do it, I'll just create a triangle like this from this right side. And triangle like that. So if you were to kind of like this, these sides are the same. Okay. And what what how do you how do you solve these sides? Like, how do you solve this side? It's going to be the top length minus the bottom length over two. And that's going to be the value of this side. This is rj minus r little r i over two. Okay, the this side and this side is the same. And if you were to cut a value from here, this side and this small side, how to cut this zoom in on this part. This side would be rj minus little r i over two. Okay. And you could you could tell based on the trapezoid. So if you were to create a small part from here, using triangles and cut this part, this side is actually the same thing as this side. Okay. So the small side would equal to rj minus little r i over two, because of similar triangles, when you cut a similar triangle like that. So now, since you know the values of these two sides, you could calculate the delta and delta here is the delta H, right, delta ij. And how do you do that? We're trying to find this delta right delta ij. To find it, we need to know the this side. Okay, so we're going to use this side is going to be big H minus delta ij, this small sliver. So if I were to redraw this triangle, hj. So what does that mean? That means this part is going to be hj minus delta ij. Okay, because these parts, if you add them up, only hj. So we can use similar triangles on this part. So the ratio of this over this small side is going to be the same as the ratio of this large H over this. Okay. So just like the same formula that we saw before, we're going to use that in this case. So this is going to be, we'll do large side, hj over large side, divided by rj minus ri over two. Okay, so now we have this formula and then we could solve for delta ij. And if we were to solve for delta ij, I'll show you guys how to do this. So we could get rid of the constant over two here, then we could cross multiply. So we can have rj multiply by r. I'll just use delta for now. Okay. So we're trying to solve for the delta. Do that we'll divide rj on both sides. Okay, so then we have hj minus delta is going to equal to hj times rj times ri. We'll move the delta to the right and then subtract by hj. So hj minus over rj minus rj. And that's going to equal to delta. So now we have this, we're going to just simplify using algebra rj minus rj minus rj hj times hj minus ri. And then after this, we're going to do hj. So the final answer would be this. So the final answer would be hj times ri minus rj over rj minus rj. And that would be your answer. And what case is this? We go back to the question. So in what case would this equation make sense? Where the bottom of the top bowl gets stuck in between the two sides of the bottom bowl? So that means that ri gets stuck in between rj and little rj. That would only make sense if ri is greater than rj, right? So because like otherwise, if it wasn't the case, it would just go sink through and then hit the ground. So the only way that's possible if the the smaller side ri is greater than rj. That's the only case. So this is when ri is greater than rj. Otherwise, it would hit it would hit literally sink to the ground. So yeah, if this is the case, then we'll look at our equation again. We're going to have to do rj. If this is the case, then delta is going to equal to hj times ri minus rj over rj minus rj. Yeah, so that would be the case. All right, let's go over the other two cases and how to derive the formula for that. The next case that we have to think about is if the top trapezoid is actually stuck inside the bottom and to do that, I'll draw you a trapezoid and the top trapezoid is actually stuck in between the bottom. So this is the top trapezoid, this trapezoid stuck in between the bottom. Okay. So when the top trapezoid stuck in the bottom, remember I is the top trapezoid, I is the top trapezoid, top trapezoid, top bull, j is the bottom bull. And that's this case. So if we want to find the delta, the delta is this part delta. And that occurs is the distance from the bottom to the bottom of the bull, right, bottom bull to the bottom bull. And that's this delta. I'm going to label the other values now. So this is, so this part is going to be h5 and the big part is going to be hj. Now, to solve for delta, I'm going to draw triangles from here. So to do that, I'll draw a triangle like this, just to make sure you guys understand. Okay. And I'll draw a triangle from here. So I hope you guys could look at it. And once I draw the triangles, I'm actually going to draw the congruent triangle from the inside. So you guys understand that's I've done, that's all I've done. So we know the inside triangle is actually similar to the outside triangle. And so we could derive an equation for the ratio of the height of the inside over the bottom. And it's the same ratio as the height of the outside over the bottom. And how we're going to do that. If you noticed this height, not the height, this side is actually going to be rj minus little rj over two, because it's the same value as this. And if you were to cut the same side on both sides, like that, it's not to scale, but these values would be the same. Right. So it would be rj minus little rj over two. Remember the second equation I showed in the beginning of the video? This is bottom side is rj minus little rj over two. So that's this side. And we'll about the small triangle, the width of the small triangle, that'd be ri minus rj over two. So and this, this value is going to be ri minus rj over two. The small value, yeah. And why is it because ri minus rj will give you the sides of these two sides. Ri minus rj will give you the sum of these two sides. And if you were to add by two, you would get the individual side of this. And that's the same thing as this. So that's why this side is ri minus ri minus rj over two. Okay, that's the length of the side. This height is just h of i plus delta. So all the label here, i plus delta. And the total height is just hj. So to solve this problem, I'm going to do the ratio of the height over each individual side. So hi plus delta divided by ri minus rj over two, it's going to be the same thing as hj over rj minus r little rj over two. So I'll write that now, j minus a little rj over two. And then we're going to cross out the constant over two. We'll cross multiply. If you were to solve this equation for delta, you're going to get ri minus rj over rj minus rj multiplied by hj minus h of i. And that would be the equation of this. So how did we get the top ball to get stuck inside the bottom ball? That only can occur if this this side is less than or equal to this side. So that only occurs when ri is less than or equal to big rj, right? Because if it was greater, then it wouldn't go inside the ball. This length has to be less than this length for the ball to get stuck inside. If it were greater, then this ball would actually be stacked on top, but it's not. So if this is the case, we'll plug in this equation. Okay, so that got us the second case. And I'm going to go over the third case, which is the third and final case. And yeah, once we go over the third case, you just I'll code it up for you guys. Alright guys, so this is the third and final case. It's when the sides of the top ball get stuck in between the tips of the bottom ball. So I already drew this for you guys, because I didn't want to waste time on it. But this is the bottom. This is the top ball, or by as you can see here. And it's stuck in between the tips of the bottom ball. Like this is the bottom ball rj. And the top ball is stuck in between the tips sides, the top tips. So that's that's that's the third case. Um, so this is our top ball. That's where the height is in this case because it's stuck in the H I it's stuck between the tips. This is the delta. It's the the change in height between the bottom side of the bottom ball and the top ball. This is a H of hj, which is the height of the bottom ball. The bottom ball. Okay, so how would you solve this problem? We could build a similar triangle, a small similar triangle like this. Um, so the ratio of this large side over this bottom side is going to be the same thing as the ratio of this side over this side. Okay, hj, subtract by the delta is going to give us this side, it's going to give us this small side. Because hj minus delta is going to give us this small side. h of i minus this is going to give us this small side. So it's going to be h of i minus hj minus delta. Okay, that's going to be this small. This side is going to be r i minus r i over two. So that's this side. r i minus a little r i over two. Yeah. And this this side is going to be r i minus r j over two. This side. So r i minus r j over two. And that's this side. Okay, based on the equation I showed you in the beginning. Now we could just do the ratio of this height over this is equal to this height over this. So to do that, this height is r i minus r j minus hj minus delta. That's this height divided by this side is going to be r i minus r j over two. And this side, the total height is h i. So we're going to do equal to the total height this side over this small side. That's r i minus little r i over two. We're going to cancel out the two. Okay guys, so after you solve the equation for this bottom part, you get this equation. And then if you were to simplify it even more, you get h j minus r j minus r i over r i minus little r i times h of i. And that's what this delta equals. Okay, so once we finish this, finish this this part, after doing all this, we know that the delta is equal to this equation. So in what case does this occur? This occurs if r i is bigger than r j. Okay, because it's the length of this top part has to be bigger than this side, this top part. So that's r i greater than r j. That's when the bowl of the top tip gets stuck in between the bottom bowl. r i is greater than r j. And that's what causes this. So yeah, that's how you do the third case. Now we're going to code up the solution. And yeah, that's pretty much it. All right, guys, so I'm going to code up the solution with you guys now. And basically, we'll just do this really quickly. It's actually not that long of a code. So first we need an array, actually three arrays representing the height, the small radius, and the big radius. So what I'm going to do is I'm going to actually, because we know the bounds, let's see the bounds. The bound is 3000. So I'm going to put just in case I'll put 3333. Okay. So the top bounds is 3333. So I'm going to put that just as reserve space for 3333. Then I need to create a delta to represent the each delta we're calculating for the change in i and j. So I'll do deltas is going to be 3333 also. Okay. Okay, so now we got that. Oh, yeah, guys, this line of code just increases speed for input and output. So we don't really need to care about that. This line of code is to change redirect input to a text file reason is because to submit on code forces, this problem required us to basically read in a text file. So I don't know why but we had to do that. Okay. Once we do that, let's look at the input, the input is C in first value n. And then yeah, and put three values. So I'm going to do that now. So what we're going to do is we're going to create a value and variable and we're gonna CNN. And then we're going to loop from x equals zero x less than n x plus plus. And we're going to see in the height, read in the height. Is a small radius or big radius? Let me check. Small radius. Yeah, small radius. Then big radius. Okay. So once we got that, now we need to create a function that calculates the delta for each value. And then we could get the maximum delta. Then we add it with the value above it. So I'm going to do that now. So I got to create a function, I'm going to call this calculate delta. And it's going to pass an i and then j. ij is going to represent the top and then j i represents the top j represents the bottom of the bowl for whatever index it is. So what are we going to do? We're going to do the same equations that we written already. I'm going to pull up the editorial because actually not the editorial. I already already pulled it up sort of. So I'm going to write that now. So we're going to create a value called current delta. We're going to set it to zero. This is going to represent the we need to get the maximum current delta, the maximum delta. So that's why we're going to use current delta to get the max. Actually, I'll call it, I'll call this max current delta max delta. Okay. So if r i is greater than or equal to big r of j from the first equation that we derived, this means it's stacked on top of each other. Yeah. So that means that we're going to return the height. So we're just return h of hj, the height of the height of the bottom, height of the bottom one. Okay. Now, if the now we got to do the three equations, which is going to be, I'm going to copy the three equations here. So if r j r i is greater than or equal to r j, then the we also have if r i is less than or equal to r j, and then we have r i is greater than or equal to r j. Okay. So now we're going to just calculate the return those three values, the equations that we made derived from the beginning. And I'm going to return max delta in the end. Okay. So now there's that. Now I'm going to calculate each individual height. So to get each height, I'm going to do current height is going to equal to r j minus r i times h i over h r i minus little r i. So I'm going to do that. So let's just calculate is going to be r j minus top r i multiply by h or by divided by minus little r i. Yeah. And then to make this better, because we made them integers, I'm going to make cast this into a double. So to do that, I'll just cast the top part to a double. So I'm going to do this double. So yeah, now that's cast to a double. And yeah, I'm going to do double. Then once we get the current height for, I'm going to put all the other equations that we derived minus little r i times r j. Okay, so now we have the three equations that we derived. I just copied and pasted them here. Now we're going to do is once we got the current height, we have to add to the delta and do that we'll just do not the delta we got to do got to get the maximum delta for each height. Okay, so some of these required some of these required us to subtract the height of the top in the bottom. So yeah, we have to do that. But I'll do max delta is equal to max of max delta and then the height subtract by current height. So this was a this was a whole equation that we derived already mathematically derived. So I'm just going to take a maximum for each of them. Okay, max delta minus current height minus and then this one was just max height. Yeah. Okay, so now once we copied the equations down to calculating the delta, we had to use max for all these because they could be more than one. So that's why once we got that now we just have to call it and that's pretty much it. I'll do that first soon. Hold up. Okay, so now since we calculated each we had a function that helps calculate our delta now I'm going to create a variable called largest height. And I'm what I'm going to do is I'm going to go through from i equal one is n i plus plus this is going to go through from the first bowl to the end. Then I'm going to start from j is equal to zero j is less than i j plus plus this is going to go from the bottom bowl this is going to represent the bottom bowl from zero to i. Okay, and then what I'm going to do is I'm going to actually create I'm going to create a variable called position and it's going to get the maximum it's going to calculate each maximum delta. And then I'm going to set the delta at i each position at the delta at i to be that position. So I'm going to do double position is equal to equal zero. Then I'm going to do position is going to equal to max delta at j plus the calculated delta at i j. Okay, what this is going to do is what is what's the problem? Double? Oh, yeah, what this is going to do is because our deltas are starting at zero right because we didn't initialize our each delta yet deltas because it's an array it's going to start at zero all the values are zero and we didn't initialize each value yet. So what I'm going to do this loop is you're just going to calculate each delta value and it's going to plus the calculated the perspective value above it each value above and below it and then it will define the maximum one. Then after that I'll just set the deltas at i is equal to position. What this is going to do is this is going to give me the maximum possible deltas I could get for each position from for each bowl right so for bowl one I want to calculate the maximum deltas from zero to one so and then if bowl two I want to calculate the maximum deltas from zero one two for for each top and all the values on the below it for three I want to calculate zero one two three so what this is doing is it's going to calculate each delta from the starting bowl that I'm out and all the bowls before it okay and once I get that I'm going to get the maximum value of those deltas from all those bowls and I'm going to set it into my array of deltas which is the delta of that I that I initialized okay so now once we got all the maximum deltas for each bowl I'm going to start loop again and this time I'm going to start from i equal zero i less than n i plus plus what am I going to do I'm going to get whoops my bad what am I going to do I'm going to find the largest height so to do that I'm going to do largest height is going to need to roll to max largest height and then what I'm going to do is I'm going to do deltas at i I'm going to add by the height before it so the height before it is going to be each of i all right so the height above it so for each delta at i I'm going to add by its respective height okay so I don't know if I told you guys this already but each uh each of these values represents the index of represents the index of the each bowl so height at one rep r at one big r at one represents the height small radius big radius at bowl one uh height two represents height small big radius at bowl two three yada yada yada so that's why I'm going to get respective deltas that the maximum deltas that I got and I'm going to add it to its respective height okay so now once I get the maximum height all I have to do is just print it out to eight decimal places so I'll just do that because it requires us to do that so I'll do print f percent 0.8 f uh it's eight decimal places and I'm going to do largest height and that's basically it that's that's how you do this question uh I hope you guys enjoy this video I hope I taught you guys how to do this per problem and I'm going to save it and submit it and see just to show you guys how to do it so let's do uh bowls see I already did it once so I'm going to do this submit code bowls wait it's going to take a while well it doesn't take that long it would probably take a maximum of like five minutes not five minutes two minutes yeah and it got accepted so yeah that's how you do this question rate cop subscribe I'll check you guys later