POJ 1759 - Garland(二分搜索)
2015-03-07 15:43
225 查看
题目:
http://poj.org/problem?id=1759
题意:
最左边灯的高度是a, 有n盏灯, 求出最右边灯的最小高度.
满足: H1 = A
Hi = (Hi-1 + Hi+1)/2 - 1, for all 1 < i < N
HN = B
Hi >= 0, for all 1 <= i <= N
题意:
二分搜索, 对第一盏灯和第二盏灯的距离进行二分.
AC.
#include <iostream>
#include <cstdio>
using namespace std;
const double esp = 1e-9;
int n;
double a, h[1005], b;
bool can(double x)
{
h[1] = a-x;
for(int i = 2; i < n; ++i) {
h[i] = 2 * h[i-1] + 2 - h[i-2];
if(h[i] < 0) return false;
}
b = h[n-1];
return true;
}
void solve()
{
double l = 0, r = a;
h[0] = a;
while(r - l > esp) {
double mid = (r + l) / 2;
if(can(mid)) l = mid;
else r = mid;
}
printf("%.2lf\n", b);
}
int main()
{
//freopen("in", "r", stdin);
while(~scanf("%d %lf", &n, &a)) {
solve();
}
return 0;
}
http://poj.org/problem?id=1759
题意:
最左边灯的高度是a, 有n盏灯, 求出最右边灯的最小高度.
满足: H1 = A
Hi = (Hi-1 + Hi+1)/2 - 1, for all 1 < i < N
HN = B
Hi >= 0, for all 1 <= i <= N
题意:
二分搜索, 对第一盏灯和第二盏灯的距离进行二分.
AC.
#include <iostream>
#include <cstdio>
using namespace std;
const double esp = 1e-9;
int n;
double a, h[1005], b;
bool can(double x)
{
h[1] = a-x;
for(int i = 2; i < n; ++i) {
h[i] = 2 * h[i-1] + 2 - h[i-2];
if(h[i] < 0) return false;
}
b = h[n-1];
return true;
}
void solve()
{
double l = 0, r = a;
h[0] = a;
while(r - l > esp) {
double mid = (r + l) / 2;
if(can(mid)) l = mid;
else r = mid;
}
printf("%.2lf\n", b);
}
int main()
{
//freopen("in", "r", stdin);
while(~scanf("%d %lf", &n, &a)) {
solve();
}
return 0;
}
相关文章推荐
- poj 1759 Garland 二分搜索
- poj 1759 Garland (二分搜索之其他)
- POJ 1759 Garland (二分搜索)
- POJ 1759 Garland(二分答案)
- POJ 1759 Garland 二分答案
- [POJ 1759] Garland (二分)
- POJ---1759(Garland,二分一个,求另一个的最优)
- POJ - 1759 Garland(二分)
- POJ 1064 1759 3484 3061 (二分搜索)
- POJ 3258 小牛过河 二分搜索最大的最小值
- POJ 2723 Get Luffy Out 2-SAT&&二分搜索
- poj 2391 floyd + 二分搜索 + 拆点建图 + dinic
- POJ - 1064 Cable master(二分搜索)
- POJ - 2976 Dropping tests(二分搜索:最大化平均值)
- POJ 3273 Monthly Expense 二分搜索
- POJ 1631-Bridging signals(最长上升子序列+二分搜索)
- POJ-2503(二分搜索)
- POJ 2456 Aggressive cows (二分搜索)
- POJ1759--Garland
- poj 2976(01分数规划搜索+二分答案)