Pro.ID22178 TitleDepot Title链接http://10.20.2.8/oj/exercise/problem?problem_id=22178 AC0 Submit0 Ratio- 时间&空间限制描述On the circular road surrounding the city, there are N refueling depots. Every depot has a large warehouse (you may assume the storage capacity is infinite), and is connected with two other depots through oil pipes. As the circular road is very busy, each depot has to serve such a lot of cars everyday, so the oil is consumed almost the same rapidly at each depot. Usually the depots are respectively supplied by a center warehouse in the city. But recently a war has broken out in Middle East, so the oil supplies are in great shortage, and the center warehouse has no more oil to supply these N depots. But fortunately some of the depots still have a large amount of oil storage. And the oil can be transferred from one depot to another through oil pipes. So, to satisfy the consumption at each depot, the manager of the depots, has decided to rearrange the oil storage, so that each depot contains the same amount of oil. The oil transfer can be carried out in unlimited times. But for some strange reasons(security, or ..., who knows), each time the oil transfer should be carried out in a special way, that is a depot must transfer the same amount of oil to its two neighboring depots. What's more, the amount of oil transferred should always be in integer units, and of course can not exceed the current storage of the source depot. Since the oil transfer cost is proportional to the amount of oil transferred, and the manager is not sure whether it's possible to make such a transfer plan, so that each depot contains the same amount of oil. So now it's your task to write a program, first answer whether the manager's goal is possible, and if possible, find a transfer plan so that the total amount of oil transferred is minimized. 输入Input may contain several test cases. The first line is a positive integer T ( T ≤ 20 ), denoting the number of test cases followed. Each test case consists of two lines. The first line is a positive integer, N ( 3 ≤ N ≤ 50000 ), indicating the number of depots on the circular road. The second line contains N non-negative integers, a1, a2, ... an, ( ai ≤ 300 ), denoting the number of units of oil in depot 1, depot 2, ..., depot n. For each i satisfying 2 ≤ i < n, depot i is connected with depot i-1 and depot i+1, and depot 1 is also connected with depot n. 输出Description On the circular road surrounding the city, there are N refueling depots. Every depot has a large warehouse (you may assume the storage capacity is infinite), and is connected with two other depots through oil pipes. As the circular road is very busy, each depot has to serve such a lot of cars everyday, so the oil is consumed almost the same rapidly at each depot. Usually the depots are respectively supplied by a center warehouse in the city. But recently a war has broken out in Middle East, so the oil supplies are in great shortage, and the center warehouse has no more oil to supply these N depots. But fortunately some of the depots still have a large amount of oil storage. And the oil can be transferred from one depot to another through oil pipes. So, to satisfy the consumption at each depot, the manager of the depots, has decided to rearrange the oil storage, so that each depot contains the same amount of oil. The oil transfer can be carried out in unlimited times. But for some strange reasons(security, or ..., who knows), each time the oil transfer should be carried out in a special way, that is a depot must transfer the same amount of oil to its two neighboring depots. What's more, the amount of oil transferred should always be in integer units, and of course can not exceed the current storage of the source depot. Since the oil transfer cost is proportional to the amount of oil transferred, and the manager is not sure whether it's possible to make such a transfer plan, so that each depot contains the same amount of oil. So now it's your task to write a program, first answer whether the manager's goal is possible, and if possible, find a transfer plan so that the total amount of oil transferred is minimized. Input Input may contain several test cases. The first line is a positive integer T ( T ≤ 20 ), denoting the number of test cases followed. Each test case consists of two lines. The first line is a positive integer, N ( 3 ≤ N ≤ 50000 ), indicating the number of depots on the circular road. The second line contains N non-negative integers, a1, a2, ... an, ( ai ≤ 300 ), denoting the number of units of oil in depot 1, depot 2, ..., depot n. For each i satisfying 2 ≤ i < n, depot i is connected with depot i-1 and depot i+1, and depot 1 is also connected with depot n. Output For each test case, if it's impossible to make such a transfer plan, just output "Impossible" (without quotation mark). Otherwise, output an integer in one line, representing the minimum amount of units of oil that should be transferred from one depot to another. No extra blank is allowed. Sample Input 2 Sample Output 2 Source 样例输入2 样例输出2 提示作者 |