• 3 days ago
Transcript
00:00Next, we will talk about this question. It's called chocolate problem.
00:03Chocolate is basically chocolate.
00:05This problem is available on the spoj name platform.
00:08And this is a very important problem.
00:10This problem is a hard level problem.
00:12Microsoft, Amazon, Google, Facebook and companies have asked in the problem.
00:16And it's a very classical problem.
00:18Based on this, there are many problems that follow the same logic.
00:22If we understand this hard level problem,
00:25then it's a good thing for you.
00:27So, we have to learn this.
00:29And those students, especially those who want to know the direction of the competitive programming,
00:33they should understand this problem a little bit.
00:36Now, what is the chocolate problem?
00:39Let's say, we have a chocolate bar.
00:41This bar's dimensions are m x n.
00:43So, these square pieces are m x n dimensions.
00:46So, this chocolate bar shows something like this.
00:51In which I have n rows and I have m columns.
00:56What the downside of this room runs is to look at it.
00:57If I need your chocolate bar, I can break this way.
01:00Now, let's pick this Ax 사실 square square.
01:01That requires thechemical bar and the many square units to drop in there.
01:02That means, in this 넷 button, I can break this one.
01:09In a lower row, I can break it all for me.
01:12It must be included in thisütur,
01:19So, there could be the minimum fraction of this point.
01:20I can break it out of this one.
01:21So, I will break it that way.
01:21I can break it withו,
01:22I can break it all.
01:22Here, normally, if I can break it let it Vor cradle an or move here.
01:25which is equal to y1. If I break the chocolate here, I have to give money to y2.
01:32So, the cost depends on size. I mean, I cut the chocolate here, or I have already cut
01:40and my chocolate is so small, then I cut it here. After that, the cost will not be variable.
01:45The cost will always be x1 if I cut the line. It depends on the line, the break goes along.
01:52This means basically this is, let's say that the cost of this line is x1.
01:57So, the first time when I cut the line, I am going to put it on this line.
02:01My chocolate has some kind of method. And what did I do?
02:06I cut the line on this line.
02:09As I cut the line from here, this will become my chocolate's first part,
02:12and this will become my chocolate's second part.
02:14So, the cost is equal to x1.
02:17But let's say another case, let's say that I have the same chocolate.
02:21And what did I do?
02:22I already have this chocolate here.
02:26I mean, this part is different.
02:29And this part is different.
02:31Now, what do I do?
02:32I put one more cut here.
02:34And how much cost is this cut?
02:36This cut is also x1.
02:37So, my cost is x1.
02:39So, my cost is x1.
02:50It's x2 is x2.
02:51And here, the horizontal lines will give us y1, y2.
02:55So, every individual line will have some cost that we don't have to give.
03:00If we put the chocolate there, we break it.
03:03We divide the whole chocolate in small squares.
03:07If we divide the whole number, we divide the whole amount of cost.
03:08So, our cost is the minimum cost.
03:10It means the minimum possible cost.
03:12We divide the whole chocolate in small small small small small small into small small small small size.
03:16This same problem is with a lot of platforms.
03:19Minimum cost to cut board into square.
03:26If you have a problem with this name, you will find the same logic, you can go and solve that
03:32problem,
03:33or not directly, we can solve the chocolate problem.
03:36Now, what is the approach?
03:38First of all, let's understand some things,
03:41let's clear some basic fundamentals,
03:43because there is a hard level problem,
03:44so we will have a little time,
03:45we will do logical things.
03:47We will do logical things in this way,
03:49let's assume that my n value is 4 and m value is 6,
03:53meaning that my n value is 4 days,
03:56and my n value is 6 columns.
03:59So, what can I say?
04:00In 4 days, there are 3 cuts.
04:03The first cut will be,
04:04this cut will be 4,
04:07this cut will be 1,
04:08this cut will be 2,
04:11and if there are 6 columns,
04:13then there are 5 cuts.
04:15First cut will be 2,
04:17second cut will be 1,
04:19third cut will be 3,
04:20fourth cut will be 1,
04:22fifth cut.
04:23So, this can be a lot of cuts and costs,
04:28I will give my n value and m value.
04:31Now, I will give this chocolate in single pieces,
04:33this single piece,
04:35this single piece,
04:36this single piece,
04:36this single piece,
04:37this single piece,
04:37this single piece,
04:38which will be divided with the final cost.
04:44In this way,
04:53I always have to divide my chocolate with the cuts
04:57Because if I have to cut all the parts from here
05:00I have to cut all the parts
05:03I also have to divide these 3 cuts
05:07I always have to divide these 3 cuts
05:09I always have to divide these 3 cuts
05:11I always have to break my chocolate
05:18I always need to break the 3 cuts
05:21I always need to break the 4 cuts
05:26I always need to break my chocolate
05:29I always need to break the 4 cuts
05:35I have to break my chocolate
05:36The real question is that I need to take the sequence
05:41Or order of cuts
05:44find out
05:44कर लूँ
05:45कि पहले
05:46कौन सा
05:46cut लगेगा
05:47chocolate के उपर
05:48बाद में
05:48कौन सा
05:49cut लगेगा
05:49और क्यूंकि
05:50मुझे cost के
05:51basis पर
05:52minimize करना है
05:53मतलब
05:53मुझे cost को
05:54minimum लेकर आना है
05:55और greedy approach
05:56को मुझे follow करना है
05:57तो मैं या से
05:58थोड़ थोड़
05:58logic लगा सकती हूँ
05:59कि cost के basis पर
06:00sorting होनी चाहिए
06:01और वो sorting क्यों होनी चाहिए
06:03उसका हम एक बार
06:04logic समझते है
06:04logically अगर मैं देखू
06:06मान लेते हैं
06:06मेरे पास ये problem है
06:08जिसके अंदर
06:09मेरे पास
06:09chocolate है
06:10जिसको मैं
06:12चार हिस्सों में
06:13divide कर सकती हूँ
06:14इस cut का
06:15मुझे cost दिया हूँ है
06:29मेरे चॉकलेट को ऐसे cut कर दिया
06:30तो मेरे चॉकलेट क्या हूई
06:31मेरे चॉकलेट दो हिस्सों में
06:33divide हो गई
06:33पहला हिस्सा ये हिस्सा हो गया
06:35दूसरा हिस्सा मेरे चॉकलेट का
06:37ये वाला हिस्सा हो गया
06:38और cost कितनी लगी अभी तक मुझे
06:40cost मुझे लगी है
07:102
07:223
07:253
07:283
07:283
07:293
07:303
07:303
07:303
07:303
07:303
07:303
07:304
07:333
07:353
07:363
07:374
07:404
07:413
07:42in this case. When I have what to do, I have vertical cut before and horizontal cut later.
07:47My cost is 8. Now I have case 2. Case 2 will be where I have horizontal cut before.
07:57I have horizontal cut before, I will divide this way so that my horizontal cost will
08:05change. So, my chocolate will be one upper part and the chocolate will be one lower part.
08:12Cost is equal to 3. Because horizontal cut is equal to 3. Now I have to divide this
08:19into small squares. So, I will place vertical cut. Vertical cut is equal to 3.
08:28So, the first part of the cut cut is equal to 2.
08:31The second part of the cut cut is equal to 2. So, here the cost of the cut cut is
08:38equal to 2.
08:38So, the cost of the total is equal to 3 plus 2 plus 2. This value is equal to 7.
08:45So, there is one case in which I have 4 squares.
08:50But in one case, the cost is 7 and the cost is 8.
08:54So, which case is favorable for me? This chocolate is the best case possible.
09:00The cost of the cut cut is the horizontal cut. Because what happened to me?
09:02My cost is the minimum.
09:05Now, in this same scenario, I can infer 2-3 things.
09:09I can remove 2-3 things.
09:09I can remove 2-3 things.
09:10The first thing I have to remove is that I have to use all cuts.
09:19I have to use all cuts.
09:22The problem is that I have to decide in order of cuts.
09:26The problem is that I have to decide in order of cuts.
09:29Also, in the same environment, which cut cut should I place?
09:31Chocolate would I have to divide first here or here?
09:35This is my first problem.
09:37Second, which I have to do, is that every time,
09:41If I do this example, have to take 2 cuts.
09:45For cost, I have to do cuts more, but it will be extra gain.
09:49So, I have to use all cuts to all cuts because I need to use all cuts.
09:51that first which will be cut and after which will be cut, this is the real question.
09:56The other thing which I can infer, which I have found out, is logically that you see,
10:02as we put one cut, the way it is that we increase the parts of chocolate,
10:07the pieces of chocolate increase. So, when the next cut will be put,
10:11there will be the cost of the cost.
10:13Like here, if I put a vertical cut, then the chocolate will be one part of the other part.
10:19The next thing, when I put a horizontal cut, then it will be two times,
10:23which I have the cost of the cost.
10:25Here, if I put a horizontal cut, then the chocolate will be two parts.
10:30Now, if I put a vertical cut, then I have to put the cost of the cost,
10:34which I have the cost of the cost.
10:37So, the other thing is that after each cut,
10:41what happens after each cut,
10:50will be cut and cut all pieces.
10:50Take the heat if I put a little period Artemis right here,
10:53if they nav permit a small cut cut with a little cut cut
10:56from the left then,
11:02which I have your prede grey measurements are more than the Или note .
11:05Which can be cut it the last indefree?
11:07If I use cut cut cutes only after the anymore cut,
11:13then, does not happen I have theiberated cut cut either?
11:17If I put the общем cut cut in..
11:20The cost was 2 and 3.
11:23What did I do?
11:25The cost was 3.
11:27The cost was 3.
11:27I had to leave 3 to 2.
11:29I had to leave 3 to 2.
11:30The cost was more expensive.
11:33The cost was less expensive.
11:36The cost was less expensive.
11:39I had to leave 3.
11:41I had to leave 3.
11:44The cost was less expensive.
11:46What did I do?
11:47I had to leave 3 more expensive.
11:49I had to leave 3 horizontal cuts.
11:51When I did vertical cuts,
11:54it will be multiplied by 2.
11:56It will also be vertical.
11:58My overall cost will be less.
12:02Logically, I have to leave
12:04expensive cuts.
12:08I have to leave.
12:09I have to leave.
12:11And a little cheap cuts.
12:12I have to leave.
12:15Because after that,
12:17the cost is cut.
12:21The cost is more expensive.
12:29If I have to leave 4.
12:33I have to leave 4.
12:37The cost is more expensive.
12:39The cost is more expensive.
12:39The cost is more expensive.
12:39I have to leave.
12:42If I have 4 horizontal pieces.
12:45If I have to leave 4 horizontal pieces.
12:45And I have to leave here.
12:46And I put here vertical cut.
12:47So, I could leave here.
12:48If it's called X.
12:50Here I have to leave cost of X.
12:52Here I will also leave.
12:53Here I have to leave, here I have to leave.
12:54Here I have to leave and here I have to leave.
12:56The cost of vertical cut here.
13:00by x
13:01How much will it be?
13:02Cost of vertical cut
13:03multiplied by x
13:08This is the way x
13:09This way, the horizontal pieces
13:12, the way through
13:13If I have cut
13:14, that it will be individually
13:16so the two pieces
13:18multiplied by x
13:20In the same way,
13:21I can visualize
13:22that if I have 4 vertical pieces
13:25way. Vertical pieces
13:26means my first piece
13:28is like this. Second chocolate
13:29piece is like this. Third piece
13:31is like this and fourth piece is like this.
13:33Now I'm going to put a horizontal cut
13:35and this horizontal cut
13:37value, the cost
13:39is like this. So I have to give the cost
13:41here and here and here
13:43and here and here. So I have to give it
13:46here. So here I have to get the cost
13:47of horizontal cut
13:51how much is it?
13:52This is how much I have
13:54vertical pieces. Vertical pieces
13:57multiplied by Y.
13:58Which is the cost of this cut.
14:01So these 3-4 things I have
14:02inferred here are very important.
14:04First of all, I have to put all the cuts
14:07at the end of the day. Because I have to divide
14:09the chocolate in single squares.
14:11But the problem is that what the cuts need to be.
14:14The other important thing is that
14:16after each cut, my chocolate pieces
14:18increase. So after all,
14:21I have to put more cuts on the cut
14:22I put the expensive cuts on the cut
14:24and the cost will increase,
14:26it will get more cost. So I will put
14:28cheap cuts on the cut.
14:29So I put the small cuts on the cut
14:31after that.
14:32After that, I have to run a
14:33more detail.
14:33That the vertical cuts
14:35will be the cost of horizontal pieces
14:37multiplied by X.
14:41And if I put a horizontal cut, then it will be the vertical pieces multiplied by this value Y.
14:47So first of all, what will I do?
14:49We will combine these three things and discuss our greedy solutions.
14:53And after that, I'm going to write its pseudo code.
14:56My greedy solution will be something like that,
14:59that I know that I have to do the cuts first.
15:02So what will I do?
15:03I will sort on the basis of our cost.
15:06My vertical cuts, if I sort the vertical cuts,
15:11I will sort on the basis of the cost.
15:14If I sort the value of 4, then it will be 3, then it will be 2, then it will
15:19be 1, then it will be 1.
15:20So I will sort them in the descending order,
15:23because I have to do the cuts first.
15:25And if I want to sort the horizontal cuts,
15:27if I want to sort them,
15:30I mean, this is my horizontal cut.
15:33I will sort them in the values.
15:36So first, it will be 4, then it will 2, then it will 1.
15:39Now I will compare the vertical and horizontal.
15:43From the beginning,
15:44I will compare the cuts to which cuts are the most expensive.
15:46I mean, 1 pointer here and 1 pointer here.
15:49They will see who the costliest is.
15:51If both values are equal,
15:53then I will do a vertical cut here.
15:55I mean,
15:57I mean,
15:57I have 4 with a vertical cut.
16:004 was my cost here.
16:01Here I have a vertical cut here.
16:03Here I have a vertical cut on my chocolate.
16:04So my chocolate will divide in two parts.
16:06This is my part 1.
16:07And this is my part 2.
16:10So this is my first cut.
16:12Now, how much cost of this cut?
16:13How much cost of this cut?
16:13Here I will calculate the cost.
16:15Now I will cut the cut cut off my 4.
16:18It will cut the cost of this cut.
16:18VonVortical cut must be 4.
16:21So the cost is equal to 4.
16:23It will be 4.
16:24I will use this 4.
16:25This 4 was cut cut cut cut.
16:27This 4 was cut cut.
16:27This 4 was cut cut cut cut.
16:29Now, I will compare each other.
16:29Now I will compare the 4 with a much expensive cut cut.
16:32For this 4,
16:33it will be more expensive.
16:34So, my 4 was cut cut cut down.
16:37Another cut cut cut of this cut has been horizontal cut.
16:39The 4 is cut cut here.
16:40The cut cut cut here.
16:43I have cut from here and cut from here.
16:46Now my chocolate will divide into 4.
16:48This is my first part, this is my second part, this is my third part and this is my fourth
16:55part.
16:55So you notice that every time chocolate pieces are increasing.
16:58And as I have cut from here, I mean cost was 4.
17:02So one was cut from here and one was cut from here.
17:08So I have to give one cost here and one cost here.
17:11So cost will be 4 plus 4.
17:14So cost will be 4 plus 4, 8 add.
17:17So this will be 4 plus 4 is equal to 12.
17:21Now I have cut from here.
17:23So I will come to my next value.
17:25Now compare to 3 and 2.
17:28Obviously 3 is bigger.
17:29So we will have vertical cut from here.
17:31So I will cut from here.
17:33So my chocolate will cut from here.
17:37Now how will the chocolate?
17:37This is how possible to come 1.1
17:39,2,3,4,5,1igue. Now
17:42I have cut from here. This
17:44cut isルündar How
17:45many? How
17:46many pieces potem are running a cut? This
17:48was. This
17:501 piece was. This
17:51was the first piece. This
17:52was the first was through. And
17:53this one piece was cut. Now
17:56on 1 piece was cut from it. So
17:582 times cost 만들 here.
18:032 times multiplied by 3 is equal to 6
18:05So, my cost, I will add 6 to 6
18:08So, 12 plus 6
18:09Cost will be 18 equal
18:11Now, what do I do?
18:133 cost I have already taken
18:14Now, I will come to my next value 2
18:16Now, I will compare 2 and 2
18:18Which cost? I will compare 2 to 2
18:20Which cost? I will compare 2 to 2
18:20I will compare 2 to 2
18:21I will not have a difference
18:21Final answer will come
18:23So, I will take a vertical cut
18:25Vertical cut means that I will talk to 2
18:27Vertical cut
18:28I will talk to this cost
18:30I will put a cut here
18:32I will put a cut in my chocolate
18:33Here, like I have put a cut
18:35Now, my chocolate will divide 1, 2, 3, 4, 5, 6, 7, 8
18:40In divide, first 6 pieces were 8 pieces
18:42Here, like I have put a cut
18:44How much cost will this cost?
18:461 will cut this part
18:471 will cut this part
18:49So, the total cost will be equal to 2 multiplied by 2
18:532 in 2 is cost
18:552 into 2 is 4
18:56So, I will add 4 to this
18:59This will be 22
19:00And here, I will increase my value 2 to 1
19:03Now, I will compare 1 to 2
19:05Which cost is the big cost?
19:07Which cost is the big cost?
19:08This cost is the big cost
19:09Where do I put it?
19:10Horizontal 2
19:10This is the big cost
19:12So, I will apply this cut
19:13I will apply here
19:14Here I will cut the chocolate
19:15Here, I will cut the chocolate
19:17Here I will cut the chocolate
19:17Here I will cut the chocolate
19:18How many parts are there?
19:18How many parts are there?
19:191, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11
19:2312 pieces of chocolate and as I cut how many parts I cut through, 1 cut through, 1 cut through,
19:311 cut through, 1 cut through and 1 cut through.
19:34So total 4 parts cross my horizontal cut.
19:38So cost will be 4 multiplied by 2 which is equal to 8.
19:42So here I will add 8 to cost, my cost will be equal to 30.
19:47Now I will update my pointer from 2 to 1.
19:52Now this cost will be equal to 1.
19:55Which cost will be equal to 1?
19:56We will take vertical 1.
19:58Vertical 1 is equal to this one.
20:00Or we can take this one, the cost and the answer will be the same.
20:04We can take this one.
20:05If I apply here, my chocolate will be cut from here.
20:09Total is how many pieces will be?
20:101, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 pieces.
20:18Now my chocolate will be cut from here.
20:19And this is the cut.
20:21How much cost is the cost?
20:22How much cost is the cost?
20:231 piece through, 1 piece through, 1 piece through, 1 piece through.
20:26So 3 times cost is the cost.
20:28How much cost is the cost?
20:281 is equal to cost.
20:30So I will add 3 to cost.
20:32Cost will be 33.
20:34I will update my pointer here.
20:36Now I will increase the pointer here.
20:39Now we will compare again.
20:40How much cost is the cost of 1 and 1?
20:42If we take this vertical 1 to higher,
20:44then there will be another cost.
20:46I will cut it from here.
20:48How many pieces will be cut?
20:501, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12,
20:5313, 14, 15, 16, 17, 18 pieces.
20:57How much cost is the cost?
20:591 piece is cut.
21:001 piece is cut.
21:021 piece is cut.
21:021 piece is cut.
21:03The total of 3 pieces is cut.
21:04How many costs is?
21:05How many costs?
21:06I will add 1.
21:07Then I will add 3 to the cost.
21:09I add 3 to the cost.
21:11The cost is equal to 36.
21:14Now I will update this pointer.
21:16This pointer will be updated when following this area.
21:19So I have then put all the vertical cuts.
21:21I will put all the first cuts.
21:23The 2st, the 3rd, the 4th, the 5th, the 5th.
21:26The other vertical cuts are all the vertical cuts.
21:28Now these are all the horizontal cuts left.
21:30These two are taken from here.
21:31one here and one here. Now, if I add one to another, we also add it.
21:34Whatever cuts will change and add it, I have added one to another here.
21:38So, as I added one to another, how many pieces are made?
21:411, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20,
21:4721, 22, 23, 24.
21:4924 pieces are made, which is equal to n into m, which is equal to 6 into 4.
21:54And how much cost is made by putting this cut?
21:561 piece through, 2 pieces, 3 pieces, 4 pieces, 5 pieces, 6 pieces.
22:016 pieces through, my cut will be cut.
22:03So, cost will be 6 multiplied by 1, which is equal to 6.
22:07So, here, cost will be 6.
22:09So, total cost will be equal to 42.
22:13And here, the pointer will be update.
22:15So, I put all vertical cuts, all horizontal cuts.
22:21What was important for these cuts?
22:23Their order was important.
22:24That first, which is vertical and which is horizontal.
22:27When I put these cuts, I divided my whole chocolate.
22:31In single square pieces.
22:32The total minimum cost.
22:34This is my minimum cost.
22:35That is equal to 42.
22:38Now, if I write my pseudo code.
22:40So, how will it be?
22:42In pseudo code, I will take my variable h,
22:45I will take one variable, v.
22:47Both start from 0.
22:49H, which is horizontal cut?
22:51V, which is vertical cut?
22:54Before I take them, I will take them.
22:58Horizontal and vertical cuts.
23:00I will sort in descending order.
23:03After that, I will take H and V pointers.
23:05What will I do?
23:07I will take another value.
23:08One is my horizontal pieces, which in the beginning is one equal.
23:11And one is my vertical pieces.
23:13This is also one equal.
23:14In the beginning, chocolate is a big chunk.
23:18There is a horizontal piece and a vertical piece.
23:22I will track them every time.
23:24So, I have to run a loop.
23:25When my h is less than my horizontal.length.
23:31And my v is less than my vertical.length.
23:34So, I have made the arrays, which was my horizontal array.
23:38And which was my vertical array.
23:40I have taken the pointer.
23:42This pointer is my pointer.
23:44This pointer will be my pointer.
23:44This pointer will be my pointer.
23:45This pointer will be my pointer.
23:45This pointer will be my pointer.
23:46So, these are my pointer.
23:47I have written in pseudo code.
23:49Now, I will run a loop.
23:50Until these two are less than total length.
23:55What will I do?
23:56I will compare.
23:57Which is less than h, if the value is less than v. value is less than v. value.
24:01Then, I will take the v. value of vertical cut.
24:04So, in that case, I will put the vertical cut.
24:08No.
24:09Then, I will put the horizontal cut.
24:15And what will last?
24:16As many horizontal cuts will change.
24:18The horizontal cuts will change.
24:20We will put the horizontal cuts.
24:20As many vertical cuts will remain.
24:22In last, we will put the vertical cuts.
24:23And with these cuts, I will remove the cost and update my vertical pieces and horizontal pieces.
24:31Overall, I have 4 pointers.
24:35For H and V, I will track the horizontal pieces and vertical pieces.
24:40I will run a loop until my H value is less horizontal and vertical.
24:46If H is less than V, I want to make vertical cuts.
24:49I want to make horizontal cuts.
24:51How are these cuts?
24:52Actually, we will analyze the code and analyze it.
24:55Like vertical cuts, I will increase my vertical pieces.
24:59Like horizontal cuts, I will increase my horizontal pieces.
25:02In vertical pieces, I will increase my horizontal pieces.
25:06And how much cost of vertical cuts?
25:08The cost of horizontal pieces is cut.
25:12And how much cost of horizontal cuts?
25:14The cost of vertical pieces is cut.
25:17We will define all the logic in code.
25:19We will define all the cuts in the last.
25:22We will apply all the cuts in the last.
25:23And finally, we will print the cost.
25:25Now, we have discussed a lot of details in dry run.
25:27We have discussed our chocolate problem.
25:29For this, we will write code.
25:31And we will write code.
25:32You will understand code.
25:34After that, you will see this solution again.
25:37And then, you will see this pseudo code again.
25:38So, these things will be clearer.
25:40Hello, Jenny.
25:40Thank you, Jenny.
25:40Okay.