"Greedy algorithm" evenly split Solitaire

Source: Internet
Author: User

Topic:

There were n piles of cards, numbered 1,2,...,n. There are several sheets on each heap, but the total number of cards must be multiples of n. You can take several cards on either heap and move them. The rules for the move are: the cards taken on the number 1 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 other stacks will 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: n=4,4 heap cards are: ①9②8③17④6 move three times can achieve the purpose: from the ③ take 4 cards to ④ and then from the ③ area 3 to ② and then from ② 1 to ①.

Input:

4

9 8 17 6

Output:

3

Ideas:

Let you divide each pile of cards into the total number of cards/n cards, requiring a minimum number of moves.

First, think of a local solution , for example, starting from the left pile, that is, the current state, then the optimal solution of the current state is to get enough cards from the second pile, or put many cards in the second heap, and then move the second pile, the third pile ...

Then, to detect whether the local strategy can be a global greedy strategy , it is clear that even in real life, but also these steps, but changed the order, you may move first, the most cards, more cards, cards less, in fact, as long as the cards are not enough average, then at least one step, To make up the number of draws, so the strategy is to satisfy the greedy strategy.

Of course there will be a special case, but also correct, such as n=3,①1②7③22, at this time each pile to 10 cards, when from the second pile to the first heap to take 9 cards, the second pile into 2, still according to the strategy to do, then the second pile from the third stack 12 cards, now is 10 cards, a total of two steps, The actual situation is two-step, but the order is different.

The code is as follows:

#include <iostream>using namespacestd;intMain () {intdui[ -]; intN; intsum=0, p,step=0; CIN>>N;  for(intI=0; i<n;i++) {cin>>Dui[i]; Sum+=Dui[i]; } P=sum/N;  for(intI=0; i<n;i++){        if(dui[i]!=p) {Dui[i+1]+=dui[i]-p; Dui[i]=p; Step++; }} cout<<step; return 0;}

"Greedy algorithm" evenly 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.