NanoApe Loves Sequence
题目链接:点我打开链接
题意:在数学课上,NanoApe 心痒痒又玩起了数列。他在纸上随便写了一个长度为 n的数列,他又根据心情随便删了一个数,这样他得到了一个新的数列,然后他计算出了所有相邻两数的差的绝对值的最大值。
这个最大值会随着他删了的数改变而改变,所以他想知道假如全部数被删除的概率是相等的话,差的绝对值的最大值的期望是多少。
这个最大值会随着他删了的数改变而改变,所以他想知道假如全部数被删除的概率是相等的话,差的绝对值的最大值的期望是多少。
为防止精度误差,你需要输出答案乘上n后的值。
题解:求出前i个数里相邻差值的最大值
,i到n里相邻差值的最大值
,那么
。时间复杂度O(n)。
AC代码:
//#include<bits/stdc++.h>
#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<iostream>
#include<cstring>
#include<vector>
#include<map>
#include<queue>
#include<set>
#include<stack>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const int N = 100000+10;
int a[N];
int MAX[3];
int MAXN[3];
int read()
{
int v = 0, f = 1;
char c =getchar();
while( c < 48 || 57 < c ){
if(c=='-') f = -1;
c = getchar();
}
while(48 <= c && c <= 57)
v = v*10+c-48, c = getchar();
return v*f;
}
int main()
{
int t,n;
scanf("%d",&t);
while(t--)
{
scanf("%d",&n);
for(int i = 1; i<=n; i++) scanf("%d",&a[i]);
memset(MAX,0,sizeof(MAX));
for(int i = 1; i < n ; i++){
int tmp=max(a[i] - a[i+1] , a[i+1] - a[i] );
for(int j=0;j<3;j++)
{
if(tmp>=MAX[j])
{
for(int k=2;k>j;--k){
MAX[k]=MAX[k-1];
MAXN[k]=MAXN[k-1];
}
MAX[j] = tmp ;
MAXN[j] = i;
break;
}
}
}
ll ans = 0;
int tmp = 0;
for(int j=0;j<3;j++)
{
if(MAXN[j] != 1)
tmp=max(tmp,MAX[j]);
}
ans+=tmp;
tmp = 0;
for(int j=0;j<3;j++)
{
if(MAXN[j] != n-1)
tmp=max(tmp,MAX[j]);
}
ans+=tmp;
for(int i=2;i<n;i++){
int tmp =max( a[i-1] - a[i+1] , a[i+1] - a[i-1]) ;
for(int j=0;j<3;j++){
if(MAXN[j] != i-1 && MAXN[j] !=i ){
tmp=max(tmp,MAX[j]);
}
}
ans+=tmp;
}
printf("%lld\n",ans);
}
return 0;
}