标签:
1
4
1 2 3 4
6
题目大意:
退役狗 NanoApe 滚回去学文化课啦!
在数学课上,NanoApe 心痒痒又玩起了数列。他在纸上随便写了一个长度为
他当然知道这个最大值会随着他删了的数改变而改变,所以他想知道假如全部数被删除的概率是相等的话,差的绝对值的最大值的期望是多少。
官方题解:
求出前
时间复杂度
个人解题思路:
这个题目应该不是很难的,因为选一个数的概率是
/**
2016 - 08 - 07 上午
Author: ITAK
Motto:
今日的我要超越昨日的我,明日的我要胜过今日的我,
以创作出更好的代码为目标,不断地超越自己。
**/
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <vector>
#include <queue>
#include <algorithm>
#include <set>
using namespace std;
typedef long long LL;
typedef unsigned long long ULL;
const LL INF = 1e9+5;
const int MAXN = 1e6+5;
const int MOD = 1e9+7;
const double eps = 1e-7;
const double PI = acos(-1);
using namespace std;
LL a[MAXN], sum[MAXN];
inline bool cmp(LL a, LL b)
{
return a > b;
}
int main()
{
int T, n;
scanf("%d",&T);
while(T--)
{
scanf("%d",&n);
for(int i=1; i<=n; i++)
scanf("%I64d",&a[i]);
memset(sum, -1, sizeof(sum));
for(int i=2; i<=n; i++)
sum[i-1] = abs(a[i]-a[i-1]);
sort(sum+1, sum+n, cmp);
LL ans = 0;
if(sum[1] == abs(a[1]-a[2]))
ans += sum[2];
else
ans += sum[1];
if(sum[1] == abs(a[n]-a[n-1]))
ans += sum[2];
else
ans += sum[1];
for(int i=2; i<n; i++)
{
LL t1 = abs(a[i]-a[i-1]);
LL t2 = abs(a[i]-a[i+1]);
LL t3 = abs(a[i+1]-a[i-1]);
if(t3 >= sum[1])
ans += t3;
else
{
if(t1 == sum[1])
{
if(t2 == sum[2])
ans += max(sum[3], t3);
else
ans += max(sum[2], t3);
}
else if(t2 == sum[1])
{
if(t1 == sum[2])
ans += max(sum[3], t3);
else
ans += max(sum[2], t3);
}
else
ans += sum[1];
}
}
printf("%I64d\n",ans);
}
return 0;
}
HDU 5805 NanoApe Loves Sequence(思维)
标签:
原文地址:http://blog.csdn.net/qingshui23/article/details/52141467