UVa 12627 Erratic Expansion - 分治
因为不好复制题目,就出给出链接吧:
Vjudge传送门[here]
UVa传送门[here]
请仔细看原题上的那幅图,你会发现,在时间t(t > 0),当前的气球构成的一幅图,它是由三个时间为(t - 1)的图再加上一块全是蓝色的一块构成。所以可以想到递归求解。对于上半部分的行求前一时刻对应几行的红球数乘2,下面的减去2t - 1然后递归前一幅图求解。
但是这样最坏的时间复杂度为O(2k-1),仍然会TLE,那么得另寻出路。如果求在时刻t整个一幅图的红球个数,那么可以直接算出来,个数为3k。
因此在递归的过程中特判一下是不是求整个图的红球个数,可以把时间复杂度将为?O(log2n)
Code
/**
* UVa
* Problem#12627
* Accepted
* Time:0ms
*/
#include<iostream>
#include<cstdio>
#include<cctype>
#include<ctime>
#include<cstring>
#include<cstdlib>
#include<fstream>
#include<sstream>
#include<algorithm>
#include<map>
#include<set>
#include<stack>
#include<queue>
#include<vector>
#include<stack>
#ifndef WIN32
#define AUTO "%lld"
#else
#define AUTO "%I64d"
#endif
using namespace std;
typedef bool boolean;
#define inf 0xfffffff
#define smin(a, b) a = min(a, b)
#define smax(a, b) a = max(a, b)
template<typename T>
inline void readInteger(T& u){
char x;
int aFlag = ;
while(!isdigit((x = getchar())) && x != '-');
if(x == '-'){
x = getchar();
aFlag = -;
}
for(u = x - ''; isdigit((x = getchar())); u = (u << ) + (u << ) + x - '');
ungetc(x, stdin);
u *= aFlag;
} int T;
int n, a, b; template<typename T>
T pow(T a, int pos) {
if(pos == ) return ;
if(pos == ) return a;
T temp = pow(a, pos / );
if(pos & ) return temp * temp * a;
return temp * temp;
} inline void init() {
readInteger(n);
readInteger(a);
readInteger(b);
} long long dfs(int dep, int top, int bottom) {
if(dep == ) return ;
if(top == && bottom == ( << dep)) return pow((long long), dep);
int mid = << (dep - );
if(bottom <= mid) return * dfs(dep - , top, bottom);
if(top > mid) return dfs(dep - , top - mid, bottom - mid);
return * dfs(dep - , top, mid) + dfs(dep - , , bottom - mid);
} inline void solve() {
long long res = dfs(n, a, b);
printf(AUTO"\n", res);
} int main() {
readInteger(T);
for(int kase = ; kase <= T; kase++) {
init();
printf("Case %d: ", kase);
solve();
}
return ;
}
UVa 12627 Erratic Expansion - 分治的更多相关文章
- UVA - 12627 Erratic Expansion 奇怪的气球膨胀 (分治)
紫书例题p245 Piotr found a magical box in heaven. Its magic power is that if you place any red balloon i ...
- UVA 12627 - Erratic Expansion
一个红球能够分裂为3个红球和一个蓝球. 一个蓝球能够分裂为4个蓝球. 分裂过程下图所看到的: 设当前状态为k1.下一状态为k2. k1的第x行红球个数 * 2 ⇒ k2第2*x行的红球个数. k1的第 ...
- Uva 12627 Erratic Expansion(递归)
这道题大体意思是利用一种递归规则生成不同的气球,问在某两行之间有多少个红气球. 我拿到这个题,一开始想的是递归求解,但在如何递归求解的思路上我的方法是错误的.在研读了例题上给出的提示后豁然开朗(顺便吐 ...
- uva 12627 - Erratic Expansion(递归求解)
递归的边界条件写的多了--不是必需写呢么多的.. 不明确可共同探讨~ #include<cstdio> #include<iostream> #include<cmath ...
- UVA - 12627 Erratic Expansion(奇怪的气球膨胀)(递归)
题意:问k小时后,第A~B行一共有多少个红气球. 分析:观察图可发现,k小时后,图中最下面cur行的红气球个数满足下式: (1)当cur <= POW[k - 1]时, dfs(k, cur) ...
- UVA 12673 Erratic Expansion 奇怪的气球膨胀 (递推)
不难发现,每过一个小时,除了右下方的气球全都是蓝色以外,其他都和上一个小时的气球是一样的,所以是可以递推的.然后定义一类似个前缀和的东西f(k,i)表示k小时之后上面i行的红气球数.预处理出k小时的红 ...
- 12627 - Erratic Expansion——[递归]
Piotr found a magical box in heaven. Its magic power is that if you place any red balloon inside it ...
- 【数形结合】Erratic Expansion
[UVa12627]Erratic Expansion 算法入门经典第8章8-12(P245) 题目大意:起初有一个红球,每一次红球会分成三红一蓝,蓝球会分成四蓝(如图顺序),问K时的时候A~B行中有 ...
- UVa 12627 (递归 计数 找规律) Erratic Expansion
直接说几个比较明显的规律吧. k个小时以后,红气球的个数为3k. 单独观察一行: 令f(r, k)为k个小时后第r行红气球的个数. 如果r为奇数,f(r, k) = f((r+1)/2, k-1) * ...
随机推荐
- PLSQL Package包的使用
创建包头 create or replace package pak_kingsql is procedure pro_kingsql(p_one in varchar2,p_two out varc ...
- scp sparkuser@spark02:/home/sparkuser/.ssh
文件计算机传送 命令格式:{scp} {计算机用户}@{计算机网络名称}:{目标计算机路径} scp sparkuser@spark02:/home/sparkuser/.ssh
- HPC高性能计算知识: 异构并行计算
版权声明:很多其它内容,请关注[架构师技术联盟]公众号 https://blog.csdn.net/BtB5e6Nsu1g511Eg5XEg/article/details/80059122 当摩尔定 ...
- SQL 2
SQL SELECT 语句 SELECT 语句用于从数据库中选取数据. SQL SELECT 语句 SELECT 语句用于从数据库中选取数据. 结果被存储在一个结果表中,称为结果集. SQL SELE ...
- 万恶之源 - Python基础知识补充
编码转换 编码回顾: 1. ASCII : 最早的编码. ⾥⾯有英⽂⼤写字⺟, ⼩写字⺟, 数字, ⼀些特殊字符. 没有中⽂, 8个01代码, 8个bit, 1个byte 2. GBK: 中⽂国标码, ...
- Python一键安装全部依赖包
requirements.txt用来记录项目所有的依赖包和版本号,只需要一个简单的pip命令就能完成. pip freeze >requirements.txt 然后就可以用 pip insta ...
- PAT 1049 Counting Ones[dp][难]
1049 Counting Ones (30)(30 分) The task is simple: given any positive integer N, you are supposed to ...
- (转载)【cocos2dx 3.x Lua] 注册事件函数详解
出处: http://www.2cto.com/kf/201409/338235.html coocs2dx 版本 3.1.1 registerScriptTouchHandler 注册触屏事件 re ...
- [LeetCode] 38. Count and Say_Easy
The count-and-say sequence is the sequence of integers with the first five terms as following: 1. 1 ...
- [LeetCode] 717. 1-bit and 2-bit Characters_Easy
We have two special characters. The first character can be represented by one bit 0. The second char ...