题目链接: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. php5.3 appache phpstudy win7win8win10下 运行速度慢

    php5.3 appache phpstudy win7win8win10下 运行速度慢 最近在部署服务器以及本地测试的时候发现了一个奇怪的现象,运行PHP程序的时候非常慢,起先以为是网速的原因,后经 ...

  2. ubuntu14.04 安装 StudioZend12

    到官网下载:http://www.zend.com/en/products/studio/downloadsLinux-64位:http://downloads.zend.com/studio-ecl ...

  3. [Git] set-upstream

    When you want to push your local branch to remote branch, for the first push: git push --set-upstrea ...

  4. Git 提供篇

    1. Git自动补全 假使你使用命令行工具运行Git命令,那么每次手动输入各种命令是一件很令人厌烦的事情.为了解决这个问题,你可以启用Git的自动补全功能,完成这项工作仅需要几分钟. 为了得到这个脚本 ...

  5. [HeadFirst-HTMLCSS学习笔记][第六章严格的HTML]

    远古 古老的html 4.01和XHTML 1.1 页面 必须用Doctype挑明,再html元素上面 html 4.01 <!DOCTYPE html PUBLIC "-//W3C/ ...

  6. plsql连接oracal数据库

    一般只是作为客户端访问Oracle,并不需要安装庞大的Oracle 免安装客户端使用plsql连接oracal数据库 在本机没有安装oracle和oracle客户端的情况下,可以安装oracle提供的 ...

  7. CSS的优先级

    样式的优先级: (内联样式表[嵌入式样式])>(内部样式表)>(外部样式表) 经过测试动手测试发现有个(唯一的)例外 情况:当引用外部样式在内部样式表(非嵌入式样式)的后面时,外部样式会覆 ...

  8. 利用jquery来隐藏input type="file"

    <li> <input type="text" name="token" value = "<?php ech$_SESSIO ...

  9. POJ 1269 - Intersecting Lines 直线与直线相交

    题意:    判断直线间位置关系: 相交,平行,重合 include <iostream> #include <cstdio> using namespace std; str ...

  10. codeforces 340C Tourist Problem(公式题)

    转载请注明出处: http://www.cnblogs.com/fraud/          ——by fraud Tourist Problem Iahub is a big fan of tou ...