LightOJ 1030 Discovering Gold

期望,$dp$。

设$ans[i]$为$i$为起点,到终点$n$获得的期望金币值。$ans[i]=(ans[i+1]+ans[i+2]+ans[i+3]+ans[i+4]+ans[i+5]+ans[i+6])/6+a[i]$,不到$6$个的单独处理一下。

#pragma comment(linker, "/STACK:1024000000,1024000000")
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<vector>
#include<map>
#include<set>
#include<queue>
#include<stack>
#include<iostream>
using namespace std;
typedef long long LL;
const double pi=acos(-1.0),eps=1e-;
void File()
{
freopen("D:\\in.txt","r",stdin);
freopen("D:\\out.txt","w",stdout);
}
template <class T>
inline void read(T &x)
{
char c = getchar();
x = ;
while(!isdigit(c)) c = getchar();
while(isdigit(c)) { x = x * + c - ''; c = getchar(); }
} int a[],T,n;
double ans[]; int main()
{
scanf("%d",&T); int cas=;
while(T--)
{
scanf("%d",&n);
for(int i=;i<=n;i++)
{
scanf("%d",&a[i]);
ans[i]=1.0*a[i];
}
for(int i=n-;i>=;i--)
{
int cnt=; double sum=;
for(int j=;j<=;j++)
{
if(i+j>n) break;
cnt++; sum=sum+ans[i+j];
}
ans[i]=ans[i]+sum/cnt;
}
printf("Case %d: %lf\n",cas++,ans[]);
}
return ;
}
上一篇:mac下安装apache+php+mysql


下一篇:【数据结构与算法Python版学习笔记】递归(Recursion)——优化问题与策略