NOIP 2002 card sharing

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 card stack taken only move number 2 heap; numbered N heap take the card, can only be moved to the number N-1 on 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 heap card number are:

98176

Mobile 3 times object can be achieved:

Taken from ③ . 4 cards into ④ ( 9,8,13,10 ) -> taken from ③ . 3 cards into ② ( 9,11,10,10 ) -> taken from ② . 1 ① card into ( 10,10,10,10 ).

Input Format

Two lines

First line: N ( N stack of cards, . 1 N . 1 0 0)

Second line: A1 , A 2 , ... , A n-  ( N stacks of cards, each card stack the initial number, . 1 A I . 1 0 00 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 

Analysis: The

meaning is clear from the title, greedy strategy is to us from start to finish piles of shift (because we can only move to an adjacent pile, the order of movement is certainly not an impact), each movement playing cards, it will become a pile of number cards (ie average) we want ;
through the sample, we analyze roughly this strategy.
First open a p [] array, p [i] denotes the i-heap a few cards, dis [] array (dis is the abbreviation disparity, meaning that gap, the difference ....) dis [i] denotes the i stack of cards and the gap between the number of goals, ans record number.
(Mean = 10)
not before the mobile: P [. 1] =. 9, P [2] =. 8, P [. 3] =. 17, P [. 4] =. 6;
DIS [. 1] = -1, DIS [2] -2 =, DIS [. 3] =. 7, DIS [. 4] = -. 4;
ANS = 0
! next for loop, if dis [i] = 0, it will be p [i + 1] + = dis [i ], p [i] - = dis [i], dis [i + 1] + = dis [i], ans ++, otherwise skip
(if dis [i] <0, the right side is about a bunch of abs (dis [ stack number]) of playing cards move to the left a pile, it is entirely established meaning of the questions)
. 1,
P [. 1] = 10, P [2] =. 7, P [. 3] =. 17, P [. 4] =. 6;
said [1] = 0, say [2] = - 3, said [3] = 7, say [4] = - 4;
years = 1
      2, p [1] = 10, p [2] = 10, p [3] = 14, p [4] = 6; 
said [1] = 0, say [2] = 0, say [3] = 4, say [4] = - 4;
2 years =
      3, p [1] = 10, p [2] = 10, p [3] = 10, p [4] = 10; 
said [1] = 0, say [2] = 0, say [3] = 0, say [4] = 0;
3 years =
Here is the code
#include <iostream>
#include <cstdio>

using namespace std;
int main()
{
    int n,p[10001],dis[10001],arv=0,ans=0;
    scanf("%d",&n);
    for(int i=1;i<=n;i++) scanf("%d",&p[i]),arv+=p[i];
    arv/=n;
    for(int i=1;i<=n;i++) dis[i]=p[i]-arv; // fact, you can also DIS [] and P [] array were combined, only one array to represent the number of cards and gaps.
     for ( int I = . 1 ; I <= n-; I ++) { IF (DIS [ I] == 0 ) Continue ; DIS [I + . 1 ] + = DIS [I], ANS ++ ;} 
    the printf ( " % D " , ANS);
     return  0 ; 
}

 

 
  
 
 

Guess you like

Origin www.cnblogs.com/majorin/p/11919383.html