A. Cherry
You are given n integers a1,a2,…,an. Find the maximum value of max(al,al+1,…,ar)⋅min(al,al+1,…,ar) over all pairs (l,r) of integers for which 1≤l<r≤n.
Input The first line contains a single integer t (1≤t≤10000) — the
number of test cases.The first line of each test case contains a single integer n
(2≤n≤105).The second line of each test case contains n integers a1,a2,…,an
(1≤ai≤106).It is guaranteed that the sum of n over all test cases doesn’t exceed
3⋅105.Output For each test case, print a single integer — the maximum
possible value of the product from the statement.
题意:给你一个数组让你在这个数组的随机的一个范围内[l,r] (0<=l<r<n)中找出一个最大值(max)和一个最小值(max),然后s=max*min 然你求这个数组内的最大的s
个人想法:当时想的贼简单,就直接数组范围内的最大值,然后再找一个最小值输出,然后wa了
思路:我们想一想,你现在只有三个数 例如: 2 3 4, 如果我从最开始2 3开始,然后放入4发现最大值变了, 但是这时候也会发现,我用3 4不是最后值不也是更大的吗,然后这里我们就可以思考,是不是所有的都找相邻的两个数就可以了,我们再来一个3 2 4,发现确实是如此的,那么在带入四个数 {2 3 4 5},{3,2,4,5},{4,3,2,5},最后发现最大值都是两个相邻的数,那么这道题的最大值,就是直接输出相邻两个数相乘最大的
代码:
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 1e5+10,mod=998244353,INF=0x3f3f3f3f;
ll a[N];
int main()
{
int T;cin>>T;
while(T--)
{
int n;cin>>n;
int ma=0,mi=INF;
ll sum=0;
for(int i=1;i<=n;i++)cin>>a[i],sum=max(sum,a[i]*a[i-1]);
cout<<sum<<endl;
}
return 0;
}