这个题比赛的时候没做出来,赛后根据题解做的,基本上按照题解的思路来的。不过需要注意的是中间尽量避免精度丢失,我最开始用的直接算每个栈里存的b,后面又一个一个算,结果导致精度丢了。
代码:
#include<cstdio>
#include<cstring>
#include<iostream>
#include<stack>
using namespace std;
const int maxn=1e5+100;
struct Node
{
double sum;
double val;
int len;
Node(){}
Node(int ssum,int slen)
{
sum=ssum;
len=slen;
}
};
int a[maxn],n;
stack<Node> s;
int main()
{
int T;
scanf("%d",&T);
while(T--)
{
scanf("%d",&n);
for(int i=1;i<=n;i++)
scanf("%d",&a[i]);
for(int i=1;i<=n;i++)
{
if(!s.empty()&&s.top().sum/s.top().len>=a[i])
{
double sum=a[i];
int cnt=1;
while(!s.empty()&&s.top().sum/s.top().len>=sum/cnt)
{
cnt+=s.top().len;
sum+=s.top().sum;
s.pop();
}
s.push(Node(sum,cnt));
}
else
s.push(Node(a[i],1));
}
double ans=0;
int now=n;
while(!s.empty())
{
double p=s.top().sum/s.top().len;
ans+=(1-p)*(1-p)*s.top().sum+p*p*(s.top().len-s.top().sum);
s.pop();
}
printf("%.6f\n",ans);
}
return 0;
}