……

51nod——2478 小b接水(预处理 思维)

  我本来想把每个谷都处理了,想了下觉得不好办。后来看其他人写的是处理每个位置,把每个位置可以接的水累加起来。整挺好。

 

#include <bits/stdc++.h>
using namespace std;
#define maxn 50050
int h[maxn],lh[maxn],rh[maxn];

int main(){
    std::ios::sync_with_stdio(0);
    cin.tie(0);
    int n; cin>>n;
    for(int i=0;i<n;i++) cin>>h[i];
    int ans=0;
    lh[0]=h[0],rh[n-1]=h[n-1];//边界
    //预处理出每个点左边最高和右边最高,取较小者减去当前高度就是这个位置可以放多少水
    for(int i=1;i<n-1;i++){
        lh[i]=max(lh[i-1],h[i]);
        rh[n-i-1]=max(rh[n-i],h[n-i-1]);
    }
    for(int i=1;i<n-1;i++){
        if(h[i]<lh[i]&&h[i]<rh[i])
            ans+=min(lh[i],rh[i])-h[i];
    }
    cout<<ans<<endl;

    return 0;
}

 

posted @ 2019-05-29 23:14  noobimp  阅读(189)  评论(0编辑  收藏  举报