Sicily 1032. Depot

来源:互联网 发布:showgirl是什么软件 编辑:程序博客网 时间:2024/06/15 18:09

1032. Depot

Constraints

Time Limit: 1 secs, Memory Limit: 32 MB

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

234 1 131 0 1

Sample Output

2Impossible

Problem Source

ZSUACM Team Member

// Problem#: 1032// Submission#: 3585062// The source code is licensed under Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License// URI: http://creativecommons.org/licenses/by-nc-sa/3.0/// All Copyright reserved by Informatic Lab of Sun Yat-sen University#include <stdio.h>const int num = 50000;long long a[num], b[num], c[num], avg, sum, p;int n;int main() {    int i, total;    scanf("%d", &total);    while (total--) {        scanf("%d", &n);        avg = 0;        for (i = 0; i < n; i++) {            scanf("%lld", &a[i]);            avg += a[i];        }        if (avg % n != 0) {            printf("Impossible\n");            continue;        }        avg /= n;        sum = 0;        p = 0;        for (i = 1; i < n; i++) {            p += a[i] - avg;            sum += p;        }        if (sum % n != 0) {            printf("Impossible\n");            continue;        }        c[0] = -sum / n;        sum = 0;        b[0] = 0;        for (i = 1; i < n; i++) {            c[i] = c[i - 1] + a[i] - avg;            sum += c[i - 1];            if (sum > b[0]) b[0] = sum;        }        sum = b[0];        for (i = 1; i < n; i++) {            b[i] = b[i - 1] - c[i - 1];            sum += b[i];        }        printf("%lld\n", sum * 2);    }    return 0;}                                 


0 0