Topic links: https://www.luogu.com.cn/problem/P1031
Title Description
There N stack of cards, are numbered 1,2, ..., N . There are a number of sheets per stack, but the total number of cards will be for the N factor. A plurality of cards can take on either a pile and then moved.
Card moving rules: the number of a pile of cards taken, only moved numbered heap 2; numbered heap take N card can only move number N - 1 of the heap;
Take another heap of cards, can be moved left or right adjacent to the heap.
Now asked to find a mobile method with the least number of moves are the number of cards per heap as much.
E.g. N = . 4, . 4 stack number cards are:
①9 ②8 ③17 ④6
Mobile 3 can achieve:
Taken from ③ . 4 cards into ④ ( 9,8,13,10 );
Taken from ③ . 3 cards into ② ( 9,11,10,10 );
Take from ② 1 card into ① ( 10,10,10,10 );
Input Format
Two lines
First line: N ( N card stack, . 1 ≤ N ≤ . 1 0 0)
Second line: A . 1 , A 2 , ... , A n- ( N stacks of cards, each card stack the initial number, . 1 ≤ A I ≤ . 1 0 0 0 0)
Output Format
Line: that is, all heap meet the minimum number of moves equal.
Sample input and output
Input # 1
. 4 . 9. 8. 17. 6
output # 1
3
. 1 #include <bits / STDC ++ H.> 2 the using namespace STD; . 3 const int MAXN = 10001 ; . 4 int A [MAXN]; . 5 int main () { . 6 int n-, SUM = 0 , Ave = 0 ; . 7 CIN> > n-; . 8 for ( int I = 0 ; I <n-; I ++ ) { . 9 CIN >> a [I]; // read the data when calculating the average of 10 Ave + = a [I]; . 11 } 12 is Ave / = n-; 13 is for ( int I = 0 ; I <n-; I ++ ) { 14 A [I] - = Ave; // with each subtracting the average, the final result is calculated with the difference between the number 15 } 16 for ( int I = 0 ; I <n-; I ++ ) { . 17 IF (! A [I] = 0 ) { 18 is A [I + . 1 ] = A [I] + A [I + . 1 ]; // Look front most solution, but assuming rightward added, then after moving a [i + 1] is a front plus the original number . 19 a [i] == 0 ; // if the post-movement position i is 0 above purpose 20 sum++; 21 }else{ 22 continue; 23 } 24 } 25 cout<<sum<<endl; 26 return 0; 27 }