Move Cards HUST

 

Description

The Problem

There are N piles of cards, and these piles are numbered as 1,2,...,N respectively. Each pile has some cards, while the total of all cards must be a multiple of N. The player can take some cards from one pile and then put them on another pile as one step.

The follow is the rules: the cards on the pile numbered 1 can only be moved to the pile 2; the cards on the pile N can only be moved to the pile N-1; the cards on other piles, numbered from 2 to N-1, can be moved to either their left pile or right one.

Now the player should move these cards to make the card numbers of each pile equal by minimum steps.

For example: there are 4 piles, and the card numbers of each pile are 9 8 17 6 respectively. So it needs 3 steps to achieve the goal at least.

The Input

The input file consists of one or more test cases. The first line of input is the number of cases. Each case contains 2 lines: the first line of each case contains the only number N indicating the number of piles, and the second line contains a group of integers separated by space, indicating the card numbers of each pile respectively.

The Output

The minimum steps to make all piles' card numbers equal.

 

Sample input

1
4
9 8 17 6

Sample output

3

水题一道
附上题目链接http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=45038
 1 #include<iostream>
 2 #include<vector>
 3 using namespace std;
 4 int main (){
 5     int cases;
 6     cin>>cases;
 7     int add , count;
 8     int average , N;
 9     while ( cases-- )
10     {
11         average=count=add=0;
12         cin>>N;
13         vector<int>pile (N);
14         for ( int i=0; i<N; i++ ) 
15         {
16             cin>>pile[i];
17             average += pile[i];
18         }
19         average/=N;
20         for ( int j=0; j<N-1; j++ )
21         {    
22             add = pile[j] - average;
23             pile[j+1] += add;
24             if ( add != 0 )
25             {
26                 count++;
27                 /*pile[j] = average;*/
28                 add = 0;
29             }    
30         }
31         cout<<count<<endl;
32     }
33 }

 

 

转载于:https://www.cnblogs.com/neverchanje/p/3464184.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值