题目链接:http://codeforces.com/problemset/problem/676/B

题意:一个n层的平面酒杯金字塔,如图,每个杯子的容量相同。现在往最顶部的一个杯子倒 t 杯酒,求流动结束后有多少个杯子被装满。

思路:每个局部的两代三个杯子的流动过程是一致的,因此可以用递归来模拟求解。

具体为:设add(cur, i, l)执行“往第 i 个杯子倒cur量的酒”,附加信息: i 位于第 l 层。若执行完剩余了surplus量的酒,则往 i 的下一层左右两侧的杯子各倒surplus/2量的酒;若无剩余,则抵达递归基。

关于 i 与它下一层的对应关系:我对所有杯子从1开始逐层从左到右编号,因此 i 的左下为i+l, 右下为i+l+1。

关于surplus的值:按照CF的题解的做法,如果有n层,则把每个杯子的容量记为volume = 2 ^ n份,这样能保证即使到第n层每次的cur也都整数。

维护数组v, 其中v[i] 记录当前第 i 个杯子中已有的酒量,若有剩余,则surplus = cur - (volume - v[i])。

最后统计下所有n*(n+1)/2个杯子中v[i]==volume的个数即可。

 #include <cstdio>
#include <cmath>
using namespace std;
const int MAX_N = ; int t, n;
int num;
int v[MAX_N*(MAX_N+)/ + ];
int volume;
int cnt; void add(int cur, int i, int l){
if(l > n) return ;
if(cur + v[i] <= volume){//最多加满当前的,不会溢出
v[i] += cur;
return ;
}
int surplus = cur - (volume - v[i]);//有溢出
v[i] = volume;
add(surplus/, i+l, l+);
add(surplus/, i+l+, l+);
} int main()
{
scanf("%d%d", &n, &t);
num = n*(n+)/;
volume = pow(, n);
int cur = volume * t;
cnt = ;
add(cur, , );
for(int i=; i<=num; i++){
//printf("%d %d\n", i, v[i]);
if(v[i] == volume) cnt++;
}
printf("%d\n", cnt);
return ;
}

【CF 676B Pyramid of Glasses】模拟,递归的更多相关文章

  1. CF 676B Pyramid of Glasses[模拟]

    B. Pyramid of Glasses time limit per test 1 second memory limit per test 256 megabytes input standar ...

  2. codeforces 676B B. Pyramid of Glasses(模拟)

    题目链接: B. Pyramid of Glasses time limit per test 1 second memory limit per test 256 megabytes input s ...

  3. Codeforces Round #354 (Div. 2) B. Pyramid of Glasses 模拟

    B. Pyramid of Glasses 题目连接: http://www.codeforces.com/contest/676/problem/B Description Mary has jus ...

  4. B. Pyramid of Glasses

    原题链接 B. Pyramid of Glasses Mary has just graduated from one well-known University and is now attendi ...

  5. Pyramid of Glasses(递推)

    Pyramid of Glasses time limit per test 1 second memory limit per test 256 megabytes input standard i ...

  6. python--递归(附利用栈和队列模拟递归)

    博客地址:http://www.cnblogs.com/yudanqu/ 一.递归 递归调用:一个函数,调用的自身,称为递归调用 递归函数:一个可以调用自身的函数称为递归函数 凡是循环能干的事,递归都 ...

  7. 深度优先搜索入门:POJ1164城堡问题(递归、用栈模拟递归)

    将问题的各状态之间的转移关系描述为一个图,则深度优先搜索遍历整个图的框架为:Dfs(v) {if( v 访问过)return;将v标记为访问过;对和v相邻的每个点u: Dfs(u);}int main ...

  8. 【ACwing 93】【模版】非递归实现组合型枚举——模拟递归

    (题面来自ACwing) 从 1~n 这 n 个整数中随机选出 m 个,输出所有可能的选择方案. 输入格式 两个整数 n,m ,在同一行用空格隔开. 输出格式 按照从小到大的顺序输出所有方案,每行1个 ...

  9. 【模拟+递归+位运算】POJ1753-Flip Game

    由于数据规模不大,利用爆搜即可.第一次用位运算写的,但是转念一想应该用递归更加快,因为位运算没有剪枝啊(qДq ) [思路] 位运算:时间效率较低(172MS),有些辜负了位运算的初衷.首先将二维数组 ...

随机推荐

  1. Android 之 ExpandableListView 的使用

    喜欢显示好友QQ那样的列表,可以展开,可以收起,在android中,以往用的比较多的是listview,虽然可以实现列表的展示,但在某些情况下,我们还是希望用到可以分组并实现收缩的列表,那就要用到an ...

  2. Linux命令之nano -

    我使用过的Linux命令之nano - 比vi简单易用的文本编辑器 本文链接:http://codingstandards.iteye.com/blog/802593   (转载请注明出处) 用途说明 ...

  3. bad interpreter: No such file or directory

    经常会遇到这种情况,在windows下写的脚本,代码会在linux下无法执行,错误就是: bad interpreter: No such file or directory 1.原因 这通常都是由于 ...

  4. HTTP协议之 简易浏览器(3)--转载

    简单的说,今天的全部工作就是 我的目的只有两个 1.加深对http协议的理解   2.深化对B/S结构的认识. 代码 1 /* 2 这个程序把主机地址写死了, 3 想更像的话,可以在加个输入.然后根据 ...

  5. Javascript - IE8下parseInt()方法的取值异常

      公司的测试小妹妹跑来对我说,下拉框第9项始终无法正确提交的时候,我还以为见鬼了.   parseInt()会把'0'开头的数字以8进制来解析,当有大于7的数字时候就按10进制来解析.   // p ...

  6. C# 实现预览dwg文件完整源代码(无需autocad环境)

    using System; using System.Drawing; using System.Collections; using System.ComponentModel; using Sys ...

  7. oracle 经典语句集合

    1.一列转多行 方法一: select a.id,  substr(','||a.name||',',instr(','||a.name,',',1,b.rn)+1,    instr(a.name| ...

  8. 二维计算几何基础题目泛做(SYX第一轮)

    题目1: POJ 2318 TOYS 题目大意: 给一个有n个挡板的盒子,从左到右空格编号为0...n.有好多玩具,问每个玩具在哪个空格里面. 算法讨论: 直接叉积判断就可以.注意在盒子的边界上面也算 ...

  9. uva 1597 Searching the Web

    The word "search engine" may not be strange to you. Generally speaking, a search engine se ...

  10. oracle之Flash Recovery Area全面介绍 (转载)

    1. 设置闪回恢复区闪回恢复区主要通过3个初始化参数来设置和管理 db_recovery_file_dest:指定闪回恢复区的位置db_recovery_file_dest_size:指定闪回恢复区的 ...