首页 > 试题广场 >

降水量

[编程题]降水量
  • 热度指数:386 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 64M,其他语言128M
  • 算法知识视频讲解
给定n个柱面的高度,表示降雨某地n块区域的海拔高度。
计算降雨之后该地最大储水面积。如果低于地平线,也就是小于0,则一定积水


输入描述:
第一行输入整数n.(1<=n<=10000)
第二行输入n个高度整数h。(-10000<=h<=10000)


输出描述:
输出答案。
示例1

输入

12
0 1 0 2 1 0 1 3 2 1 2 1

输出

6
//两次遍历分别找到从起始点到该点的最大高度和从终点到该点的最大高度,且高度最低取0
//再次遍历,每个点的的雨水量等于该点左右最大高度中较低者减去该点高度


#include<iostream>
#include<vector>
using namespace std;
int main() {
	int n;
	cin >> n;
	vector<int>height(n,0);
	vector<int>maxL(n,0);
	vector<int>maxR(n,0);
	int tmp;
	for (int i = 0; i < n; i++) {
		cin >> height[i];
	}
	int max = 0;
	for (int i = 0; i < n; i++) {
		max = height[i] > max ? height[i] : max;
		maxL[i] = max;
	}
	max = 0;
	for (int i = n - 1; i >= 0; i--) {
		max = height[i] > max ? height[i] : max;
		maxR[i] = max;
	}
	int count = 0;
	for (int i = 0; i < n; i++) {
		int small = maxL[i] < maxR[i] ? maxL[i] : maxR[i];
		count += (small - height[i]);
	}
	cout << count;
	system("pause");
	return 0;
}

发表于 2019-09-09 17:33:03 回复(0)
def trap(height):
    def stepv(num):
        temp = 0
        out = 0
        for i in num:
            if i < temp:
                out += temp
            else:
                out += i
                temp = i
        return out, temp

    height2 = height[::-1]
    v1, maxh = stepv(height)
    v2, maxh = stepv(height2)
    return v1 + v2 - len(height) * maxh - sum(height)

n = int(input())
l = list(map(int,input().split(" ")))
print(trap(l))

发表于 2019-09-03 11:10:39 回复(2)
//找到第一个可蓄水凹点的左右两端最高点下标L、R,使用L、R下标对应高度较低的点作为蓄水的最高高度H,
//蓄水体积=(R -L-1)*H - ∑H(i)  (L <i <R),使用R作为新的蓄水起始点递归该函数求和 
int getWaterVolume(std::vector<int>& water, int index,int length)
{

    if (index == length -1)
        return 0;

    //凹洞左边的最高点
    int lowTrend = index;
    //凹洞右边的最高点
    int highTrend = index;
    //当前访问的点
    int visitIndex = index;
    //凹洞最低的点
    int lowtestIndex = index;

    while (visitIndex<length)
    {
        //向下降的趋势,则这块有可能积水,且是目前最高点
        if (visitIndex < length-1 &&water[visitIndex] > water[visitIndex + 1])
        {
            lowTrend = visitIndex;
            ++visitIndex;

            //找到最低点
            while (visitIndex < length-1 && water[visitIndex] > water[visitIndex + 1])
            {
                ++visitIndex;
            }
            //没有向上的趋势
            if (visitIndex == length - 1 && water[visitIndex] < water[visitIndex - 1])
            {
                return 0;
            }

            lowtestIndex = visitIndex - 1;

            //找到最高点
            while (visitIndex < length - 1 && water[visitIndex] < water[visitIndex + 1])
            {
                ++visitIndex;
            }
            
            //最高点
            
            highTrend = visitIndex;


            //计算坑里面的体积
            int liquidVolume = 0;
            //蓄水高度参照
            int compareNum = 0;
            //比较向下趋势的最高点,与向上趋势的最高点,谁高
            if (water[lowTrend] > water[highTrend])
            {
                compareNum = water[highTrend];
            }
            else 
            {
                compareNum = water[lowTrend];
            }
            
            for (int addIndex = lowTrend + 1; addIndex < highTrend; ++addIndex)
            {
                liquidVolume += water[addIndex];
            }

            liquidVolume = (highTrend - lowTrend - 1)*compareNum - liquidVolume;

            return liquidVolume + getWaterVolume(water, highTrend, length);


        }
        else//向上升的趋势,则这块无法积水
        {
            ++visitIndex;
        }
    }

    return 0;

}
发表于 2019-09-10 18:54:23 回复(0)