Codeforces--676B--Pyramid of Glasses

题目描述:
Mary has just graduated from one well-known University and is now attending celebration party. Students like to dream of a beautiful life, so they used champagne glasses to construct a small pyramid. The height of the pyramid is n. The top level consists of only 1 glass, that stands on 2 glasses on the second level (counting from the top), then 3 glasses on the third level and so on.The bottom level consists of n glasses.

Vlad has seen in the movies many times how the champagne beautifully flows from top levels to bottom ones, filling all the glasses simultaneously. So he took a bottle and started to pour it in the glass located at the top of the pyramid.

Each second, Vlad pours to the top glass the amount of champagne equal to the size of exactly one glass. If the glass is already full, but there is some champagne flowing in it, then it pours over the edge of the glass and is equally distributed over two glasses standing under. If the overflowed glass is at the bottom level, then the champagne pours on the table. For the purpose of this problem we consider that champagne is distributed among pyramid glasses immediately. Vlad is interested in the number of completely full glasses if he stops pouring champagne in t seconds.

Pictures below illustrate the pyramid consisting of three levels.
输入描述:
The only line of the input contains two integers n and t (1 ≤ n ≤ 10, 0 ≤ t ≤ 10 000) — the height of the pyramid and the number of seconds Vlad will be pouring champagne from the bottle.
输出描述:
Print the single integer — the number of completely full glasses after t seconds.
输入:
3 5
4 8
输出:
4
6
题意:
已知每秒钟他向最高层的酒杯倾倒的酒量刚好等于能倒满一个酒杯,当一个酒杯注满以后,酒就会顺着杯子流下来,并均匀地分向下一层的两个酒杯。如果最底层的酒杯注满了,酒会顺着杯子流到桌子上。为了方便起见,这里不考虑酒在流的过程中停留在杯沿的部分和挥发的部分,并假设酒的流速是足够快的。现在弗瑞达想知道,如果他在t秒后停止倒酒,金字塔中会有多少个已经被注满的酒杯?
题解
递推,模拟整个倒酒的过程dp[i][j]表示第i层第j个杯子
代码:

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
using namespace std;

const int maxn = 1000 + 5;
double dp[maxn][maxn];

int main(){
    int n,t;
    while(scanf("%d%d",&n,&t)!=EOF){
        memset(dp,0,sizeof(dp));
        dp[1][1] = (1.0) * t;
        int ans = 0;
        for(int i = 1; i <= n; i ++){
            for(int j = 1; j <= i; j ++){
                if(dp[i][j] >= 1.0){
                    ans ++;
                    dp[i + 1][j] += (dp[i][j] - 1) * 0.5;
                    dp[i + 1][j + 1] += (dp[i][j] - 1) * 0.5;
                }
            }
        }
        cout<<ans<<endl;
    }
    return 0;
}

发布了228 篇原创文章 · 获赞 1 · 访问量 13万+

猜你喜欢

转载自blog.csdn.net/Ypopstar/article/details/105169296
今日推荐