POJ 1759 Garland (二分搜索)
来源:互联网 发布:微信小程序个人淘宝客 编辑:程序博客网 时间:2024/05/21 07:58
Garland
Time Limit: 1000MS Memory Limit: 10000KTotal Submissions: 1438 Accepted: 638
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.
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
由于B的大小正比于a[2] 所以二分a[2]就可以了 如果有小于0说明不满足太小 l = mid
AC代码如下:
//// Created by TaoSama on 2015-04-30// Copyright (c) 2015 TaoSama. All rights reserved.//#include <algorithm>#include <cctype>#include <cmath>#include <cstdio>#include <cstdlib>#include <cstring>#include <iomanip>#include <iostream>#include <map>#include <queue>#include <string>#include <set>#include <vector>using namespace std;const int INF = 0x3f3f3f3f;const int MOD = 1e9 + 7;const int N = 1e5 + 10;int n;double A, B, a[1005];//h2的值bool check(double x) { a[2] = x; for(int i = 3; i <= n; ++i) { a[i] = 2 * a[i - 1] + 2 - a[i - 2]; if(a[i] < 0) return false; } B = a[n]; return true;}int main() {#ifdef LOCAL freopen("in.txt", "r", stdin);// freopen("out.txt","w",stdout);#endif ios_base::sync_with_stdio(0); scanf("%d%lf", &n, &A); a[1] = A; double l = -1, r = 1e3 + 1; 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;}
0 0
- POJ 1759 Garland (二分搜索)
- poj 1759 Garland 二分搜索
- POJ 1759 - Garland(二分搜索)
- [POJ 1759] Garland (二分)
- POJ - 1759 Garland(二分)
- POJ 1759 Garland 二分答案
- POJ-1759-Garland
- uva 1555 Garland(二分)
- 【二分】Garland
- POJ 1759 Garland 已翻译
- URAL - 1066 Garland(二分)
- UVA1555-- Garland(推导+二分)
- poj1759 Garland二分
- POJ - Cable master (二分搜索)
- POJ - 3104 Drying (二分搜索)
- poj 2976 drop texts(二分搜索)
- poj 3258 River Hopscotch(二分搜索)
- POJ 1003 Hangover(二分搜索)
- xUtils系列之DbUtils-保存自定义类型
- smtp协议发邮件(需用户登录部分)
- SQLServer函数 left()、charindex()、stuff()的使用
- clearbox 中文详解
- [原]vs2010配置Qt
- POJ 1759 Garland (二分搜索)
- SGU - 134 Centroid 无根树转有根树 + 树形DP
- spring学习笔记(24)——xml方式配置事务
- 软件概要设计做什么,怎么做
- jpa @Temporal
- HDU 2196 树形DP经典题
- 例题6-2 铁轨(栈)
- 《鸟哥的Linux私房菜》读书笔记:登录文件(log)及其分析
- 计算磁盘IOPS