最大存水量c++

Background

Special for beginners, ^_^

Description

给你 n 个非负整数 1,2,…,a1​,a2​,…,an​。每个整数表示平面直角坐标系中的一个点(i,ai​)。
现在我们可以画出 n 条垂直线段,其中第 i 条线段的两个端点分别为 (i,ai​) 和 (i,0)。
请你在这 n 条线段中找出两条,使其与 x 轴构成一个容器的侧面图,使得该容器能保存最大容量的水。注意你不能使容器倾斜。

Format

Input

输入包含一个非负整数n(1<n≤105),接着连续输入n个整数。

Output

输出容器的最大存水量,为一个整数(不超过 10181018)。

Samples

輸入資料 1

9
1 8 6 2 5 4 8 3 7

輸出資料 1

49

輸入資料 2

2
1 1

輸出資料 2

1

輸入資料 3

5
4 3 2 1 4

輸出資料 3

16

輸入資料 4

3
1 2 1

輸出資料 4

2

Hint

 解释:图中垂直线代表输入数组 [1,8,6,2,5,4,8,3,7]。在此情况下,容器能够容纳水(表示为蓝色部分)的最大值为 49。

Limitation

1s, 1024KiB for each test case.

代码:

#include<bits/stdc++.h>
using namespace std;
const int maxn = 1e5 + 5;
long long a[maxn];
int front,back;
long long max_area;
int main()
{
	int n;
	cin>>n;
	for(int i = 1; i <= n; ++i)
	{
		cin>>a[i];
	}
	front = 1;
	back = n;
	while(front != back)
	{
		max_area = max(max_area, (back - front) * min(a[front],a[back]));
		if(a[front] > a[back])
		{
			back--;
		}
		else
		{
			front++;
		}
	}
	cout<<max_area<<endl;
	return 0;
}

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值