POJ 1003 解题分析
2010-07-08 11:53
417 查看
Technorati 标签: ACM,POJ
真的好囧啊,1001还放着没做呢就先把1002和1003做了。本着做一篇写一篇的原则先把分析写上来的。
Memory Limit: 10000K
Total Submissions: 57207
Accepted: 26884
有
对于给定的
找到最小的n使得
很显然,一个一个从小往大试肯定不行。我暂时想到了两种方法来解这道题。
调和级数:形如
的级数称为调和级数,它是p=1的p级数。
调和级数是发散级数。在n趋于无穷时其部分和没有极限(或部分和为无穷大)。
调和级数公式:
其中r为常数,Euler近似地计算了r的值,约为0.577218。这个数字就是后来称作的欧拉常数。
关于调和级数的更多信息,请移步百度百科调和级数条目(话说维基百科被墙的很厉害,图都看不到,实在是令我不得不吐槽一下)
那么现在我就非常开心啦,把这个公式变换一下:
当然,最终还得对n取整数上界
整个算法也相当简单了,这里r我取的0.5772156649015328,精度够高了吧。
然而杯具还是发生了,WA了,后来发现这个算法精度还是不够高,大概差了0.1吧,只好放弃了T_T
因此我估计最终输出的范围应该在2~300之间,经过计算,发现实际上为277。
根据这些,我们能够推断出这道题主要的耗时部分在于查找,因此可以将所有数据算出来构建一个利于查找的数据结构进行查找。
以f(n)的值为key,n为value建立平衡二叉查找树,查找c应该插入的位置,取出n即为所求。
需要注意的是,由于数据已知,在构建平衡二叉查找树的时候,可以有目的的选择数据,以减少平衡旋转的消耗。
这里是我通过的代码:
甚至于可以更极端一些,直接将树用静态结构建好。(反正数据都是已知的,还不用修改)
总的时间复杂性为T(n) = O(n) + O(n) + mO(logn) = O(n) + mO(logn)
其中,n为276,m为输入c的个数。
总的来说,如果没有注意到初始数据规模很小,可以一次性计算出来然后建立适合查找的数据结构的话,这道题就很难解了:)
真的好囧啊,1001还放着没做呢就先把1002和1003做了。本着做一篇写一篇的原则先把分析写上来的。
题目描述
题目链接:POJ 1003Hangover
Time Limit: 1000MSMemory Limit: 10000K
Total Submissions: 57207
Accepted: 26884
Description
How far can you make a stack of cards overhang a table? If you have one card, you can create a maximum overhang of half a card length. (We're assuming that the cards must be perpendicular to the table.) With two cards you can make the top card overhang the bottom one by half a card length, and the bottom one overhang the table by a third of a card length, for a total maximum overhang of 1/2 + 1/3 = 5/6 card lengths. In general you can make n cards overhang by 1/2 + 1/3 + 1/4 + ... + 1/(n + 1) card lengths, where the top card overhangs the second by 1/2, the second overhangs tha third by 1/3, the third overhangs the fourth by 1/4, etc., and the bottom card overhangs the table by 1/(n + 1). This is illustrated in the figure below.Input
The input consists of one or more test cases, followed by a line containing the number 0.00 that signals the end of the input. Each test case is a single line containing a positive floating-point number c whose value is at least 0.01 and at most 5.20; c will contain exactly three digits.Output
For each test case, output the minimum number of cards necessary to achieve an overhang of at least c card lengths. Use the exact output format shown in the examples.Sample Input
1.00 3.71 0.04 5.19 0.00
Sample Output
3 card(s) 61 card(s) 1 card(s) 273 card(s)
Source
Mid-Central USA 2001解题分析
转换成形式化描述:有
对于给定的
找到最小的n使得
很显然,一个一个从小往大试肯定不行。我暂时想到了两种方法来解这道题。
调和级数
第一眼看这道题的序列就非常眼熟,查了一下资料,果然被我找到了,原来是高数课学过(汗,高数没好好学,真是惭愧)。调和级数:形如
的级数称为调和级数,它是p=1的p级数。
调和级数是发散级数。在n趋于无穷时其部分和没有极限(或部分和为无穷大)。
调和级数公式:
其中r为常数,Euler近似地计算了r的值,约为0.577218。这个数字就是后来称作的欧拉常数。
关于调和级数的更多信息,请移步百度百科调和级数条目(话说维基百科被墙的很厉害,图都看不到,实在是令我不得不吐槽一下)
那么现在我就非常开心啦,把这个公式变换一下:
当然,最终还得对n取整数上界
整个算法也相当简单了,这里r我取的0.5772156649015328,精度够高了吧。
int main() { do { double goal; cin >> goal; if(abs(goal - 0.) < 0.001) break; double const r = 0.5772156649015328; int n = static_cast<int>(exp(goal - r + 1) - 1 + 0.5); cout << n << " card(s)" << endl; } while(true); return 0; }
然而杯具还是发生了,WA了,后来发现这个算法精度还是不够高,大概差了0.1吧,只好放弃了T_T
平衡二叉查找树
需要注意的是,给定的c在0.01和5.20之间,而例子中有一条数据是这样的:输入为5.19,输出为273。因此我估计最终输出的范围应该在2~300之间,经过计算,发现实际上为277。
根据这些,我们能够推断出这道题主要的耗时部分在于查找,因此可以将所有数据算出来构建一个利于查找的数据结构进行查找。
以f(n)的值为key,n为value建立平衡二叉查找树,查找c应该插入的位置,取出n即为所求。
需要注意的是,由于数据已知,在构建平衡二叉查找树的时候,可以有目的的选择数据,以减少平衡旋转的消耗。
这里是我通过的代码:
#include <iostream>; #include <cmath>; #include <map>; using namespace std; int main() { map<double, int> harmonicSeriesMap; double sum = 0; for(int i=2; i<=276; i++) { sum += 1.0/i; harmonicSeriesMap[sum] = i; } do { double goal; cin >> goal; if(abs(goal - 0.) > 0.001) break; map<double, int>::const_iterator i = harmonicSeriesMap.lower_bound(goal); cout << i->second - 1 << " card(s)"; << endl; } while(true); return 0; }
甚至于可以更极端一些,直接将树用静态结构建好。(反正数据都是已知的,还不用修改)
总的时间复杂性为T(n) = O(n) + O(n) + mO(logn) = O(n) + mO(logn)
其中,n为276,m为输入c的个数。
总结
这道题如果能用调和级数来解的话那就太快了,哈哈。总的来说,如果没有注意到初始数据规模很小,可以一次性计算出来然后建立适合查找的数据结构的话,这道题就很难解了:)
相关文章推荐
- POJ 1003解题报告
- POJ 1007 解题分析
- POJ1003解题报告
- poj 1080 Human Gene Functions 解题报告(附详细分析)
- POJ1003解题方案
- POJ 1003求Hangover解题报告——【PKU ACM】
- POJ 1003解题报告
- POJ 1001 解题分析
- POJ 1060 Post Office 分析&解题报告
- [POJ 1003] Hangover C++解题
- 北大POJ解题报告-1003 Hangover
- POJ 1003 解题报告 Hangover
- poj 1003 Hangover 解题报告
- POJ 1003 解题报告
- POJ 1003 Hangover [解题报告] Java
- POJ 1006 解题分析
- POJ 1008 解题分析
- POJ 1003 Hangover 解题报告
- POJ 1003(Hangover 简单数学) 解题报告
- poj 1003 Hangover 解题报告