codeforces 1554A

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;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值