linkkkkkk
题意:
给定长度为
n
n
n的数组
c
c
c,求
m
a
x
(
(
j
−
i
)
∗
(
c
i
+
c
j
)
)
max((j-i)*(c_i+c_j))
max((j−i)∗(ci+cj))
思路:
- 将式子转化为
m
a
x
(
(
j
−
i
)
∗
(
c
j
−
(
−
c
i
)
)
)
max((j-i)*(c_j-(-c_i)))
max((j−i)∗(cj−(−ci)))
将 ( j , c j ) , ( i , − c i ) (j,c_j),(i,-c_i) (j,cj),(i,−ci)分别看做是矩形的左下角和右上角的点,问题就转化成了求矩形的最大面积。
假设 a = ( j , c j ) , b = ( i , − c i ) a=(j,c_j),b=(i,-c_i) a=(j,cj),b=(i,−ci)。 - 可以看出
b
b
b是有决策单调性的,固定
a
a
a的话,肯定
b
b
b越往右上越优;所以对于
b
b
b来说,如果
b
[
u
]
x
.
<
b
[
v
]
.
x
,
b
[
u
]
.
y
<
=
b
[
v
]
.
y
b[u]x.<b[v].x,b[u].y<=b[v].y
b[u]x.<b[v].x,b[u].y<=b[v].y,那么
v
v
v是比
u
u
u优的,可以直接忽略掉
u
u
u。
a a a也同理。 - 如果计算答案呢,假设 a a a数组的长度为 l a la la, b b b数组的长度为 l b lb lb,此时的两个数组都先按 x x x从小到大再按 y y y从小到大排序了,即从左下到右上。用 s o l v e ( l 1 , r 1 , l 2 , r 2 ) solve(l1,r1,l2,r2) solve(l1,r1,l2,r2)表示 a [ l 1 : r 1 ] a[l1:r1] a[l1:r1]和 b [ l 2 : r 2 ] b[l2:r2] b[l2:r2]的最大值,考虑怎么实现比较优秀的递归。
- 设
m
i
d
=
(
l
1
+
r
1
)
/
2
mid=(l1+r1)/2
mid=(l1+r1)/2,遍历
b
[
l
2
,
r
2
]
b[l2,r2]
b[l2,r2]找到
p
o
s
pos
pos使得
a
[
m
i
d
]
−
b
[
p
o
s
]
a[mid]-b[pos]
a[mid]−b[pos]的面积最大,即
p
o
s
pos
pos是相对于
m
i
d
mid
mid的最优解。那么接下来应该递归
s
o
l
v
e
(
l
1
,
m
i
d
−
1
,
l
2
,
p
o
s
)
solve(l1,mid-1,l2,pos)
solve(l1,mid−1,l2,pos)和
s
o
l
v
e
(
m
i
d
+
1
,
r
1
,
p
o
s
,
r
2
)
solve(mid+1,r1,pos,r2)
solve(mid+1,r1,pos,r2)。
图片来自
所以对于
a
a
a中
m
i
d
mid
mid左下的点来说,最优解只会在
p
o
s
pos
pos以及
p
o
s
pos
pos左边。
时间复杂度
O
(
n
l
o
g
n
)
O(nlogn)
O(nlogn)
代码:
// Problem: L. Two Buildings
// Contest: Codeforces - 2020-2021 ACM-ICPC, Asia Seoul Regional Contest
// URL: https://codeforces.ml/gym/102920/problem/L
// Memory Limit: 512 MB
// Time Limit: 1000 ms
//
// Powered by CP Editor (https://cpeditor.org)
#include<iostream>
#include<cstdio>
#include<string>
#include<ctime>
#include<cmath>
#include<cstring>
#include<algorithm>
#include<stack>
#include<climits>
#include<queue>
#include<map>
#include<set>
#include<sstream>
#include<cassert>
#include<bitset>
#include<list>
#include<unordered_map>
//#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<ll, ll>PLL;
typedef pair<int, int>PII;
typedef pair<double, double>PDD;
typedef pair<string,string>PSS;
#define I_int ll
inline ll read(){ll x = 0, f = 1;char ch = getchar();while(ch < '0' || ch > '9'){if(ch == '-')f = -1;ch = getchar();}while(ch >= '0' && ch <= '9'){x = x * 10 + ch - '0';ch = getchar();}return x * f;}
inline void write(ll x){if (x < 0) x = ~x + 1, putchar('-');if (x > 9) write(x / 10);putchar(x % 10 + '0');}
#define read read()
#define closeSync ios::sync_with_stdio(0);cin.tie(0);cout.tie(0)
#define multiCase int T;cin>>T;for(int t=1;t<=T;t++)
#define rep(i,a,b) for(int i=(a);i<=(b);i++)
#define repp(i,a,b) for(int i=(a);i<(b);i++)
#define per(i,a,b) for(int i=(a);i>=(b);i--)
#define perr(i,a,b) for(int i=(a);i>(b);i--)
#define x first
#define y second
ll ksm(ll a, ll b,ll mod){ll res = 1;while(b){if(b&1)res=res*a%mod;a=a*a%mod;b>>=1;}return res;}
const int maxn=5e6+100,inf=0x3f3f3f3f;
ll ans,n,c[maxn];
struct node{
ll x,y;
}a[maxn],b[maxn];
bool cmp(node a,node b){
if(a.x==b.x) return a.y<b.y;
return a.x<b.x;
}
ll solve(int l1,int r1,int l2,int r2){
if(l1>r1||l2>r2) return 0;
int mid=(l1+r1)/2;
ll maxx=-1e18,pos=l2;
for(int i=l2;i<=r2;i++){
if(a[mid].x>b[i].x&&a[mid].y>b[i].y) continue;
ll tmp=(b[i].x-a[mid].x)*(b[i].y-a[mid].y);
if(maxx<tmp) maxx=tmp,pos=i;
}
maxx=max(maxx,solve(l1,mid-1,l2,pos));
maxx=max(maxx,solve(mid+1,r1,pos,r2));
ans=max(ans,maxx);
return maxx;
}
int main() {
n=read;
rep(i,1,n){
c[i]=read;
a[i]={i,-c[i]};b[i]={i,c[i]};
}
sort(a+1,a+1+n,cmp);
sort(b+1,b+1+n,cmp);
int pos=1,idx=0;
unordered_map<int,int>mp;
for(int i=2;i<=n;i++)
if(a[i].y>=a[pos].y) mp[i]=1;
else pos=i;
for(int i=1;i<=n;i++)
if(!mp[i]) a[++idx]=a[i];
int tot=0;mp.clear();
pos=n;
for(int i=n-1;i;i--)
if(b[i].y<=b[pos].y) mp[i]=1;
else pos=i;
for(int i=1;i<=n;i++)
if(!mp[i]) b[++tot]=b[i];
solve(1,idx,1,tot);
cout<<ans<<endl;
return 0;
}