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;
}