ACM枚举法知识点和练习

1.枚举法思想简介

基本思想:

  1. 枚举也称作穷举,指的是从问题所有可能的解的集合中一一枚举各元素。
  2. 用题目中给定的检验条件判定哪些是无用的,哪些是有用的。能使命题成立。即为其解。

枚举法优缺点:

  优点:算法简单,在局部地方使用枚举法,效果会十分的好

  缺点:运算量过大,当问题的规模变大的时候,循环的阶数越大,执行速度越慢。计算量容易过大

枚举法初体验:

  例子1:

问题: 百钱买百鸡问题:有一个人有一百块钱,打算买一百只鸡。到市场一看,公鸡一只3元,母鸡一只5元,小鸡3只1元,试求用100元买100只鸡,各为多少才合适?


  策略:

  根据题意我们可以得到方程组

  3X + 5Y + Z/3 = 100;
  
X + Y + Z = 100;
  (100 > X,Y,Z > 0, Z%3 == 0),根据这两个公式,我们可以写出最为简单的代码,一一列举所有解进行枚举

  代码:

 

int x,y,z;
    for( x = 0; x < 100; x++ )
        for( y = 0; y < 100 ; y++ )
            for( z = 0; z < 100; )
            {
                if( x + y + z == 100 && 3 * x + 5 * y + z / 3 == 100 )
                {
                     cout << x << " " << y << " " << z << endl;
                }
                z += 3;
            }

   然而我们可以根据已知条件来进行优化代码,减少枚举的次数:

   三种鸡的和是固定的,我们只要枚举二种鸡(x,y),第三种鸡就可以根据约束条件求得(z = 100 - x - y),这样就缩小了枚举范围。

  另外我们根据方程特点,可以消去一个未知数,得到下面

  4X + 7Y = 100;
  X + Y + Z = 100;
  (X,Y,Z > 0, Z%3 == 0),=>>    0 <= x < = 25因此代码可以优化为下面这样子:

  

for( x = 0; x <= 25; x++ )
{
            y = 100 - 4 * x;
            if( y % 7 == 0 && y >= 0 )
            {
                y /= 7;
                z = 100 - x - y;
                if( z % 3 == 0 && 3 * x + 5 * y + z / 3 == 100  )
                   cout << x << " " << y << " " << z << endl;
            }
}

 

  采用枚举的方法进行问题求解,需要注意3个问题:

  1. 简历简单数学模型,数学模型中变量数量尽量少,它们之间相互独立。这样问题解的搜索空间的维度就小,反应到程序代码中,循环嵌套的层次就会少。我们上面从3维优化到一维。
  2. 减少搜索的空间。利用已有知识,缩小数学模型中各个变量的取值范围,避免不必要的计算。反应到程序代码中,循环体被执行的次数少
  3. 采用合适的搜索顺序。对搜索空间的遍历顺序要与数学模型中的条件表达式一致。

   例子2:生理周期问题 来源POJ 1006

  

Description

Some people believe that there are three cycles in a person's life that start the day he or she is born. These three cycles are the physical, emotional, and intellectual cycles, and they have periods of lengths 23, 28, and 33 days, respectively. There is one peak in each period of a cycle. At the peak of a cycle, a person performs at his or her best in the corresponding field (physical, emotional or mental). For example, if it is the mental curve, thought processes will be sharper and concentration will be easier. 
Since the three cycles have different periods, the peaks of the three cycles generally occur at different times. We would like to determine when a triple peak occurs (the peaks of all three cycles occur in the same day) for any person. For each cycle, you will be given the number of days from the beginning of the current year at which one of its peaks (not necessarily the first) occurs. You will also be given a date expressed as the number of days from the beginning of the current year. You task is to determine the number of days from the given date to the next triple peak. The given date is not counted. For example, if the given date is 10 and the next triple peak occurs on day 12, the answer is 2, not 3. If a triple peak occurs on the given date, you should give the number of days to the next occurrence of a triple peak. 
Input

You will be given a number of cases. The input for each case consists of one line of four integers p, e, i, and d. The values p, e, and i are the number of days from the beginning of the current year at which the physical, emotional, and intellectual cycles peak, respectively. The value d is the given date and may be smaller than any of p, e, or i. All values are non-negative and at most 365, and you may assume that a triple peak will occur within 21252 days of the given date. The end of input is indicated by a line in which p = e = i = d = -1.
Output

For each test case, print the case number followed by a message indicating the number of days to the next triple peak, in the form: 

Case 1: the next triple peak occurs in 1234 days. 

Use the plural form ``days'' even if the answer is 1.
Sample Input

0 0 0 0
0 0 0 100
5 20 34 325
4 5 6 7
283 102 23 320
203 301 203 40
-1 -1 -1 -1
Sample Output

Case 1: the next triple peak occurs in 21252 days.
Case 2: the next triple peak occurs in 21152 days.
Case 3: the next triple peak occurs in 19575 days.
Case 4: the next triple peak occurs in 16994 days.
Case 5: the next triple peak occurs in 8910 days.
Case 6: the next triple peak occurs in 10789 days.

例题2生理周期问题

  解题思路:

  假设从第一天开始数,第x天三个高峰全部出现,则推出  d<= x <= 21252,且满足

( x - p ) % 23 == 0 && (x - e) % 28 ==0 && (x - i) % 33 ==0


  搜索空间为[d + 1,21252],我们能想到的最简单方法(开销最大的就是一个一个的在这个空间搜索),代码如下:

#include <iostream>

using namespace std;

int main()
{
    int p, e ,i, d;
    int x;
    int num = 1;
    while ( cin >> p >> e >> i >> d)
    {
        if( p != -1 && e != -1 && i != -1 && d != -1)
        {
            for( x = d + 1; x <= 21252; x++ )
            {

                    if(( x - p ) % 23 == 0 && (x - e) % 28 ==0 && (x - i) % 33 ==0)
                    {
                        cout <<"Case " << num << ": the next triple peak occurs in " << x - d <<" days." << endl;
                        break;
                    }
            }
            num++;
        }

        else
            break;


    }

    return 0;
}

Code


  但是这样做是没有必要的,我们可以进行代码优化,在问题的数学模型中,有多个条件可以满足时(我们要满足3个条件),可以采用逐步减小搜索空间的方法提高计算的效率,依次按照条件一,条件二,。。进行搜索。在最初的搜索空间上,首先按照条件一就行判定,然后将符合条件一的搜索空间,作为下面条件的索索空间。代码可以优化成:

  

#include <iostream>

using namespace std;

int main()
{
    int p, e ,i, d;
    int x;
    int num = 1;
    while ( cin >> p >> e >> i >> d )
    {
        if( p == -1 && e == -1 && i == -1 && d == -1)
        {
            break;
        }
        else
        {


            for( x = d + 1; x <= 21252; x++ )
            {
                    if(( x - p ) % 23 == 0 )
                        break;
            }
            for( ; x <= 21252; )
            {
                if( ( x - e ) % 28 == 0)
                    break;
                else
                    x += 23;//这一步是上文中说到的满足条件1的搜索空间,
            }
            for( ; x <= 21252; )
            {
                if( ( x - i ) % 33 == 0)
                {
                    break;
                }

                else
                    x += 23 * 28;//这一步是上文中说到的满足条件1和条件2的搜索空间,
            }
            cout <<"Case " << num << ": the next triple peak occurs in " << x - d <<" days." << endl;

            num++;
        }


    }

    return 0;
}

刷题:

  枚举例题:

  POJ:3050    1166    2028    1013    1744    1007    1046    1064    1079    1054    1117    1118    1123    1178    1313    1411    1468    1681     2363    2443    2381    2453    3080poj有难度,做题需谨慎。。)

 1.POJ_1753

    

 

 

 

 

 

参考文献:Programming_guide_and_online_pratice  (北京大学)

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值