贪心算法

性质:

  1. 没有固定形式
  2. 选择当前情况的最优解,再求出总解。
  3. 只对有些问题局部策略能导致全局最优解)有效
  4. 关键是贪心策略的选择
    思路:
    贪心算法(中英对照版)总结:就是把问题描述成数学模型,再用while循环分解成多个子问题,利用可行方法求出子问题的最优解,最后通过最优解推断出最终解。
    例子:
    贪心算法(中英对照版)
    陶陶家的院子里有一棵苹果树,每到秋天树上就会结出N个(1≤N≤1000000)苹果。苹果成熟的时候,陶陶就会跑去摘苹果。陶陶有个30厘米高的板凳,当她不能直接用手摘到苹果的时候,就会踩到板凳上再试试。
    现在已知N个苹果到地面的高度,以及陶陶把手伸直的时候能够达到的最大高度,请帮陶陶算一下她能够摘到的苹果的数目。假设她碰到苹果,苹果就会掉下来。
    输入
    输入包括两行数据。第一行包含N+1个数,第一个为N,第2个到第N+1个为N个100到200之间(包括100和200)的整数(以厘米为单位)分别表示N个苹果到地面的高度,两个相邻的整数之间用一个空格隔开。第二行只包括一个100到120之间(包含100和120)的整数(以厘米为单位),表示陶陶把手伸直的时候能够达到的最大高度。
    输出
    输出包括一行,这一行只包含一个整数,表示陶陶能够摘到的苹果的数目。
    代码如下:
#include<iostream>
#include<cstdio>
using namespace std;
int main()
{
    int i,j,m,b,sum,n;
    scanf("%d",&n);
    sum=0;
    int a[1000]= {0};
    for(i=0; i<n; i++)
    {
        scanf("%d",&m);
        a[m]++;
    }
    scanf("%d",&b);
    for(i=100; i<=b+30; i++)
    {
        sum+=a[i];
    }
    printf("%d\n",sum);
    return 0;
}

谢谢观看,下次再见!

(English)

Greedy Algorithm

Nature:

  1. No fixed form

  2. Choose the best solution of the current situation, and then find the total solution.

  3. Only for some problems (local strategies can lead to global optimal solutions)

  4. The key is the choice of greedy strategy.

Train of thought:
贪心算法(中英对照版)
I drew it myself!
Summary: It describes the problem as a mathematical model, then decomposes it into several sub-problems by while cycle, finds the optimal solution of sub-problems by feasible method, and finally deduces the final solution by the optimal solution.

Example:
贪心算法(中英对照版)
There is an apple tree in Tao Tao’s yard. Every autumn, there are N apples (1 < N < 1000000). When the apple is ripe, Taotao will run to pick it. Taotao has a bench 30 centimeters high. When she can’t pick apples directly with her hands, she will step on the bench and try again.

Now we know the height of N apples to the ground and the maximum height Taotao can reach when she stretches out her hand. Please help Taotao calculate the number of apples she can pick. Suppose she touches an apple, it will fall down.

input

The input consists of two rows of data. The first line contains N + 1 numbers, the first is N, and the second to N + 1 are N integers (in centimeters) between 100 and 200, representing the height of N apples to the ground, separated by a space between two adjacent integers. The second line contains only an integer (in centimeters) between 100 and 120, indicating the maximum height that the pottery handle can reach when it is stretched out.

output

The output consists of a line, which contains only one integer, indicating the number of apples Taotao can pick.

The code is as follows:

#include<iostream>
#include<cstdio>
using namespace std;
int main()
{
    int i,j,m,b,sum,n;
    scanf("%d",&n);
    sum=0;
    int a[1000]= {0};
    for(i=0; i<n; i++)
    {
        scanf("%d",&m);
        a[m]++;
    }
    scanf("%d",&b);
    for(i=100; i<=b+30; i++)
    {
        sum+=a[i];
    }
    printf("%d\n",sum);
    return 0;
}

Thanks for watching,see you!

相关文章: