WenZhouTuHao is a very boring person(You will know who he is). There are n Building blocks. The blocks are number from 1 to n . For each i, the distance between the bottoms of Building blocks i and i+1 is exactly w . For each i,the height of Building blocks i is an integer bewteen 1 and H[i]. Tuhao want to take a single piece of rope and use it to join the top ends of all Building blocks. Tuhao want to know what the maximized length he need to join the top ends of all Building blocks.
输入
输出
output the maximized length of the rop we need .Your return value must have a relative or an absolute error of less than 1e-3.
样例输入
3 2
3 3 3
样例输出
5.657
提示
//东北大学 1535
#include <iostream>#include <cstdio>
#include <cstring>
#include <algorithm>
#include<string>
#include <string.h>
#include <cmath>
#include <cstdlib>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include<iomanip>
using namespace std;
double Dis(double w,int i,int j) // 计算高度分别为i j ,相距为w的 distance
{
return sqrt((i-j)*(i-j)+w*w);}
double l[101][101]={0}; //l[i][j] 代表前i栋楼,最后一栋楼为j高度,的最大值。
int h[101];
int n;
double w;
int main()
{
cin>>n;
cin>>w;
for(int i=1;i<=n;i++)
cin>>h[i];
for(int i=0;i<101;i++)
for(int j=0;j<101;j++)
l[i][j]=0;
for(int i=2;i<=n;i++)
for(int j=1;j<=h[i];j++)
{
double maxx=0;
for(int k=1;k<=h[i-1];k++){
maxx=max(maxx,l[i-1][k]+Dis(w,k,j));
}
l[i][j]=maxx;
}
printf("%.3f\n",l[n][h[n]]);
return 0;
}