蓄水池蓄水问题


// Water pool.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include <windows.h>
#include <iostream>
using namespace std;

int Volume(int * height, int n)
{
    int result = 0;
    int p_now = 1, p_prev = 0, p_prevprev = -1;
    int zeropool = 1;
    int p_highestleft = 0;
    int lengthbrick = 0;
    int h_highnext = 0;
    if (height[p_now] < height[p_prev])
        zeropool = 0;
    for (size_t i = 2; i < n; i++)
    {
        p_now = i;
        p_prev = i - 1;
        p_prevprev = i - 2;
        if ((height[p_now] < height[p_prev]) && (height[p_prevprev] < height[p_prev]))
        {
            cout << "xushui" << endl;
            if (!zeropool)
            {

                if (height[p_highestleft] > height[p_prev])
                {
                    result += lengthbrick*(height[p_prev] - h_highnext);
                    for (size_t j = p_highestleft + lengthbrick + 1; j < p_prev; j++)
                    {
                        result += height[p_prev] - height[j];
                    }
                    lengthbrick = p_prev - p_highestleft;
                    h_highnext = height[p_prev];
                }
                else //(height[p_highestleft] <= height[p_prev])
                {
                    result += lengthbrick*(height[p_highestleft] - h_highnext);
                    for (size_t j = p_highestleft + lengthbrick + 1; j < p_prev; j++)
                    {
                        result += height[p_highestleft] - height[j];
                    }
                    p_highestleft = p_prev;
                    lengthbrick = 0;
                    h_highnext = height[p_now];
                }

            }
            else //(zeropool)
            {
                zeropool = 0;
                p_highestleft = p_prev;
                h_highnext = height[p_now];

            }
        }
        else //if ((height[p_now] < height[p_prev]) && (height[p_prevprev] < height[p_prev]))
        {

        }
        //cout << i << ":"<<result << endl;
    }   
    p_prev = n - 1;
    p_prevprev = n - 2;
    if ((height[p_prevprev] < height[p_prev]))
    {
        if (!zeropool)
        {

            if (height[p_highestleft] > height[p_prev])
            {
                result += lengthbrick*(height[p_prev] - h_highnext);
                for (size_t j = p_highestleft + lengthbrick + 1; j < p_prev; j++)
                {
                    result += height[p_prev] - height[j];
                }
            }
            else //(height[p_highestleft] <= height[p_prev])
            {
                result += lengthbrick*(height[p_highestleft] - h_highnext);
                for (size_t j = p_highestleft + lengthbrick + 1; j < p_prev; j++)
                {
                    result += height[p_highestleft] - height[j];
                }
            }

        }
    }

    cout << result;
    return result;
}
int main(void)
{
    int A[] = { 3, 2, 1, 2, 1, 2, 1, 2, 3 };//10
    int n = 9;
    //
    //int A[] = { 3,2,1,2,1,2,3 };//7
    //int n = 7;
    Volume(A, n);
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值