題目描述
現在有一條筆直的水槽,裏面有N塊擋板。你需要從中選取兩塊擋板,使得能夠獲取最多的水容量。
輸入
測試數據由多組輸入樣例組成
第一行輸入一個整數N,( 2 <= N <= 20000 )
第二行輸入N個整數 ni ( 0 <= ni <= 10000),分別代表擋板的高度
輸出
輸出能夠獲取的最多水容量
樣例輸入 Copy
9
1 8 6 2 5 4 8 3 7
樣例輸出 Copy
49
解題思路
每次用左邊的擋板和右邊的擋板比較,那根擋板段就用那根擋板的高度*他們的距離,如果左邊的擋板段l+1,如果右邊的擋板短r-1;知道l>=r爲止。
#include<bits/stdc++.h>
using namespace std;
int main(){
int n,i,a[20005],j,t,max1,l,r,s;
while(~scanf("%d",&n)){
for(i=1;i<=n;i++){
scanf("%d",&a[i]);
}
l=1;
r=n;
max1=0;
while(l<r){
t=min(a[l],a[r]);
s=(r-l)*t;
max1=max(s,max1);
if(a[l]<=a[r]) l++;
else r--;
}
printf("%d\n",max1);
}
return 0;
}