UVA11300 Spreading the wealth

Source: Internet
Author: User

problem

A communist regime is trying to redistribute wealth in a village. They has decided to sit everyone around a circular table. First, everyone has converted all of their properties to coins of equal value, such this total number of coins are divi Sible by the number of people in the village. Finally, each person gives a number of coins to the person on the He right and a number coins to the person on the he left, such That's in the end and everyone has the same number of coins. Given the number of coins of all, compute the minimum number of coins that must is transferred using this method s O that everyone has the same number of coins.

The Input

There is a number of inputs. Each input is begins with n(n<1000001) and the number of people in the village. n lines follow, giving the number of coins in the village, in counterclockwise order around the TA ble. The total number of coins would fit inside an unsigned-bit integer.

The Output

For each input, the output of the minimum number is coins that must is transferred on a.

Sample Input

310010010041254

Sample Output

04

Problem Setter:josh Bao

/* Divide money only left and right points, ask the minimum number of coins to use A[i]: The number of money currently owned XI: Indicates that I give i+1 the number of money, is a parting, negative expression of income M: After the completion of the state (positive, negative income) x1 m = a[1] + x1-x2- --x2 = a[1]-m + x1 = b[1] + x1 m = a[2] + x2-x3---x3 = a[2]-m + x2 = b[2] + x2 = b[1] + b[2] + x1 ..... ... xn = SUM (b[1] + ... + b[n-1]) + x1 answer = MIN (abs (XI-X1) + ... + abs (XI-XN)) (1 ... n) median */#include &L    t;cstdio> #include <cstring> #include <algorithm> #include <cmath>using namespace Std;int main () {    int n;    int a[1100000];    int c[1100000];        while (~SCANF ("%d", &n)) {long Long sum = 0;            for (int i = 1; I <= n; i++) {scanf ("%d", &a[i]);        Sum + = A[i];        } int temp = sum/n;        C[0] = 0;        for (int i = 1; i < n; i++) c[i] = C[i-1] + a[i]-temp;        Sort (c, C + N);        int x = C[N/2];        sum = 0;        for (int i = 0; i < n; i++) {sum + = Fabs (C[i]-X);    } printf ("%lld\n", sum);    }return 0;} 

  

UVA11300 Spreading the wealth

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.