Codeforces Round #735 (Div. 2)-A. Cherry-题解

Posted Tisfy

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Codeforces Round #735 (Div. 2)-A. Cherry-题解相关的知识,希望对你有一定的参考价值。

Codeforces Round #735 (Div. 2)-A. Cherry

传送门
Time Limit: 1 second
Memory Limit: 256 megabytes

Problem Description

You are given n n n integers a 1 , a 2 , … , a n a_1, a_2, \\ldots, a_n a1,a2,,an. Find the maximum value of m a x ( a l , a l + 1 , … , a r ) ⋅ m i n ( a l , a l + 1 , … , a r ) max(a_l, a_{l + 1}, \\ldots, a_r) \\cdot min(a_l, a_{l + 1}, \\ldots, a_r) max(al,al+1,,ar)min(al,al+1,,ar) over all pairs ( l , r ) (l, r) (l,r) of integers for which 1 ≤ l < r ≤ n 1 \\le l < r \\le n 1l<rn.

Input

The first line contains a single integer t t t ( 1 ≤ t ≤ 10   000 1 \\le t \\le 10\\,000 1t10000)  — the number of test cases.

The first line of each test case contains a single integer n n n ( 2 ≤ n ≤ 1 0 5 2 \\le n \\le 10^5 2n105).

The second line of each test case contains n n n integers a 1 , a 2 , … , a n a_1, a_2, \\ldots, a_n a1,a2,,an ( 1 ≤ a i ≤ 1 0 6 1 \\le a_i \\le 10^6 1ai106).

It is guaranteed that the sum of n n n over all test cases doesn’t exceed 3 ⋅ 1 0 5 3 \\cdot 10^5 3105.

Output

For each test case, print a single integer  — the maximum possible value of the product from the statement.

Sample Input

4
3
2 4 3
4
3 2 3 1
2
69 69
6
719313 273225 402638 473783 804745 323328

Sample Onput

12
6
4761
381274500335

Note

Let f ( l , r ) = m a x ( a l , a l + 1 , … , a r ) ⋅ m i n ( a l , a l + 1 , … , a r ) f(l, r) = max(a_l, a_{l + 1}, \\ldots, a_r) \\cdot min(a_l, a_{l + 1}, \\ldots, a_r) f(l,r)=max(al,al+1,,ar)min(al,al+1,,ar).

In the first test case,

So the maximum is f ( 2 , 3 ) = 12 f(2, 3) = 12 f(2,3)=12.

In the second test case, the maximum is f ( 1 , 2 ) = f ( 1 , 3 ) = f ( 2 , 3 ) = 6 f(1, 2) = f(1, 3) = f(2, 3) = 6 f(1,2)=f(1,3)=f(2,3)=6.


题目大意

给你一个数组,让你从中选取连续的几个数( ≥ 2 \\geq2 2个),计算这些数的最大值和最小值的乘积。

输出最大的那个乘积。

解题思路

假如要选择 l l l r r r的数,那么光选择上的复杂度已经达到了 O ( n 2 ) O(n^2) O(n2)

接下来看如何优化:

在选择了 l l l之后(假设 a [ l ] a[l] a[l]是这个序列中最大的元素),在 l l l r r r之间一定经过 a [ l + 1 ] a[l+1] a[l+1]。如果 a [ l + 1 ] a[l+1] a[l+1]很大,那么再往后选就没有意义了,因为 a [ l + 2 ] ≤ a [ l + 1 ] a[l+2]\\leq a[l+1] a[l+2]a[l+1]。同样,如果 a [ l + 1 ] a[l+1] a[l+1]很小,再选上 a [ l + 2 ] a[l+2] a[l+2]也是要经过 a [ l + 1 ] a[l+1] a[l+1]的,那么最小的那个还是会变成 a [ l + 1 ] a[l+1] a[l+1]。因此选择长度 > 2 >2 >2的序列没有意义。

因为 l l l可以任意选择,所以不用担心 a [ l + 2 ] a[l+2] a[l+2]特别大的情况,因为早晚会选到 a [ l + 2 ] a[l+2] a[l+2]作为最大的元素(最后一个元素也没有关系,会由倒数第二个与之专门成对)。

代码实现就不困难了。


AC代码

记得用 l o n g   l o n g long\\ long long long

#include <bits/stdc++.h>
using namespace std;
#define mem(a) memset(a, 0, sizeof(a))
#define dbg(x) cout << #x << " = " << x << endl
#define fi(i, l, r) for (int i = l; i < r; i++)
#define cd(a) scanf("%d", &a)
typedef long long ll;
ll a[200010];
int main()
{
    int N;
    cin>>N;
    while(N--)
    {
        int n;
        cd(n);
        for(int i=0;i<n;i++)
            scanf("%lld",&a[i]);
        ll ans=0;
        for(int i=1;i<n;i++)
            ans=max(ans,a[i-1]*a[i]);
        printf("%lld\\n",ans);
    }
    return 0;
}

原创不易,转载请附上原文链接哦~
Tisfy:https://letmefly.blog.csdn.net/article/details/119240081

以上是关于Codeforces Round #735 (Div. 2)-A. Cherry-题解的主要内容,如果未能解决你的问题,请参考以下文章

Codeforces Round #735 (Div. 2)-C. Mikasa-题解

Codeforces Round #735 (Div. 2)-B. Cobb-题解

Codeforces Round #735 (Div. 2)-A. Cherry-题解

Codeforces Round #735 (Div. 2)-C. Mikasa-题解

Codeforces Round #735 (Div. 2)-B. Cobb-题解

Codeforces Round #735 (Div. 2)-A. Cherry-题解