title:
categories:
tags:
-
mathjax: true
---
Problem Portal
Portal1: Luogu
Portal2: LibreOJ
Description
\ (G \) company \ (n-\) th line arranged along the annular rail transport warehouse, varying amounts of goods stored in each warehouse. How can you make with the least amount of handling \ (n \) the same number of warehouse inventory. When handling cargo, handling only between adjacent warehouse.
Input
The first file \ (1 \) line there \ (1 \) positive integers \ (n \) , expressed \ (n \) warehouse.
The first \ (2 \) line there \ (n \) positive integer representing \ (n \) inventory depots.
Output
Conveying the least amount of output.
Sample Input
5
17 9 14 16 4
Sample Output
11
Hint
For \ (100 \% \) test data: \ (. 1 \ n-Leq \ Leq 100 \) .
Solution
Although the network flow 24
problem, in fact, greedy + Sort enough.
Code
#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<cmath>
using namespace std;
const int MAXN=105;
int n, a[MAXN];
int main() {
scanf("%d",&n);
int sum=0;
for (int i=1; i<=n; i++) {
scanf("%d",&a[i]);
sum+=a[i];
}
sum/=n;
for (int i=1; i<=n; i++)
a[i]+=a[i-1]-sum;
sort(a+1, a+n+1);
sum=a[(n+1)>>1];//尽量用位运算,比较快
int ans=0;
for (int i=1; i<=n; i++)
ans+=abs(a[i]-sum);//计算每一个距离中间的位置
printf("%d\n",ans);
return 0;
}
Attachment
Test data Download: https://www.lanzous.com/i3juukh