POJ 1759(数学递推+二分)
来源:互联网 发布:乐知英语的公司在哪 编辑:程序博客网 时间:2024/04/20 09:32
Description
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
Output
Sample Input
692 532.81
Sample Output
446113.34
首先要用数学知识推导,然后根据推导出来的公式的性质计算,最后用二分暴搜答案
题意
- 题目背景是在绳上挂灯。。我觉得完全不用鸟这个背景。。。
- 它表达的核心意思,就是一个数列告诉你首项h1, 项数n,递推式hi = (hi+1 + hi-1 ) / 2 - 1
- 在保证hi都大于等于0的前提下,让hn最小
思路
- 这题不难想到可以使用二分,但是二分哪个变量,找到哪样的单调关系需要考虑一下。
- 首先,通过题目给出的递推式,很容易得到标准的递推形式: hi+1 = 2hi - hi-1 + 2
- 这样,我们就知道,只要确定了h2,hn肯定就确定了,所以我们二分的变量就是h2了
- 接下来,我们从递推式入手,来找单调关系
- 假如递推式为:hi = (hi+1 + hi-1 ) / 2,那会有怎样的结果呢?不难想象,当h2确定时,整个数列就是一个等差数列了,所以数列肯定是单调的。
- 而如果hi在这个基础上减一呢?不难想象,在hi+1 < hi-1时,hi位于两个数等分位偏hi+1的位置一些,而在hi+1 > hi-1时,hi位于两个数等分位偏hi-1的位置一些。所以在h2 < h1的情况下,整个数列画出的图像一定是先单调递减,但递减幅度越来越小,直至幅度减到足够小,然后递增,并且递增幅度越来越大的过程。
- 因此,我们的单调关系就是,f(h2): hi是否都大于等于0
Hi = (i-2) * H2 - (i-1) * H1 + (i-1)*(i-2)
,也就意味着H2和Hn成正相关,求最小的Hn也就变成了求最小的H2。#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <queue>
#include <map>
using namespace std;
const int N = 1100;
double h[N];
int judge(double y);
double x;
int n;
int main()
{
while(scanf("%d %lf", &n, &x)!=EOF)
{
double l=0, r=x, mid, ans;
for(int i=0;i<100;i++)
{
mid=(l+r)/2;
if(judge(mid))
{
ans=mid;
r=mid;
}
else
{
l=mid;
}
}
printf("%.2f\n",(n-1)*ans-(n-2)*x+(n-2)*(n-1));
}
return 0;
}
int judge(double y)
{
if(y<0)
{
return 0;
}
h[1]=x, h[2]=y;
for(int i=3;i<=n;i++)
{
h[i]=2*h[i-1]-h[i-2]+2;
if(h[i]<0)
{
return 0;
}
}
return 1;
}
- POJ 1759(数学递推+二分)
- POJ 1850 Code (递推数学)
- poj 2506 Tiling(数学:递推+高精度)
- POJ 2002(hash||二分,数学)
- POJ - 1905 Expanding Rods (数学+二分)
- POJ-3252-Round Numbers-组合数学-递推
- poj 1037 A decorative fence(递推+组合计数/数学)
- SOJ-2857(数学递推公式)
- HLJUOJ1128 && HDU2046(数学递推)
- 汉诺塔IV (递推+数学)
- 汉诺塔II (递推+数学)
- 折线分割平面(递推+数学)
- HRBUST1589(数学递推)
- Hduoj2501【数学递推】
- hdu5492(递推+数学)
- 递推 数学
- [POJ 2002]Squares[二分][数学]
- POJ 1019(递推)
- 微软的十大变动
- 最长不下降子序列 (nlgn)做法
- STM32库内程序一些好的编程思路、方法及好的程序
- maven项目中引入外部数据库链接文件db.properties中的一个bug
- composer 基本使用方法
- POJ 1759(数学递推+二分)
- MSM8937平台bootloader调试之一
- Python 【Day2】
- Majority Element
- poj1742(多重背包dp)
- inent的用法
- 【U3D日记-2016年9月2日】设计模式解决工作问题的一个实例
- c++中定制输入输出操作
- TIJ读书笔记02-控制执行流程