满分做法:

几组样例充分说明,最优的子序列单调区间最多不超过两个。所以可以用树状数组维护三个值,一个是单增的最大值,一个是单减的最大值,一个是有拐点的最大值。最后离散化倒叙处理即可

#include<cstring>
#include<cmath>
#include<iostream>
#include<algorithm>
#include<cstdio>
using namespace std;
typedef long long ll;
const int maxm=1e5+7;
int n;
ll a[maxm],b[maxm];
ll c1[maxm];
ll c2[maxm];
ll c3[maxm];
int l;
double ans=0;
int lowbit(int x)
{
 return x&(-x);
}
void add1(int x,ll y)
{
   for(;x;x-=lowbit(x))
   c1[x]=max(c1[x],y);
}
void add2(int x,ll y)
{
   for(;x<=l;x+=lowbit(x))
   c2[x]=max(c2[x],y);
}
void add3(int x,ll y)
{
   for(;x;x-=lowbit(x))
   c3[x]=max(c3[x],y);
}
ll ask1(int x)
{
  ll ans=0;
  for(;x<=l;x+=lowbit(x))
  ans=max(ans,c1[x]);
  return ans;
}
ll ask2(int x)
{
  ll ans=0;
  for(;x;x-=lowbit(x))
  ans=max(ans,c2[x]);
  return ans;
}
ll ask3(int x)
{
  ll ans=0;
  for(;x<=l;x+=lowbit(x))
  ans=max(ans,c3[x]);
  return ans;
}
int main()
{
 scanf("%d",&n);
 for(int i=1;i<=n;i++)
 scanf("%lld",a+i),b[i]=a[i];
 sort(b+1,b+n+1);
 l=unique(b+1,b+n+1)-b-1;
 for(int i=1;i<=n;i++)
 {
  a[i]=lower_bound(b+1,b+l+1,a[i])-b+1;//防止有0    
 }
 l++;
 for(int i=n;i>=1;i--)
 {
  ll w1=ask1(a[i]+1),w2=ask2(a[i]-1),w3=ask3(a[i]+1);
  ans=max(ans,(1.0*w3+b[a[i]-1])/2.0);
  ans=max(ans,(1.0*w2+b[a[i]-1])/2.0);
  ans=max(ans,1.0*w1+b[a[i]-1]);
  add1(a[i],b[a[i]-1]+w1);
  add2(a[i],b[a[i]-1]+w2);
  add3(a[i],b[a[i]-1]+max(w2,w3));
 }
 printf("%.3lf",ans);
 return 0;
}
01-14 11:39