zoukankan      html  css  js  c++  java
  • 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 }
  • 相关阅读:
    四川省选2012 day1 喵星球上的点名 (后缀数组,并不是完全的正解)
    6.2.1 最短路
    5.3.3 敌兵布阵
    6.1.1 Constructing Roads
    6.2.4 Arbitrage
    6.1.6 Jungle Roads
    5.3.6 Cow Sorting (HDU 及 POJ)
    6.2.5 Trucking
    6.1.4 还是畅通工程
    6.1.3 畅通工程
  • 原文地址:https://www.cnblogs.com/neverchanje/p/3464184.html
Copyright © 2011-2022 走看看