"Hey Wei said" algorithm design and analysis - greedy algorithm thought Summary (HDU 2088 Box of Bricks)

Citation directory:

First, the greedy algorithm The basic idea and personal understanding

Second, select the greedy nature of vehicle refueling problem

Third, a greedy algorithm greedy thoughts problem coaching sublimation

Four, pair programming situation

 

 

First, the greedy algorithm and a personal understanding of the basic idea :

  1.1 Basic concepts:

  First we carefully read what the basic concept of greed from textbooks, as the name implies, the greedy algorithm always make the best choice in the current view. That greedy algorithm does not consider the best overall, just select it made some sense in the local optimum choice. Of course, we hope the final result of the greedy algorithm is the best overall. While the greedy algorithm can not have been the best overall solution to all problems, but many of the problems it generates overall optimal solution. The single-source shortest path problem, minimum spanning tree problems. In some cases, even if the greedy algorithm can not obtain an optimal overall solution, the end result is a good approximation of the optimal solution.

  So for the greedy algorithm, the algorithm is no fixed template such routine, when the local optimum and global optimum solution proved to be consistent, you can use the greedy algorithm.

 

  1.2 Conditions of use:

  (1) needs to have selected properties greedy

  The so-called greedy nature is meant that the choice of the overall problem seeking the optimal solution may be the best choice through a series of partially, i.e., to achieve the greedy choice. This is a greedy algorithm feasible first essential element, the main difference is the greedy algorithm and dynamic programming algorithm.

  Solutions typically dynamic programming algorithm to a bottom-up manner each sub-question, the greedy algorithm is usually a top-down manner, in an iterative manner to select successive greedy, greedy choice will each make a demand to simplify the problem by It is the size of smaller sub-problems.

  For a specific issue, to determine whether it has a selective nature greedy, greedy choice must prove every step made eventually lead to an overall optimal solution.

  (2) needs to have a sub-optimal structural properties

  When the optimal solution contains optimal solution to a problem of his son's problems, saying that this issue has structural sub-optimal. Optimal substructure nature of the problem is that the problem can be a key feature of a dynamic programming algorithm or the greedy algorithm.

  1.3 Use idea:

  Greedy algorithm is starting from a certain initial value problem and keep close to the given specific target, as fast as possible to achieve better solutions. Upon reaching a certain step in the algorithm can not continue to move forward, the algorithm stops. But the greedy algorithm can not guarantee that the final solution is the best and can not solve the largest or smallest solution of the problem, only to meet the range of feasible solutions to solve certain constraints.

 

  Classic kind of problem 1.4 greedy algorithm:

(1) knapsack problem

Question (2) the timing of activities

(3) line coverage issues

(4) a combination of digital problems

(5) the largest integer problems

(6) sharing card issue

(7) looking for change problem

 

 

Second, the greedy nature of vehicle refueling selection problem :

  PTA7-1 vehicle refueling problem: The last journey of the largest gas station when looking for car full of fuel can travel, and reset the current variable fuel after refueling, and increase the frequency with a variable ans mark, and move on with this method , special attention is the need to check whether the time of maximum support from every walk more than a short car full of fuel.

 

 

Third, a greedy algorithm problem coaching sublimation greedy thoughts :

  Topic 2.1 Source:

    HDU:http://acm.hdu.edu.cn/showproblem.php?pid=2088

 

  2.2 casual working title:

  Box of Bricks

·Problem Description:

  Little Bob likes playing with his box of bricks. He puts the bricks one upon another and builds stacks of different height. “Look, I've built a wall!”, he tells his older sister Alice. “Nah, you should make all stacks the same height. Then you would have a real wall.”, she retorts. After a little consideration, Bob sees that she is right. So he sets out to rearrange the bricks, one by one, such that all stacks are the same height afterwards. But since Bob is lazy he wants to do this with the minimum number of bricks moved. Can you help?

 

 

 

·Input

  The input consists of several data sets. Each set begins with a line containing the number n of stacks Bob has built. The next line contains n numbers, the heights hi of the n stacks. You may assume 1≤n≤50 and 1≤hi≤100.
  The total number of bricks will be divisible by the number of stacks. Thus, it is always possible to rearrange the bricks such that all stacks have the same height.
  The input is terminated by a set starting with n = 0. This set should not be processed.
 
·Output
  For each set, print the minimum number of bricks that have to be moved in order to make all the stacks the same height.
  Output a blank line between each set.
 
·Sample Input
  6
  5 2 4 1 7 5
  0
 
·Sample Output
  5

 

   2.3  题目大意:

    题目意思是:给定一堆积木,并且分成k列,试问如何用最少的移动操作(只能移动到相邻位置),能够将k列堆积木达到平均高度。

 

  2.4  题目思路:

    本题的贪心性质选择为:计算出总共的砖块数,在求出N堆砖的平均高度(即需要达到的最终高度),把高的堆上的砖移到不足平均高度的堆上。应该计算出所有不足高度的堆上总共差多少砖达到高度,即为结果。

 

  2.5  题目AC代码:

#include<stdio.h>
#include<string.h>

int main()
{
    int N;
    int a[55];
    int kase = 1;
    while(scanf("%d",&N) && N)
    {
        int sum = 0;
        for(int i = 0; i < N; i++)
        {
            scanf("%d",&a[i]);
            sum += a[i];
        }
        int ans = 0;
        for(int i = 0; i < N; i++)
        {
            if(a[i] < sum/N)
                ans += sum/N - a[i];
        }
        printf("Set #%d\n",kase++);
        printf("The minimum number of moves is %d.\n\n",ans);
    }

    return 0;
}

 

四、 结对编程情况:

  经过前几次实践题合作之后,结对编程逐渐顺利,能够让双方都能不断进行思路碰撞,实现较为合适的算法,当和三木小哥哥想到一起完成了算法设计、完成代码书写、成功AC一题之后,会有较为愉悦的心情感,较为顺利的完成三道题,继续加油,暂无较大问题。

 

 

如有不合理的地方,请及时指正,我愿听取改正~

参考链接:https://oi-wiki.org/

Guess you like

Origin www.cnblogs.com/WinniyGD/p/11889634.html