题意:
给出一个数n,问在序列1到n的所有排列中,如果排列h某一位置的h[i]>h[i-1]&&h[i]>h[i+1],则价值v加上c[i],问v的期望是多少
思路:
其实还是有思路值得借鉴的,如果眼拙看不出来规律(比如我),不妨分析一波。
最后要求的是期望,由于期望满足可加性,所以我们只要算出来每个位置对于最后期望的贡献即可。在2~n-1的位置上,每个位置pos对于左右都有6种不同的大小情况,其中只有两种情况会使v加上c[pos],所以这位置的贡献是c[pos]/3
再看1和n这两个位置,每个有两种情况,其中只有一种会对最后产生影响,所以贡献是c[pos]/2。
这样就扫一遍把贡献都加上就行了。
代码:
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
using namespace std;
const int MAXN = 1005;
int a[MAXN];
int main() {
int n;
while (scanf("%d", &n) != EOF) {
for (int i = 1; i <= n; i++)
scanf("%d", &a[i]);
if (n == 1) {
printf("%.6f\n", a[1] * 1.0);
continue;
}
double ans = a[1] / 2.0 + a[n] / 2.0;
//cout << ans << endl;
for (int i = 2; i < n; i++)
ans += a[i] / 3.0;
printf("%.6f\n", ans);
}
return 0;
}