鞋码全真模拟赛4 补题报告
Problem(T1)
有一个长度为 的数组 ,求 最大的三元组 ,满足:
如果没有满足条件的三元组,输出 。
Solution(T1)
枚举 ,然后使 最大有四种情况:
- 正正正。三个非负整数,那 和 要尽量大,所以 ,。
- 负负正。前两个负数负负得正,那么 就要尽量小, 还是尽量大。
- 负正正。结果是负数,那 要尽量大, 尽量小。
- 负负负。 要尽量大, 尽量小。
所以用 表示前缀最小值, 表示后缀最大值。 中的小于 的最大值,可以用树状数组或 维护。 中大于 的最小值,同理,用树状数组或 维护。
Code
#include<bits/stdc++.h>
#define int long long
using namespace std;
int read(){
int s = 0,w = 1;
char ch = getchar();
while(ch < '0' || ch > '9'){if(ch == '-') w = -1;ch = getchar();}
while(ch >= '0' && ch <= '9'){s = (s << 3) + (s << 1) + ch - '0';ch = getchar();}
return s * w;
}
const int MAXN = 1e5 + 8;
int n,a[MAXN],pre[MAXN],suf[MAXN],maxn = 0;
void solve(){
n = read();
set<int> s,t;
pre[0] = 2e18;
for(int i = 1;i <= n;i++)
a[i] = read(),pre[i] = min(pre[i - 1],a[i]);
suf[n + 1] = -2e18;
for(int i = n;i >= 1;i--)
suf[i] = max(suf[i + 1],a[i]);
int ans = -2e18;
bool flag = true;
for(int i = 1;i <= n;i++){
if(pre[i - 1] >= a[i]){
s.insert(a[i]);
continue;
}
if(suf[i + 1] <= a[i]) continue;
auto x = s.lower_bound(a[i]);
if(x != s.begin()){
flag = false;
x--;
int X = *x;
// cout << pre[i - 1] << " " << X << " " << " " << a[i] << " " << suf[i + 1] << "\n";
ans = max(ans,max(pre[i - 1] * a[i] * suf[i + 1],X * a[i] * suf[i + 1]));
}
s.insert(a[i]);
}
for(int i = n;i >= 1;i--){
if(pre[i - 1] >= a[i]) continue;
if(suf[i + 1] <= a[i]){
t.insert(a[i]);
continue;
}
auto x = t.upper_bound(a[i]);
if(x != t.end()){
flag = false;
int X = *x;
// cout << pre[i - 1] << " " << X << " " << " " << a[i] << " " << suf[i + 1] << "\n";
ans = max(ans,max(pre[i - 1] * a[i] * suf[i + 1],pre[i - 1] * a[i] * X));
}
t.insert(a[i]);
}
if(flag) cout << "-1\n";
else cout << ans << "\n";
}
signed main(){
// freopen("team.in","r",stdin);
// freopen("team.out","w",stdout);
int T;T = read();
while(T--)
solve();
return 0;
}