Algorithm (prob. solving) achieving fastest runtime

后端 未结 4 1992
广开言路
广开言路 2021-01-31 11:19

For an algorithm competition training (not homework) we were given this question from a past year. Posted it to this site because the other site required a login.

This i

4条回答
  •  情话喂你
    2021-01-31 11:41

    There is no need to calculate every house!!!

    It's not fully developed, but I think it is worth thinking about it:

    modulo N

    N is the number of all houses, n shall be the "adress" (number) of some of the houses.

    If you walk around the island, you will find that n is raising by 1 for each house you pass. If you reach the house where n is N, then the next house has the number 1.

    Let us use a different system of numbering: increase every house-number by 1. Then n goes from 0 to N-1. this is the same way how numbers modulo N will behave.

    Litres is a function of the house-number n (modulo N)

    You can calculate the amount of liters for each house-Number by building the sum of all products of distance and people living there.

    You can also draw a graph of that function: x is n, y is the number of litres.

    the function is periodic

    If you understand what modulo means, you will understand that the graph you just did draw is just one periode of a periodic function, since Litre(n) ist eaqual to Litre(n + x * N) where x is a integer (that might be negative too).

    if N is big, the function is "Pseudo-continuous"

    What I mean is this: If N is really big, then the amount of litres will not change very much if you move from house a to its neighbour, house a+1. So you can use methods of interpolation.

    you are looking for the place of the "global" maximum of a periodic pseudo-continuous function (only really global within one periode)

    This is my suggestion:

    Step 1: select a distance d that is bigger than 1 and smaller than N. I can't say why, but I would use d=int(sqrt(N)) (maybee there might be a better choice, try it out).
    Step 2: calculate the Litres for House 0, d, 2d, 3d, ... Step 3: you will find some values that are higher than both of their neighbours. Use this high-points and their neighbours feed them using a method of interpolation to calculate more points close to that a high points (interval-splitting).

    Repeat this interpolations for other high points as long as you have time (you have 1 second, which is a long time!)

    Jump from one high-point to another if you see, that the global maximum must be elsewhere.

提交回复
热议问题