zoukankan      html  css  js  c++  java
  • POJ 1405 Heritage(贪心)

    Description

    Your rich uncle died recently, and the heritage needs to be divided among your relatives and the church (your uncle insisted in his will that the church must get something). There are N relatives (N <= 18) that were mentioned in the will. They are sorted in descending order according to their importance (the first one is the most important). Since you are the computer scientist in the family, your relatives asked you to help them. They need help, because there are some blanks in the will left to be filled. Here is how the will looks: 

    Relative #1 will get 1 / ... of the whole heritage, 
    Relative #2 will get 1 / ... of the whole heritage, 
    ---------------------- ... 
    Relative #n will get 1 / ... of the whole heritage. 

    The logical desire of the relatives is to fill the blanks in such way that the uncle's will is preserved (i.e the fractions are non-ascending and the church gets something) and the amount of heritage left for the church is minimized.

    Input

    The only line of input contains the single integer N (1 <= N <= 18).

    Output

    Output the numbers that the blanks need to be filled (on separate lines), so that the heritage left for the church is minimized.

    Sample Input

    2

    Sample Output

    2
    3

     

    分析:

    这道题贪心就好,每个人尽量往大取就好。那么如何保证每次能取到最大呢?

    可以注意到,不算church分的,N=i 和N=i-1 时,前面i-1的人分的大小是一样的。比如当N=2时,分别是1/2,1/3;而当N=3时可算出分别是1/2,1/3,1/7。也就是如果用P[i]表示第i个人分多少,那么当N=i时,P[1]~P[i-1]是不需要再算的(前面算出来后存起来就行了)。我们的任务就是算第N个最大可以取多少。

    那么如果算第N个呢?就是在剩下的部分里找个比它小的最大的分数。而且可以知道,当N=i时,抛去前面i-1后剩下的部分就是当N=i-1时church分到的那部分。比如还是上面的N=3,前面2个分好后剩下的是1/6,正好是N=2是church的部分。那么我们就算比它小的一个最大的分数,即让上次剩下的数分母加1。1/7就是这么来的。

    接下来还有个问题是church分到的部分(剩下的部分)怎么能快速算出来保存起来。自己用几个例子算一下或者用数学推一下就发现,当N=i时,这次剩下的部分的分母=上次剩下的部分的分母*第i个人分到的部分的分母

    总结一下就是:设p[i]为每个人分到的部分的分母,设last[i]为当N=i时剩下的部分的分母, 则对于i=2~N有:

    p[i]=last[i-1]+1;

    last[i]=p[i]*last[i-1];(p[i]=2 , last[1]=2)

    最后的问题就是整个过程需要高精度运算。

     

    主代码:

     

    int main()
    
    {
    
        int N;
    
        cin>>N;
    
        last[1]="2";
    
        p[1]="2";
    
        string c="1";
    
        for (int i=2;i<=N;i++)
    
        {
    
            p[i]=ADD_INT(last[i-1],c);
    
            last[i]=MULTIPLY_INT(p[i],last[i-1]);
    
        }
    
        for (int i=1;i<=N;i++)
    
            cout<<p[i]<<endl;
    
        return 0;
    
    }

     

    举杯独醉,饮罢飞雪,茫然又一年岁。 ------AbandonZHANG
  • 相关阅读:
    一个简单例子:贫血模型or领域模型
    eclipse从数据库逆向生成Hibernate实体类
    Hibernate unsaved-value 属性
    webservice和restful的区别
    Web Service 的工作原理
    Hibernate3的DetachedCriteria支持
    hibernate criteria中Restrictions的用法
    Google Gson 使用简介
    struts2 访问国际化资源 <s:text>作为属性
    EL表达式从request和session中取值
  • 原文地址:https://www.cnblogs.com/AbandonZHANG/p/2598406.html
Copyright © 2011-2022 走看看