POJ 1759 Garland(二分答案)
【题目链接】 http://poj.org/problem?id=1759
【题目大意】
有n个数字H,H[i]=(H[i-1]+H[i+1])/2-1,已知H[1],求最大H[n],
使得所有的H均大于0.
【题解】
我们得到递推式子H[i]=2*H[i-1]+2-H[i-2],发现H[n]和H[2]成正相关
所以我们只要二分H[2]的取值,同时计算每个H是否大于等于0即可。
【代码】
#include <cstdio>
int n;
double H[1010],A,B;
bool check(double x){
H[1]=A,H[2]=x;
for(int i=3;i<=n;i++){
H[i]=2.0*H[i-1]+2-H[i-2];
if(H[i]<0)return 0;
}return B=H[n],1;
}
int main(){
while(~scanf("%d%lf",&n,&A)){
double l=0,r=A;
for(int i=0;i<100;i++){
double mid=(l+r)/2;
if(check(mid))r=mid;
else l=mid;
}printf("%.2f\n",B);
}return 0;
}
POJ 1759 Garland(二分答案)的更多相关文章
- POJ 1759 Garland(二分+数学递归+坑精度)
POJ 1759 Garland 这个题wa了27次,忘了用一个数来储存f[n-1],每次由于二分都会改变f[n-1]的值,得到的有的值不精确,直接输出f[n-1]肯定有问题. 这个题用c++交可以 ...
- poj 1759 Garland
Garland Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 2365 Accepted: 1007 Descripti ...
- POJ 3104 Drying(二分答案)
题目链接:http://poj.org/problem?id=3104 ...
- POJ 3122 Pie 二分答案
题意:给你n个派,每个派都是高为一的圆柱体,把它等分成f份,每份的最大体积是多少. 思路: 明显的二分答案题-- 注意π的取值- 3.14159265359 这样才能AC,,, //By Sirius ...
- poj 1759 Garland (二分搜索之其他)
Description The New Year garland consists of N lamps attached to a common wire that hangs down on th ...
- POJ Building roads [二分答案 2SAT]
睡觉啦 #include <iostream> #include <cstdio> #include <cstring> #include <algorith ...
- Poj 1743 Musical Theme(后缀数组+二分答案)
Musical Theme Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 28435 Accepted: 9604 Descri ...
- 字符串hash + 二分答案 - 求最长公共子串 --- poj 2774
Long Long Message Problem's Link:http://poj.org/problem?id=2774 Mean: 求两个字符串的最长公共子串的长度. analyse: 前面在 ...
- POJ 1064 Cable master (二分答案)
题目链接:http://poj.org/problem?id=1064 有n条绳子,长度分别是Li.问你要是从中切出m条长度相同的绳子,问你这m条绳子每条最长是多少. 二分答案,尤其注意精度问题.我觉 ...
随机推荐
- Python学习笔记(五)Python的切片和迭代
切片 Python提供了切片操作符,可以对list.tuple.字符串进行截取操作. list中的切片应用 语法如下: >>> L = ['Michael', 'Sarah', 'T ...
- locate命令的安装
linux中locate命令可以快速定位我们需要查找的文件,但是在yum中,locate的安装包名为mlocate(yum list | grep locate可以查看),安装方法: yum -y i ...
- break语句的使用
先举一个简单点的例子 #include<stdio.h> #include<stdlib.h> int main() { float a,b; char c; printf(& ...
- How Many Fibs?
How Many Fibs? 点我 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others ...
- python操作 redis-list
#!/usr/bin/python #!coding: utf-8 import redis if __name__=="__main__": try: conn=redis.St ...
- javascript总结--2014-04-17
HTML DOM Function Data http://www.oschina.net/translate/learning-javascript-design-patterns?cmp& ...
- Android 自定义PopupWindow动画效果
public class RollActivity extends Activity { private View view; private Button btn; private PopupWin ...
- 强烈推荐一款CSS导航菜单
强烈推荐一款CSS导航菜单,用到政府学校类网站上超级不错,有点类似站长网菜单的味道,只不过颜色不一样而已,这种菜单还不是真正意义上的“下拉”菜单,应该叫滑出菜单吧?反正比较不错,不多说了. <! ...
- linux 用户空间获得纳秒级时间ns
一.引言 我们在测试程序的性能的时候往往需要获得ns级的精确时间去衡量一个程序的性能,下面介绍下linux中用户空间获得ns级时间的方法 二.用户空间获得ns级时间 使用clock_gettime函数 ...
- 【学习总结】autostart 与 init
学习总结/etc/xdg/autostart/xxx.desktop,是开机从登录界面跳转到桌面启动的,可以拿到桌面环境变量,用户id是“普通用户”,如果自启动的程序文件所属者为root,则需要 执行 ...