4756. 【NOIP2016提高A组模拟9.4】幻象 (Standard IO)
Time Limits: 1000 ms Memory Limits: 1001472 KB Detailed Limits
Description
phantom是一位爱思考的哲♂学家。
最近phantom得到了森の妖精的真传。在他练功的时候, 每秒他的思绪中都有一定的概率浮现出奇♂异的幻象,持续x秒的幻象将产生x^2 的幻象值。
phantom练功发自真心,他想知道,在N秒内他期望产生的幻象值是多少。
Input
第一行包含 1 个正整数 N ,表示总时间 N 秒。
第二行包含 N 个用空格隔开的在[0,100]之间的正整数,其中第i个数a[i]表示第i秒浮现幻象的概率为百分之a[i]。
Output
1 个实数,四舍五入后保留一位小数,表示期望幻象值。
Sample Input
3
50 50 50
Sample Output
2.8
Data Constraint
对于 40%的数据 N ≤ 10
对于 60%的数据 N ≤ 100
对于 100%的数据,N ≤ 10^6
数据规模较大,请使用效率较高的读入方式。
Source / Author: phantom
题解:
设 L[i]为第 i 秒幻象的持续时间的期望. 显然 L[i] = (L[i-1] + 1) * a[i]%
第一种情况:不选,持续时间为0,贡献为0
第二种情况:选 , 期望持续时间是 (L[i-1] + 1)*a[i]% , 就是前面的期望长度+1再乘继续选这一位的概率。
N=3时
L[1] = 1*a1
L[2] = L[1] *a2+a2 = a1*a2 + a2 (两个都选 或 第一不选)
L[3] = L[2] * a3 +a3 = a1*a2*a3 + a2*a3 + a3 (同理)
设 f[i]表示前 i 秒的答案 f[i] = f[i-1] + ((L[i-1] + 1)^2 – L[i-1]^2) * a[i]% 时间复杂度为 O(N)
((L[i-1] + 1)^2 – L[i-1]^2)
若要算i的答案,要把之前的剪掉,最后乘上选i的概率。
#include<bits/stdc++.h>
#define mem(a,b) memset(a,b,sizeof(a))
#define rint register int
#define N 1000010
#define open(x) freopen(x".in","r",stdin);freopen(x".out","w",stdout);
#define inf 2147483647
using namespace std;
int i,j,n,t,a[N];
double l[N],f[N];
double sqr(double x){return x*x;}
void read(int &x)
{
x=0;
char ch=getchar();
while(!isdigit(ch))ch=getchar();
while(isdigit(ch))x=(x<<1)+(x<<3)+ch-'0',ch=getchar();
return ;
}
int main()
{
scanf("%d",&n);
for(i=1;i<=n;i++)read(a[i]);
for(i=1;i<=n;i++)l[i] = (l[i-1]+1)*a[i]/100;
for(i=1;i<=n;i++)f[i] = f[i-1] + (sqr(l[i-1]+1) - sqr(l[i-1]))*a[i]/100;
printf("%.1lf",f[n]);
return 0;
}