poj 1759 Garland

Garland

Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 2365   Accepted: 1007

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

/*
* @Author: Lyucheng
* @Date:   2017-07-25 10:07:16
* @Last Modified by:   Lyucheng
* @Last Modified time: 2017-07-29 19:25:29
*/
/*
 题意:有一串项链,给出第一个珠子的位置,然后保证每个珠子不能掉到地上,也就是说高度必须大于等于零,让你求最后一个珠子的位置

 思路:二分答案就可以,判断条件可以推出公式
    H1=A
    H2=A/2 + H3/2 - 1
    H3=A/3 + (H4*2)/3 - 2
    ...
    Hn-1=A/(n-1) + (Hn*n-2)/n-1 - (n-2)
    然后逆向推过来

 错误:上面的公式可能存在损失精度的问题...可是打印了所有答案,真的没错...poj C++能过,但是G++就过不了

 改进:H[i] = 2 * H[i - 1] + 2 - H[i - 2];

 还有个问题,用printf输出就不行,用cout输出就可以
*/

#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <string>
#include <math.h>
#include <stdlib.h>
#include <time.h>
#include <iomanip>

#define MAXL -1
#define MAXR 1000+16
#define MAXN 1000+16
#define EXP 1e-9

using namespace std;

int n;
double A,B;
double F[MAXN];

bool ok(const double &x){
    F[2]=x;
    for(int i=3;i<=n;i++){
        F[i]=2*F[i-1]+2-F[i-2];
        if(F[i]<0) return false;
    }
    B=F[n];
    return true;
}

int main(){
    // freopen("in.txt", "r", stdin);
    // freopen("out.txt", "w", stdout);
    cin>>n>>A;
    F[1]=A;
    double l=MAXL,r=MAXR,mid;
    for(int i=0;i<100;i++){
        mid=(l+r)/2.0;
        if(ok(mid)==true){
            r=mid;
        }else{
            l=mid;
        }
    }
    cout << fixed << setprecision(2) << B << endl;
    return 0;
}
时间: 2024-10-10 15:26:35

poj 1759 Garland的相关文章

POJ 1759 Garland(二分+数学递归+坑精度)

POJ 1759 Garland  这个题wa了27次,忘了用一个数来储存f[n-1],每次由于二分都会改变f[n-1]的值,得到的有的值不精确,直接输出f[n-1]肯定有问题. 这个题用c++交可以过,g++交过不了, f[i]=2+2*f[i-1]-f[i-2]; f[0]=A,f[1]=x; 二分枚举x的值,最终得到B的值(不是f[n-1]), 上述递推式是非齐次的二阶递推式,解其齐次特征方程,可以得到f[n-1]的齐次时的表达式,对于非齐次的,目前我还没法求出通式, B与f[n-1]的关

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 che

poj 1759 Garland (二分搜索之其他)

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

POJ 1759(二分

题意还是需要看原题,不好描述. 实际上这个题只需要注意一点,就是把关系式改成递推式,然后就可以发现第二项完全决定了后面的位置,所以二分第二项的高度即可. import java.util.*; import java.io.*; import java.math.*; public class Main { public static final int maxv = 200005; public static final int shift = 100000; static int N; st

POJ 1064 1759 3484 3061 (二分搜索)

POJ 1064 题意 有N条绳子,它们长度分别为Li.如果从它们中切割出K条长度相同的绳子的话,这K条绳子每条最长能有多长?答案保留小数点后2位. 思路 二分搜索.这里要注意精度问题,代码中有详细说明:还有printf的%.2f会四舍五入的,需要*100再取整以截取小数点后两位. #include<stdio.h> #include<string.h> #include<string> #include<iostream> #include<math

POJ1759 Garland —— 二分

题目链接:http://poj.org/problem?id=1759 Garland Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 2477   Accepted: 1054 Description The New Year garland consists of N lamps attached to a common wire that hangs down on the ends to which outermo

ACM训练方案-POJ题目分类

ACM训练方案-POJ题目分类 博客分类: 算法 ACM online Judge 中国: 浙江大学(ZJU):http://acm.zju.edu.cn/ 北京大学(PKU):http://acm.pku.edu.cn/JudgeOnline/ 杭州电子科技大学(HDU):http://acm.hdu.edu.cn/ 中国科技大学(USTC):http://acm.ustc.edu.cn/ 北京航天航空大学(BUAA)http://acm.buaa.edu.cn/oj/index.php 南京

转载:poj题目分类(侵删)

转载:from: POJ:http://blog.csdn.net/qq_28236309/article/details/47818407 按照ac的代码长度分类(主要参考最短代码和自己写的代码) 短代码:0.01K–0.50K:中短代码:0.51K–1.00K:中等代码量:1.01K–2.00K:长代码:2.01K以上. 短:1147.1163.1922.2211.2215.2229.2232.2234.2242.2245.2262.2301.2309.2313.2334.2346.2348

POJ - 3186 Treats for the Cows (区间DP)

题目链接:http://poj.org/problem?id=3186 题意:给定一组序列,取n次,每次可以取序列最前面的数或最后面的数,第n次出来就乘n,然后求和的最大值. 题解:用dp[i][j]表示i~j区间和的最大值,然后根据这个状态可以从删前和删后转移过来,推出状态转移方程: dp[i][j]=max(dp[i+1][j]+value[i]*k,dp[i][j-1]+value[j]*k) 1 #include <iostream> 2 #include <algorithm&