There is a Taobao merchant who has n warehouses in a city and each warehouse has different storage volumes. Now we need to transport the goods, the storage volume of each warehouse is the same, and the transportation lines between n warehouses are surrounded by a circle, that is, 1-> 2-> 3-> 4->... -> N-> 1-> ..., Goods can only be transported through a connected warehouse. The minimum transportation cost (transportation volume * distance) is designed to meet the requirements of Taobao merchants and the code is written.
Solution:
Assume that the initial storage volume of n warehouses is warehouse [1], warehouse [2],…, Warehouse [n]
Calculate the average storage volume
Average = (warehouse [1] + warehouse [2] +... + Warehouse [n])/n
Even in the final result, each warehouse should have inventory
First, ship k from warehouse 1 to warehouse n;
Then, a specific value is shipped down from 1 to N-1 so that the margin of each warehouse is average. The remaining problem is to find the minimum value of the total cost.
Set Step 1 to migrate data from warehouse 1 to warehouse n (note that the path length is 1 because it is a circle). k can be negative. If it is a negative number, meaning transport from n to 1 | k | inventory, and then transport from (1 to n-1) to I + 1 warehouse, the amount of transportation must ensure that the I warehouse is equal to the average after transportation.
Step 2 (ship k from warehouse 1 to warehouse n): Cost: | k |,
Step 2 (make sure that the margin of warehouse 1 is average): The cost is
| Warehouse [1]-average-k |
That is, transport from 1 to 2 groups from 2 to 1.
Step 2 (make sure that the margin of Warehouse 2 is average): cost is
| Warehouse [2] + warehouse [1]-average-k-average | = | warehouse [1] + warehouse [2]-2average-k |
...
N-1. Step n-1: The price is
| Warehouse [1] + warehouse [2] +... + Warehouse [n-1]-(n-1) average-k |
At this time, the remaining goods volume in the warehouse n:
(Warehouse [n] + k) + warehouse [1] + warehouse [2] +... + Warehouse [n-1]-(n-1) average-k = (warehouse [1] + warehouse [2] +... + Warehouse [n]-(n-1) average = average
This is exactly the same. In fact, we don't need to deduce it here, because the average value is well calculated, so we must have done it right away.
The total cost is:
| K | + | warehouse [1]-average-k | + | warehouse [1] + a [2]-2average-k | +... + | Warehouse [1] + warehouse [2] +... + Warehouse [n-1]-(n-1) average-k |
Make sum [I] = warehouse [1] + warehouse [2] +... + Warehouse [I]-I * average
Then, the total cost can be expressed as: | k | + | sum [1]-k | + | sum [2]-k | +... + | Sum [n-1]-k |
This formula can be regarded as finding a vertex k on the horizontal number axis, so that the vertex k can reach 0, sum [1], sum [2], sum [3],…, The sum of the distance of sum [n-1] is the smallest. Obviously k should take the median of the n number. Now the problem is solved.
Provide detailed comments:
# Include "stdafx. h"
Include <iostream>
Include <algorithm>
Include <string>
Using namespace std;
Const int X = 100000;
Double sum [X], warehouse [X];
Int n;
Double Abs (double x)
{
Return max (x,-x );
}
Int _ tmain (int argc, _ TCHAR * argv [])
{
While (true)
{
Double total = 0;
Double mid = 0;
Cout <"enter the number of warehouses :";
Cin> n;
// Read the values of n warehouses and calculate the total number
For (int I = 1; I <= n; I ++)
{
Cout <"enter" <I <"inventory of warehouses :";
Cin> warehouse [I];
Total + = warehouse [I];
}
// Calculate the final stored value of each warehouse
Double average = total/n;
// Calculate the sum array
For (int I = 1; I <n; I ++)
Sum [I] = warehouse [I] + sum [i-1]-average;
// Demedian after sorting
// Sort adopts semi-open and semi-closed intervals, so the sorting is 0 ~ N-1
Sort (sum, sum + n );
// You can give yourself a number to understand this.
If (n % 2! = 0)
{
Mid = sum [n/2];
}
Else
{
Mid = (sum [n/2] + sum [n/2-1])/2;
}
Cout <"should start from 1, transport" <mid <"goods, and then ensure that the goods meet the conditions in turn" <endl;
Double ans = Abs (mid );
For (int I = 1; I <n; I ++)
Ans + = Abs (sum [I]-mid );
Cout <"Total cost:" <ans <endl;
Cout <"-----------------------------------" <endl;
}
Return 0;
}
For errors, please leave a message to point out ..