poj1759 数学+二分
来源:互联网 发布:淘宝网怎么没有一淘了 编辑:程序博客网 时间:2024/06/05 23:39
Garland
Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 2466 Accepted: 1047
Description
The New Year garland consists of N lamps attached to a common wire that hangs down on the ends to which outermost lamps are affixed. The wire sags under the weight of lamp in a particular way: each lamp is hanging at the height that is 1 millimeter lower than the average height of the two adjacent lamps.
The leftmost lamp in hanging at the height of A millimeters above the ground. You have to determine the lowest height B of the rightmost lamp so that no lamp in the garland lies on the ground though some of them may touch the ground.
You shall neglect the lamp’s size in this problem. By numbering the lamps with integers from 1 to N and denoting the ith lamp height in millimeters as Hi we derive the following equations:
H1 = A
Hi = (Hi-1 + Hi+1)/2 - 1, for all 1 < i < N
HN = B
Hi >= 0, for all 1 <= i <= N
The sample garland with 8 lamps that is shown on the picture has A = 15 and B = 9.75.
Input
The input file consists of a single line with two numbers N and A separated by a space. N (3 <= N <= 1000) is an integer representing the number of lamps in the garland, A (10 <= A <= 1000) is a real number representing the height of the leftmost lamp above the ground in millimeters.
Output
Write to the output file the single real number B accurate to two digits to the right of the decimal point representing the lowest possible height of the rightmost lamp.
Sample Input
692 532.81
Sample Output
446113.34
Source
Northeastern Europe 2000
个人理解:推到公式,并不能直接二分结果。从公式中可以看出H[2]如果已知,那么结果就能得出,并且满足单调性。
对二分的理解:把对结果单调的条件由未知变为已知,那么就可以得出一种最优解策略,按照这种最优解策略判断二分的数值是否可行。(如果没有此条件那么很难找出最优解策略,故可以用二分并加条件)
#include<iostream>#include<math.h>#include<cstring>#include<algorithm>#include<set>#include<map>#include<queue>#include<cstdio>#include<vector>#include<iomanip>const int INF=0x7fffffff;const int maxn=2000;using namespace std;double A,B;int N;double H[maxn];bool C(double mid){ H[1]=mid; for(int i=2;i<N;i++) { H[i]=2.0*H[i-1]+2-H[i-2]; if(H[i]<0) return false; } B=H[N-1]; return true;}int main(){ cin>>N>>A; double lb=-1,ub=1000+10; H[0]=A; for(int i=0;i<100;i++) { double mid=(lb+ub)/2; if(C(mid)) ub=mid; else lb=mid; } cout << fixed << setprecision(2) << B << endl; return 0;}
- poj1759 数学+二分
- poj1759 二分
- 推公式+二分--poj1759
- poj1759 Garland二分
- poj1759
- poj1759
- POJ1759
- poj1759(二分找寻最值)
- 《挑战程序设计竞赛》3.1.5 二分搜索-其它 POJ1759 3484
- POJ1759--Garland
- poj1759 Garland
- POJ1759-Garland
- POJ1759-Garland
- hdu 2899 (数学+二分)
- Hduoj2817【数学二分】
- [SCU4437]Carries[数学][二分]
- hdu 2899 二分+数学
- HDU2675 数学 二分
- 为什么要有一个好的架构
- 权限管理--权限资源管理
- mybatis学习之路----模糊查询实现
- 那些年开发通知栏遇到的坑
- ORACLE基本数据类型总结
- poj1759 数学+二分
- Jquery实现子菜单散开动画
- JavaScript 执行环境 与 变量对象
- vue中select问题
- python小练习9(这些简单的题目你都会吗)
- C/C++中的NULL与nullptr
- 1021: 恶搞指数
- 主键和唯一索引的区别
- 2017-09-09 LeetCode_013 Roman to Integer