Teleporting Ants & Dynamic Programming #SoME2

preview_player
Показать описание
Codeforces Global Round 15
Problem 1552F. Telepanting

Please leave any constructive criticism in the comments!

Submission to 3b1b's Summer of Math Exposition 2

Written and Animated by: Henry Liu, Samuel Brashears
Produced and Narrated by: Henry Liu
Channel Art by: Amanda Nguyen

Рекомендации по теме
Комментарии
Автор

for all I know (I have rating of 2194 on cf) for any solution to this problem, we would need to find first x[j] greater than y[i] for all i=1..n and we would either need to sort y, or do n binary searches. We can't do n binary searches in O(n) time, and we can't sort general array in less than O(n log n), but what we can do is use constraint that y[i] < 1e9 and use radix sort(basically 2 count sorts in this case, but for numbers not up to 1e9 but 1e4.5, so linear time since n is up to 2e5), but this feels very unsatisfactory and leaves savory taste in mouth, so I would leave this problem at being O(n log n)

rwarazor
Автор

No one can explain a 2200-rated problem better than this... definitely made me a bit wiser.

gurjotcheema
Автор

This video actually looks like one of those 4m subscriber channels, great job man!

shivam-tiwari
Автор

I solved the problem much differently and pure mathematically before watching the video. it is very hard to explain a solution in text form but the main idea was to see the portals as binary numbers. I split the problem into two. evaluating how many times the player has entered a portal (to teleport not passover) and then calculating the total distance traveled. The second problem's solution was easy I just had to calculate the difference in position between the portal and the teleportation target to calculate the "cost" of the teleportation. the total length of the line plus all the costs were equal to the total distance traveled. now for the solution to the first part, look at the first example with 4 portals (red, orange, yellow, green). in the solution of this part ignore all the distances. if you look at the first red portal it teleports the player right behind itself and is thus equivalent to a binary number with one digit. orange also is a one digit number but yellow perfectly encapsulates orange but no other portal so they collectively create two digit binary number. green is encapsulating the entire one digit and two digit numbers. green is not symbolized as 3 digit but (1 + 2)+1 digits. plus operation is not summation in this notation but symbolizes that those two numbers have to be calculated independently.
so collectively the table for the puzzle is 1+2+(1+2+1).

we have to take the initial positions of the portals into account. the first one digit portal is closed so it has the value of 1. notice 1 is the maximum value a one digit binary number can hold so the next stop over it will pass it.
orange and yellow collectively two digit number seems to have the value (0x01=1) but you can observe they act as inverted. so actually they have the value (0x10 = 2) of 2.
and lastly green was symbolizing the entire copy of the puzzle plus itself but when the player passes it, all the portals will be open. so the value for green is (0 + 0 + 0)

binary digit table = 1+2+(1+2+1)
initial value table = 1+2+(0+0+0)

we are close to be able to calculate total teleportation count. just remember that one digit binary numbers can hold maximum of 1 and leak for the value of 2. similarly two digit numbers can hold the maximum of 3 and leaks for the value of 4.

binary digit table = 1+2+(1+2+1)
initial value table = 1+2+(0+0+0)
max value table = 1+3+(1+3+1)
difference table = 0+1+(1+3+1)
when we sum the difference table 1+1+3+1 = 6 you can see that player will teleport 6 times.

conveying the idea behind these tables isn't that easy in text form so if they are not clear try to read it again and calculate them by yourself this is the best that I can do.

lastls we have to calculate the costs. as I said calculating the cost for a portal is easy we have to calculate the distance between the portal and the target but when we unite two portals into a multi digit number since the two portals creating that multi digit (in this example two) number can have different costs we have to symbolize them independently. for this example in the case of orange and yellow portals, their costs are 1 and 3 so we will notate them as (1+3) for the 2 digit number. this says that if the player enters the first digit the cost is 1 and if it enters the second digit the cost is 3. we need to somehow evaluate how many times which digit will flip while counting in binary. I will calculate that for the general case later but for two digit binary (00 10 01 11) the first digit will flip every time and the second digit will flip every two times. collectively first 4 times and second 2 times.

let me write the tables again this time with the cost table too.

cost table = 1+(1+3)+1+(1+3)+7
binary digit table = 1 + 2 + (1 + 2 + 1)
initial value table = 1 + 2 + (0 + 0 + 0)
difference table = 0 + 1 + (1 + 3 + 1)
total cost table = 0+(1+0)+1+(1+3+1)+7 = 14
total distance travelled = cost + distance = 14 + 9 = 23

the reason for the cost for the second two digit number to be not (1+3) but (1+3+1) is because while counting 00 10 01 11 first digit is flipped from 0 to 1 twice and second digit once.

thus when the binary digit table is constructed total distance traveled can be evaluated mathematically with near zero computational cost.
I have spent around 2 hours for the solution and an additional 2 hours watching the video and writing this comment. I'm very happy if you have read it to here and I hope you like the solution. have a nice day.

furkanunsal
Автор

The sum on contiguous elements idea was genius, I never would have thought of that but it's so obvious in hindsight

__
Автор

Probably the best explanation to a problem I've ever seen

salaheddin
Автор

As a green coder (1200-1400 CF) I can say that I understood the 2200 rated problems solution proving that this guy have put a great work into his explanation!

tagberli
Автор

I'm shocked that this is the first video on this channel, the quality is something I would expect it to take months or even years to achieve. Can't wait for the next one!!!

jakegoat
Автор

Awesome video. I noticed this is your first on YouTube, I hope you keep it up! Really tough problem but you explained it well. Always wished there was a 3b1b for data structure and algs. Could be you.

saurabhshah
Автор

So I might have taken your challenge to solve this problem in O(n) a bit too seriously.
It took a variant of counting sort to allow building a hash map from each exit to the closest entrance in O(n), so we can skip the binary search and replace it with an access to the hash map in O(1), thus resulting in a total complexity of O(n) (where n is the length of the road rather than the portal count, though).

gzethicus
Автор

i'm completely shocked that this is the only video this channel has posted, this is incredible. thank you for putting in the work to make free educational resources

bimbom
Автор

i really liked the animations, especially the last part where the lines of code are highlighted each step and you can see where the results move to.

RayZhaTV
Автор

You are an awesome storyteller and a talented movie maker, and not to mention an excellent teacher whatsoever.

mathguy
Автор

I really liked how you showed how much DP speeds things up at 7:18. I also like how you cut off the music at 6:37 when making your conclusion. One criticism I have is you sometimes show graphics on the screen only while you talk about it, so it's hard to visually digest what you're showing and listen in that short time (ie sometimes it's a bit fast such as at 5:30)

i_am_acai
Автор

the motion graphics illustrating the algorithm are absolutely delightful!

Sofia-tsgy
Автор

This is the best video on the internet, until you upload the next one

angelasun
Автор

The animations were really nice, particularly for the DP graph visualization. Very creative.

thestemgamer
Автор

This is a great video! But the part near 6:40, explaining how dp was calculated was paced a bit too quick, and I had to rewatch that part to understand it. Otherwise, a great explanation of a hard problem.

somebodyhere
Автор

Fantastic visuals. I wonder how many followed it all while watching the video without pausing it. I sure didn't. But still I'm impressed.

FloydMaxwell
Автор

Absolute gold. Keep it up. Wish to see several algorithm implementations and problems in this format.

mananshah
visit shbcf.ru