• 贪心大法的几个问题


    最小生成树Prim算法Kruskal算法都是漂亮的贪心算法。

    贪心法的应用算法有Dijkstra的单源最短路径和Chvatal的贪心集合覆盖启发式
    贪心算法可以与随机化算法一起使用,具体的例子就不再多举了。
    很多的智能算法(也叫启发式算法),本质上就是贪心算法和随机化算法结合。
    这样的算法结果虽然也是局部最优解,但是比单纯的贪心算法更靠近了最优解。
     

    一.把3/7和13/23分别化为三个单位分数的和

    设a、b为互质正整数,a<b 分数a/b 可用以下的步骤分解成若干个单位分数之和:
    步骤一: 用b 除以a,得商数q1 及余数r1。(r1=b - a*q1)
    步骤二:把a/b 记作:a/b=1/(q1+1)+(a-r1)/b(q1+1)
    步骤三:重复步骤2,直到分解完毕
    3/7=1/3+2/21=1/3+1/11+1/231
    13/23=1/2+3/46=1/2+1/16+1/368
    斐波那契的求解埃及分数的贪心算法:
    设某个真分数的分子为a,分母为b;
    把b除以a的商部分加1后的值作为埃及分数的某一个分母c;
    将a乘以c再减去b,作为新的a;
    将b乘以c,得到新的b;
    如果a大于1且能整除b,则最后一个分母为b/a;算法结束;
    或者,如果a等于1,则,最后一个分母为b;算法结束;
    否则重复上面的步骤。
     
    二.POJ 3377:Best Cow Line, Gold 贪心+字典序
    题目:
     
    总时间限制:  10000ms 单个测试点时间限制:  1000ms 内存限制:  65536kB 描述FJ is about to take his N (1 <= N <= 30,000) cows to the annual "Farmer of the Year" competition. In this contest every farmer arranges his cows in a line and herds them past the judges.
    
    The contest organizers adopted a new registration scheme this year: simply register the initial letter of every cow in the order they will appear (e.g., If FJ takes Bessie, Sylvia, and Dora in that order, he just registers BSD). After the registration phase ends, every group is judged in increasing lexicographic order (i.e., alphabetical order) according to the string of the initials of the cows' names.
    
    FJ is very busy this year and has to hurry back to his farm, so he wants to be judged as early as possible. He decides to rearrange his cows, who have already lined up, before registering them.
    
    FJ marks a location for a new line of the competing cows. He then proceeds to marshal the cows from the old line to the new one by repeatedly sending either the first or last cow in the (remainder of the) original line to the end of the new line. When he's finished, FJ takes his cows for registration in this new order.
    
    Given the initial order of his cows, determine the least lexicographic string of initials he can make this way.输入* Line 1: A single integer: N
    
    * Lines 2..N+1: Line i+1 contains a single initial ('A'..'Z') of the cow in the ith position in the original line输出The least lexicographic string he can make. Every line (except perhaps the last one) contains the initials of 80 cows ('A'..'Z') in the newline.
    
    样例输入
    6
    A
    C
    D
    B
    C
    B
    样例输出ABCBCD
    View Code

    示例片段:

     1 int n;
     2 char s[maxl+1];
     3 void solve()
     4 {
     5     int a=0,b=n-1;
     6     while(a<=b)
     7     {
     8         bool left=false;
     9         for(int i=0;i+a<=b;i++)
    10         {
    11             if(s[a+i]<s[b_i])
    12             {
    13                 left=true;
    14                 break;
    15             }
    16             else if(s[a+i]>s[b_i])
    17             {
    18                 left=false;
    19                 break;
    20             }
    21         }
    22         if(left){putchar(s[a++];)}
    23         else putchar(s[b--];)
    24     }
    25     putchar('
    ');
    26 }
    View Code

    原序取反,正反比较每次取出小的字符输出即字典序排列,字典序比较常用得到贪心算法。

     
  • 相关阅读:
    设计模式之单例模式实践
    有关集合的foreach循环里的add/remove
    项目中常用的MySQL优化方法--壹拾玖条
    Solr
    Lucene补充
    Lucene
    一千行 MySQL 学习笔记
    Servlet
    CSS未知宽高元素水平垂直居中
    深拷贝和浅拷贝
  • 原文地址:https://www.cnblogs.com/dzzy/p/4711352.html
Copyright © 2020-2023  润新知