number of rounds passing choclate - algorithm

We are playing a game where there are n children sitting in a circle. Each of them has some number of chocolates. The total number of chocolates are such that they can be divided equally among all children.
In one round any one of children passes one chocolate to the left or to the right. We need to answer, minimally how many such rounds will it take for all of them to have the same number of chocolates.
The number of children n and the number of chocolates with each of them is given.
What algorithm shall we apply??

My heuristic would be:
while (not equally divided)
find kid A with the most chocolates
while (A has more that he should)
find closest kid B with fewer than he should
pass one from A to B and add to the number of moves (taking distance into account)

Related

Game puzzle, two players playing the game of replacing coin values

We have given, n coins with each having some face value k.
Now, there are two players, nick and james playing the game with alternating turn. In each turn, a player can choose any coin and replace it with more than one coins having sum of face value equal to replaced coin. Each of the new coins must be having same equal face value. Integer p is also given, which is the limit denotes that a player can't use the coin for replacement having face value less than p.
All players have given unlimited number of coins with unlimited face values.
So sample input will be n,k,p and where n is no. of coin with each face value is k and limit p is described above. If both play optimally, and nick starts first who will win the game. A player loose the game if he can't able to play its turn (means can't able to replace any of the coin).
Is it game of nim problem or DP? How can we solve this?
This is definitely the kind of game which Nim is the right way to think about: it is a two-player game, it is perfect information meaning both players know the full game state at all times, there is no element of chance, it is an impartial game meaning the same moves are available to both players, and you lose if you are unable to move. So the Sprague–Grundy theorem applies to this game; every position in the game is equivalent to a nimber. We can solve a position to find who will win given optimal play, by computing the nimber for the position; a position is a first-player-win if and only if the nimber is not zero.
However, that turns out to be completely unnecessary for this specific problem, because all of the coins in the starting position have the same value.
If there are an even number of coins, then player 2 wins by a mirror strategy. Match the coins in pairs, and then whatever move the opponent plays on one coin, mirror that move on the other one. Match the resulting coins in pairs and continue mirroring on them.
If there are an odd number of coins, then player 1 wins by splitting one of them into coins of the smallest possible value >= p, such that no more moves can be made on those coins. Then player 1 adopts the mirror strategy given above for the remaining coins, which there are an even number of.
There is a special case: if the coins' face value is already such that player 1 cannot make any move, then player 2 always wins, regardless of whether the number of coins is odd or even.
So the answer is: player 1 wins if and only if n is odd and k has a factor >= p. Clearly there is not going to be a more efficient solution.

Radius Nearest Neighbors - get number of neighbors in neighbors

I have coordinates. Each coordinates represent a building and gives number of resident. I would like to predict neighbors in radius 0.5km for any point. This point represents base stations. I want to calculate how many subscribers base station will cover. Anyway, I need number of neighbors in radius. According to my research, I found RadiusNearestNeighborsRegression technique. Bu this teqnique never give me the number of neighbors. It is only give the average value for all neighbors in radius. I need sum of neighbors output or number of neighbors. I will multiply average output and number of neighbors(thats gives me the total output for all neighbors.
I hope, I explained clearly.
You could partition the residences into regions such that, if the minimum distance between any 2 regions is too large, there would be no need to make comparisons between any pair of residences split between the 2 regions.
Also, when you have found that A & B are close enough, A would count towards B's neighbors and B would count towards A's w/ having to repeat that calculation.

Given lengths of 3 sides of a triangle, compute an average distance between the middle points of the sides

I have come across a problem. The problem statement is
A team of 3 people, is going to participate in a competition. According to the regulations of this competition, every team is given a single computer, which is located on a triangular table, and three chairs.
Team thinks that the most convenient location of participants is the one where each of the three participants sits at his/her own side of the triangular table, and, what’s important, exactly at the middle of this side. Of course, chairs should be put the same way.
It is important that, during the competition, the participants sit not very far one from another. The Dream Team’s captain thinks that a proper estimation of this factor is an average distance between all the pairs of these participants.
In the case of this competition, one have to compute an average distance between the middle points of the sides of a triangular table. Write a program which computes exactly this. Note that the distance is Euclidean – that is, the distance between (x1,y1) and (x2,y2) is sqrt((x_1 - x_2)^2 + (y_1 - y_2)^2).
Input
The input file contains three positive integer numbers not exceeding 100 – the lengths of sides of the table. It is guaranteed that such a table will have a non-zero area.
Output
Output the average distance between the middle points of the sides of the table, which was described in the input.
Examples
Input Output
3 4 5 2.00000000
5 5 7 2.83333333
I thought of one way to solve this
1. Assume origin as 1 point.
2. If one of the length is 3, assume the point as (3,0).
3. Now, I struck at finding 3rd coordinate
Is my approach, Ok?
Please, give me an algorithm to solve this.
Thank you
Note that segment, connecting middle of edge A and middle of edge B, is half of edge C and the same is true for other sides.
So solution is very simple - average distance for triangle with side lengths A,B,C is
M = (A/2 + B/2 + C/2) / 3 =
(A + B + C ) / 6

Optimal solution for traversing random grids on a board by 2 players

Consider an infinite 2D board. We have two players at points P1 and P2 on the board. They need to traverse a sequence of boxes on the board G1, G2, G3 .... Gn.
At the start only G1 is known. The coordinates of G2 to Gn are not known only after the box previous to it has been traversed. The players can move one at a time in one of the 8 possible directions on the board in unit time. We need to find the minimum time to traverse all required boxes using the two players.
The obvious solution is a greedy approach where the player nearer to the box that needs to be traversed moves towards it. Then we calculate the nearer player again for the next G. I feel a better solution exists to this problem that I cannot get my head around right now. Does a better solution exist?
I think as the board is infinite we should try to cover as much area with both players within n moves as possible (for every n). This way we maximize the abound of fields we can reach within n moves.
So my strategy would be:
Who is next to the next box?
Let this be P1.
Let P1 go to the box (shortest path) and go with the other player P2 in the directly opposite direction. This way we maximize the distance between the two players which minimizes the overlapping of the area they can reach in n steps. This way we maximize the coverage of the area the two players can reach in n steps for the next box.
Choosing the player closest to the next box is the best heuristic you can find.
Explanation: Whenever a new goal appears, there are only two options: Move player 1 or player 2 to the goal at the cost of the distance in fields. We also prefer a situation where players are far apart in contrast to close together. The most extreme case would be that both players are on the same field which is as good as having only one player. Since the playing field is infinite, being far apart is always better.
If this is correct, then you should ask yourself: Should I really choose the player being further away from the goal and end up in a situation where the players are closer together than they would have been if I had taken the other player?
Certainly not. In an infinite field, choosing the closest player helps with both, minimizing the current cost and improving the situation for the next goal (players far apart).
Since the problem is non deterministic, the solution must be heuristic.
The "price" at each turn is the number of moves taken in the turn. This can be PrN1 or PrN2 for the number of moves for player1 or player2 respectively in turn N.
The "score" at each turn can be thought of as the probability that a certain arrangement (the position of both players) after the moves will be a good arrangement for the rest of the turns.
You'd want to use an evaluation function that considers both price and score to make the decision.
The problem is, the only useful scoring function is one that is some function of the distance between the players (the larger the distance, the greater the chance of being closer to the next turns) and this is exactly in sync with the minimum price. Any choice that keeps the players as far apart as possible is necessarily the one that was cheapest to make.
What all that means if that the best algorithm is simply to move the closest player, which is the first instinct you had.
Had the board not been infinite, you could create a better scoring function, one that considers the probabilities of the next boxes which would give lower scores to arrangements that leave a player on the edges of the board.

Shortest distance travel - common meeting point

I came across this problem wherein there are a number of houses on a 2-D grid (their coordinates are given) and we essentially have to find which house can be used as a meeting point so that the distance traveled by everyone minimizes. Let us assume that a distance along the x or y-axis takes 1 unit and a distance to the diagonal neighbors takes (say) 1.2 units.
I cannot really think of a good optimization algorithm for this.
P.S: Not a homework problem. And I am only looking for an algorithm (not code) and if possible, its proof.
P.S #2: I am not looking for the Exhaustive solution. Believe it or not, that did strike me :)
As already pointed, in case of Manhattan distance the median gives a solution. This is an obvious conclusion from the well-known fact that median minimizes the mean of absolute deviation:
E|X-c| >= E|X-median(X)| for any constant c.
And here you can find an example of the proof for discrete case:
https://stats.stackexchange.com/questions/7307/mean-and-median-properties/7315#7315
This is probably really inefficient, but loop through all the houses, then loop through all the other houses. (nested for loops) Use the distance formula to find the distance between the 2 houses. Then you have the distance between every house. One quick and easy way to find which house is the closest distance is to add everyone's walking distance together for the given house. The house with the least total walking distance is the meeting area of choice.
I have been bugged by the same problem for some time now. The solution is the obvious consensus given in earlier posts: find the median (mx, my) independently and then find the point closest in the given N points and that is the meeting place. To see why this is actually the solution you should first consider the distance.
d = sum(|xi-x|) + sum(|yi-y|) over all 1<=i<=N,
which is independent in x and y. Hence we can solve the 1-D case for x and y. I will skip over the explanation given ^^ and hence conclude that (mx,my) is the best solution if we consider all possible points.The bigger challenge is to prove that we may move from (mx, my) to the closest (xi,yi) such that (xi, yi) is one of the given points, w/o changing(increasing) the distance. The proof goes:
Consider that we have sorted x-coordinates( for sake for proof ) and
that X1<X2<...<Xn. Also Xj<mx<X(j+1) where j = N/2, now let's move mx
one step to left, that is mx' <- mx-1.
Hence d' = |X1-mx+1| + .. + |Xj-mx+1| + |X(j+1)-mx+1| + .. + |Xn-mx+1|
We know that mx-1 will increase N/2 values( for k>=j+1 and decrease
for <=j ) hence the effect is the same. Thus (mx-1, my) gives the same
solution. It means that there is a space from Xj<mx<X(j+1) and
Yj<my<Y(j+1) where the distance does not change. Thus we can find the
closest such point which is the answer.
I have ignored the subtle case of even/odd nodes, but I hope the math works out itself when you realize the basic proof.
This is my first post, do help me improve my writing skills.
Your distance metric is weird. You'd expect that travelling on the diagonal should take at least sqrt(2) ~= 1.41 times the distance of travelling along a component direction, because that's how much further it is if travelling in a straight line along the diagonal by the Pythagorean theorem.
If you insisted on a manhattan distance (no diagonals allowed), then you'd want to pick the house closest to the median(x) + median(y) of the houses.
Consider the 1D case, you have a bunch of points on a line, and you want to pick the meeting spot. For concreteness/simplicity, let's say there are 5 houses, none duplicate.
Consider what happens as the meeting spot drifts away from the median to the right. For every unit away until you pass the 4th house left to right order, 3 people have to take an additional step to the right, and 2 people have to take one less step to the left, so the cost goes up by 1. Once you pass the 4th house, then 4 people have to taken an additional step to the right, and a single person has to take one less step to the left, so the cost increases by 3. An identical argument holds as you move the meeting spot to the left from the median. Moving away from the median always increases the cost.
The argument generalizes to any number of people, with or without duplicate houses, and even across to arbitrary number of dimensions, so long as you aren't allowed to use the diagonal.
Your problem is called Optimal Meeting Point Finding.
The following paper gives an efficient approximate algorithm
http://www.cse.ust.hk/~wilfred/paper/vldb11.pdf
Well, you could brute force it. Take each house and calculate the distance to each other house. Sum the distances up for each individual house. Then just grab the house that had the lowest sum.

Resources