Greedy Practice (ii) Cargo handling

Source: Internet
Author: User

Title Description

Heaven and earth heartless people sentient beings, one side have difficult p plus support! After the Wenchuan earthquake, the disaster area is the most shortage of relief tents, supporting tents across the country are urgently transported to the disaster area. Suppose that there is a storage point around the Kagawa of N relief tents with circular arrangement, the number of tents per storage point is m1,m2,......,mn, and s=m1+m2+......+mn must be a multiple of n. Any number of tents can be transported to adjacent storage points at any one of the storage points.
Now we need to find a way to carry the fewest tents so that the number of tents in each storage point is the same.
For example: N=5, the number of tents per storage point is 17 9 14 16 4 respectively, we carry out the following handling:
(1) Storage point ① to the storage point ② handling 1 tents;
(2) Storage point ① to the storage point ⑤ handling 4 tents;
(3) Storage point ③ to the storage point ② handling 2 tents;
(4) Storage point ④ 4 tents to the storage point ⑤.
The total number of moving tents is 1+4+2+4=11 and can prove to be the best handling method.

Input

The 1th line is a positive integer n (n≤10000), which indicates that there are n storage points;
Row 2nd n integers (integer range) representing the number of tents in n storage points.

Output

An integer representing the minimum number of tents to be transported.

Sample input
517 9 14) 16 4
Sample output
11
This is similar to the split card deformation, is to find the average, the use of the array to move, but because the minimum value required, so the need to move several times until the minimum value.

#include <iostream> #include <cmath>using namespace Std;int main () {    long long a[20001];    Long long b[20001];    int N;    Long long sum=0;    Long long cha=0,su=0;    Long long max=9223372036854775807;    cin>>n;    for (int i=0;i<n;i++) {        cin>>a[i];        A[i+n]=a[i];        Sum+=a[i];    }    Long long avg=sum/n;    for (int i=0;i<n;i++) {        su=0;        for (int j=i;j<n+i;j++) b[j-i+1]=a[j];        for (int k=0;k<n;k++) {            if (b[k]!=avg) {                cha=b[k]-avg;                B[k+1]+=cha;                B[k]=avg;                Su+=abs (CHA);            }        }        if (Su<max) max=su;    }    cout<<max<<endl;    return 0;}

  

Greedy Practice (ii) Cargo handling

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.