"Codevs" "greedy" 1098 split Solitaire

Source: Internet
Author: User

There are n stacks of cards, numbered separately, ..., N. There are several sheets on each heap, but the total number of cards must be multiples of N. You can take a card on any heap and move it.
The rules for the move are: the cards on the numbered 1 heap can only be moved to the heap numbered 2, and the cards taken on the heap numbered N can only be moved to the heap numbered N-1, and the cards on the other heap may be moved to the adjacent left or right heap.
Now it's time to find a way to move, with the fewest number of moves to make as many cards as you can on each heap.

For example, the n=4,4 heap of cards are:
①9②8③17④6
Move 3 times to achieve the goal:
Take 4 cards from ③ to ④ (9 8) and take 3 cards from ③ to ② (9), take 1 cards from ② and put them in ① (10 10 10 10).

Enter a description input Description

First row n (n heap Solitaire, 1 <= n <= 100)
Second line A1 A2 ... An (N heap solitaire, initial number of cards per pile, l<= Ai <=10000)

outputs description output Description

Output to the screen. The format is:
The minimum number of moves when all heaps are equal. ‘

sample input to sample

4
9 8 17 6

Sample output Sample outputs 3

  For this problem, I was just beginning to think that DP, and then no train of thought ... Have to open the tag, found to be a greedy!

How can this be greedy ... The way to look at the sample seems to be to move the cards as far as possible to the fewest piles of cards, but the code is wrong to write ...

Had to turn to the bacteria, but what to fix is that the code is very simple, the idea is very confusing ...

This problem from the 1~n traversal, if the current card less than the average, the extra card to move to i+1, if less, from i+1 take less of that part of the card

But here's a question, what if i+1 becomes a negative after taking a card from i+1? The solution is not to take care of it, to fill the vacancy with a future pile of cards .

It is magical, however, that such an operation is equivalent to an operation that i+1 does not become a negative number, We just changed the order of the cards and didn't change the number of cards we took. as the great God's explanation http://m.blog.csdn.net/blog/maverick1990/17262243

If the current card is not enough, then it is like the right hand heap borrowing, if borrowed all the cards on the right, still not reach the average, it is necessary to the right side to borrow, and so on ... However, in order to ensure the fewest number of moves, the neighboring cards can not be moved in the same direction two times , that is, from the i->i+1 up to one time, if moving two times is not optimal. Example: Card heap (1,1,10), obviously from 10 to the left to move cards, move two times, and from left to right to traverse, the number of moves is 3, that is (2,0,10)-(2,6,4)--(4,4,4), two moves from the 2nd heap moved to the 1th heap, so is not optimal. So how do we meet the best when we traverse from left to right? That is, you can allow the borrowing of negative numbers, such as the 1th place from the 2nd place to borrow 3, 2nd bit to 2, and then borrow 6 from the 3rd position, you can find the best steps.

The code is as follows:

  

#include <cstdio>#include<algorithm>#include<cstring>#include<iostream>using namespacestd;intn,a[ the],sum=0, ok=0, avg;voidRead () {cin>>N;  for(intI=1; i<=n;i++) {scanf ("%d",&A[i]); Sum+=A[i]; } avg=sum/N;}voidWork () { for(intI=1; i<=n;i++)    {        if(A[I]==AVG)Continue; if(a[i]>avg) {            intm=a[i]-avg; A[i+1]+=m; OK++; }        if(a[i]<avg) {            intm=avg-A[i]; A[i+1]-=m; OK++; }} printf ("%d", OK);}intMain () {read ();    Work (); return 0;}

"Codevs" "greedy" 1098 split Solitaire

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.