Codeforces Round #354 (Div. 2) B. Pyramid of Glasses (模拟+思维)

原题请戳这里

题意:

将杯子摆成杨辉三角状,即顶层1个杯子,第二层2个杯子,……第N层N个杯子。

每一秒能倒满1个杯子,每次一个杯子满了,酒会平分得流入它下面支撑它的两个杯子中。

如下图所示。1 ≤ n ≤ 10, 0 ≤ t ≤ 10 000。

 

 

分析:由于n很小,所以直接模拟此过程,其实也是一个递推的过程。

注意,如果递推的时候没有递推到n+1层,那么最后统计的时候是统计>=1的个数而不是==1的个数,

因为当酒能倒满的杯子大于n层杯子即n*(n+1)/2<t时,递推过程终止于n层,不能向下推了。故最下面

的一层酒是大于1的。代码如下:

 

#include<cstdio>
#include<map>
#include<algorithm>
#include<iostream>
#include<set>
#include<cmath>
#include<cstring>

using namespace std;

typedef long long ll;

double p[12][12];

int main()
{
    int n,t;
    while(~scanf("%d%d",&n,&t))
    {
        memset(p,0,sizeof(p));
        for(int i=1;i<=t;i++)
        {
            p[1][1]+=1;
            for(int j=1;j<n;j++)
            {
                for(int k=1;k<=j;k++)
                {
                    //cout<<j<<' '<<k<<endl;
                    if(p[j][k]>1)
                    {
                        p[j+1][k] += (p[j][k]-1)/2;
                        p[j+1][k+1] += (p[j][k]-1)/2;
                        p[j][k] = 1;
                        //cout<<i<<endl;
                        //cout<<j+1<<' '<<k<<' '<<p[j+1][k]<<endl;
                        //cout<<j+1<<' '<<k+1<<' '<<p[j+1][k+1]<<endl;
                    }
                }
            }
        }
        int cnt = 0;
        for(int j=1;j<=n;j++)
        {
            for(int k=1;k<=j;k++)
            {
                if(p[j][k]>=1)
                    cnt++;
            }
        }
        cout<<cnt<<endl;
    }
    return 0;
}
View Code

若递推到n+1层,则统计的是==1的个数。

#include<cstdio>
#include<map>
#include<algorithm>
#include<iostream>
#include<set>
#include<cmath>
#include<cstring>

using namespace std;

typedef long long ll;

double p[12][12];

int main()
{
    int n,t;
    while(~scanf("%d%d",&n,&t))
    {
        memset(p,0,sizeof(p));
        for(int i=1;i<=t;i++)
        {
            p[1][1]+=1;
            for(int j=1;j<=n;j++)  //<=n保证递推到n+1层
            {
                for(int k=1;k<=j;k++)
                {
                    if(p[j][k]>1)
                    {
                        p[j+1][k] += (p[j][k]-1)/2;
                        p[j+1][k+1] += (p[j][k]-1)/2;
                        p[j][k] = 1;
                    }
                }
            }
        }
        int cnt = 0;
        for(int j=1;j<=n;j++)
        {
            for(int k=1;k<=j;k++)
            {
                if(p[j][k]==1)  //
                    cnt++;
            }
        }
        cout<<cnt<<endl;
    }
    return 0;
}

 

posted @ 2016-05-29 22:55  fukan  阅读(203)  评论(0编辑  收藏  举报