Description
NanoApe, the Retired Dog, has returned back to prepare for the National Higher Education Entrance Examination! In math class, NanoApe picked up sequences once again. He wrote down a sequence with numbers on the paper and then randomly deleted a number in the sequence. After that, he calculated the maximum absolute value of the difference of each two adjacent remained numbers, denoted as . Now he wants to know the expected value of , if he deleted each number with equal probability.Input
The first line of the input contains an integer , denoting the number of test cases. In each test case, the first line of the input contains an integer , denoting the length of the original sequence. The second line of the input contains integers , denoting the elements of the sequence.Output
For each test case, print a line with one integer, denoting the answer. In order to prevent using float number, you should print the answer multiplied by .Sample Input
1 4 1 2 3 4Sample Output
6
给你n个数,现在要删一个数,删每个数的概率是一样的, 现在问你删一个值后的相邻数绝对值最大差的期望是多少, 因为担心精度误差,让你答案乘n 先把每个相邻数的差求出来 sort一下 要记住id 然后就一个一个删 求出当前数列的相邻数差的最大值 加就行了 要注意当 n = 3时 只有两个差值 所以当删除第二个数时就没有差值了 要特殊处理 直接 dmax = t 就在这里WA了一发
还有就是不能用cin输入 就在这里T了一发。。。
#include <iostream> #include <algorithm> #include <cstdio> #include <cstring> #include <queue> #include <vector> #include <cmath> #include <stack> #include <string> #include <map> #include <set> #define pi acos(-1) #define LL long long #define ULL unsigned long long #define inf 0x3f3f3f3f #define INF 1e18 #define lson l,mid,rt<<1 #define rson mid+1,r,rt<<1|1 using namespace std; typedef pair<int, int> P; const int maxn = 1e5 + 5; LL a[maxn]; struct Node { LL d, id; }dif[maxn]; int cmp(Node a, Node b) { return a.d > b.d; } int main(void) { // freopen("C:\\Users\\wave\\Desktop\\NULL.exe\\NULL\\in.txt","r", stdin); LL T, n, i, j, dmax, t, sum, dex; scanf("%I64d", &T); while (T--) { scanf("%I64d", &n); scanf("%I64d", &a[1]); for (i = 2; i <= n; i++){ scanf("%I64d", &a[i]); t = a[i] - a[i-1]; if (t < 0) t = -t; dif[i-1].d = t; dif[i-1].id = i-1; } sort(dif+1, dif+n, cmp); sum = 0; dex = 1; while (dif[dex].id == 1) dex++; sum += dif[dex].d; dex = 1; while (dif[dex].id == n-1) dex++; sum += dif[dex].d; for (i = 2; i <= n-1; i++){ t = a[i-1] - a[i+1]; if (t < 0) t = -t; dex = 1; while (dif[dex].id == i || dif[dex].id == i-1) dex++; if (dex == n) dmax = t; else dmax = max(t, dif[dex].d); sum += dmax; } printf("%I64d\n", sum); } return 0; }