传送门
- 当求最大值的题求的是平均值的最大值时:注意两个数求平均值后一定不会比原数中最大的那个更大,所以平均值取得越多越劣
Code:
#include <bits/stdc++.h>
using namespace std;
#define INF 0x3f3f3f3f
#define N 100010
#define ll long long
//#define int long long
char buf[1<<21], *p1=buf, *p2=buf;
#define getchar() (p1==p2&&(p2=(p1=buf)+fread(buf, 1, 1<<21, stdin)), p1==p2?EOF:*p1++)
inline int read() {
int ans=0, f=1; char c=getchar();
while (!isdigit(c)) {if (c=='-') f=-f; c=getchar();}
while (isdigit(c)) {ans=(ans<<3)+(ans<<1)+(c^48); c=getchar();}
return ans*f;
}
int n;
int a[N];
namespace task1{
double ans;
ll dp[N], rdp[N];
void solve() {
for (int i=n; i; --i) {
for (int j=i+1; j<=n; ++j) if (a[i]>a[j]) rdp[i]=max(rdp[i], rdp[j]);
rdp[i]+=a[i];
}
for (int i=1; i<=n; ++i) {
for (int j=1; j<i; ++j) if (a[i]>a[j]) dp[i]=max(dp[i], dp[j]);
ans=max(ans, 1.0*dp[i]+a[i]);
ans=max(ans, (dp[i]+rdp[i])/2.0);
dp[i]+=a[i];
}
printf("%.3lf\n", ans);
exit(0);
}
}
namespace task2{
int uni[N], usize, ua[N];
double ans;
ll rdp[N];
struct seg{
int tl[N<<2], tr[N<<2];
ll dat[N<<2];
multiset<ll> s[N<<2];
#define tl(p) tl[p]
#define tr(p) tr[p]
#define dat(p) dat[p]
#define s(p) s[p]
#define pushup(p) dat(p)=max(dat(p<<1), dat(p<<1|1))
void build(int p, int l, int r) {
tl(p)=l; tr(p)=r;
if (l==r) return ;
int mid=(l+r)>>1;
build(p<<1, l, mid);
build(p<<1|1, mid+1, r);
}
void add(int p, int pos, ll val) {
if (tl(p)==tr(p)) {s(p).insert(val); dat(p)=*s(p).rbegin(); return ;}
int mid=(tl(p)+tr(p))>>1;
if (pos<=mid) add(p<<1, pos, val);
else add(p<<1|1, pos, val);
pushup(p);
}
void del(int p, int pos, ll val) {
if (tl(p)==tr(p)) {s(p).erase(val); dat(p)=s(p).empty()?0ll:*s(p).rbegin(); return ;}
int mid=(tl(p)+tr(p))>>1;
if (pos<=mid) del(p<<1, pos, val);
else del(p<<1|1, pos, val);
pushup(p);
}
ll query(int p, int l, int r) {
if (l<=tl(p) && r>=tr(p)) {return dat(p);}
int mid=(tl(p)+tr(p))>>1; ll ans=0;
if (l<=mid) ans=max(ans, query(p<<1, l, r));
if (r>mid) ans=max(ans, query(p<<1|1, l, r));
return ans;
}
}left, right;
void solve() {
for (int i=1; i<=n; ++i) uni[i]=a[i];
sort(uni+1, uni+n+1);
usize=unique(uni+1, uni+n+1)-uni-1;
for (int i=1; i<=n; ++i) ua[i]=lower_bound(uni+1, uni+usize+1, a[i])-uni;
left.build(1, 1, usize); right.build(1, 1, usize);
for (int i=n; i; --i) {
rdp[i]=(ua[i]>1?right.query(1, 1, ua[i]-1):0)+a[i];
right.add(1, ua[i], rdp[i]);
}
// cout<<"rdp: "; for (int i=1; i<=n; ++i) cout<<rdp[i]<<' '; cout<<endl;
for (int i=1; i<=n; ++i) {
ll tem=(ua[i]>1?left.query(1, 1, ua[i]-1):0);
ans=max(ans, 1.0*tem+a[i]);
ans=max(ans, (tem+rdp[i])/2.0);
left.add(1, ua[i], tem+a[i]);
right.del(1, ua[i], rdp[i]);
}
printf("%.3lf\n", ans);
exit(0);
}
}
signed main()
{
freopen("b.in", "r", stdin);
freopen("b.out", "w", stdout);
n=read();
for (int i=1; i<=n; ++i) a[i]=read();
// task1::solve();
task2::solve();
return 0;
}