数据结构的应用——计算面积(Areas on the Cross-Section Diagram)

题目描述

Your task is to simulate a flood damage.

For a given cross-section diagram, reports areas of flooded sections.

Assume that rain is falling endlessly in the region and the water overflowing from the region is falling in the sea at the both sides. For example, for the above cross-section diagram, the rain will create floods which have areas of 4, 2, 1, 19 and 9 respectively.
在这里插入图片描述

input

A string, which represents slopes and flatlands by /, \ and _ respectively, is given in a line. For example, the region of the above example is given by a string “\///_//\\//\///__\_\//_/”.

output

Report the areas of floods in the following format:
A K L 1 L 2 L 3 ⋅ ⋅ ⋅ A\\ KL_{1}L_{2}L_{3}··· AKL1L2L3
In the first line, print the total area A A A of created floods.
In the second line, print the number of floods k k k and areas L i ( i = i , 2 , ⋅ ⋅ ⋅ , k ) L_i(i =i,2,···,k) Li(i=i,2,,k) for each flood from the left side of the cross-section diagram. Print a space character before L i L_i Li.

Sample Input 1

\//

Sample Output 1

4
1 4

Sample Input 2

\///_ //\\//\///__\_\//_/\

Sample Output 2

35
5 4 2 1 19 9

题目大意

就是用一串字符串表示一个地形断面图。“ \ ”表示左上右下倾斜,“ / ”表示右上左下倾斜,“ _ ”表示平面。然后我们要计算这个断面图最后能积多少水。我们可以先假设有连续不停的大雨,这样溢出多余的雨水将会从地形的两侧流入左右的大海。

实现思路

这题一开始我不会做,书上讲的是用栈,有点类似于那个括号匹配的思想。因为这个积水面是一个平衡的水面,所以水的左面和右面的斜面应该是一对一对应起来的。所以我们只需要统计有多少个斜面对以及它们之间的距离即可。
首先我们先计算总面积
我们可以将整个断面图看成一个Y-X的平面直角坐标系,然后开始遍历 s i s_i si:

  • 如果是“ \ ”,就将该字符的位置 i i i压入栈 S 1 S1 S1
  • 如果是“ / ”,就从S1中取出与之相对应的“ \ ”的位置 i t i_t it,算出“ / ”和相对应的“ \ ”的距离 i − i t i-i_t iit并累加的总面积里
  • 如果是“ _ ”,就将“ \ ”和“ / ”的距离加1。

其次是各积水处面积的算法:
我们需要另外一个栈 S 2 S2 S2,这个栈的每个元素包含一对数据,分别是该积水处最左侧“ \ ”的位置和该积水处当前的面积。随着不断读取“ / ”,该积水处的面积会不断增加。接下来,新形成的面积 = 当前 S 2 S2 S2中的两个面积之和 + 新形成的 i − j i - j ij部分的面积。我们需要从 S 2 S2 S2中取出被引用的多个面积,再将新算出的面积压入 S 2 S2 S2

实现细节

准备阶段

	stack<int> s1;
    stack<pair<int, int>> s2;	//准备两个栈
    char ch;	//读取每个字符
    int sum = 0;	//存储积水总面积

计算阶段

由于不确定要输入多少字符,而且我们需要逐个判断字符并记录每个字符的位数,所以采用了for循环,将 c i n > > c h cin>>ch cin>>ch放入进了判断中,我们在输入完毕后,可以输入Ctrl+Z来结束输入。

for (int i = 0; cin >> ch; i++)
    {
        if (ch == '\\') //判断"/"需要添加转义字符
            s1.push(i);		//将该处的位置入栈
        else if (ch == '/' && s1.size() > 0) 
//如果为"/"且有与之对应的"\",我们需要记录"/"的位置,并计算当前的积水面积i-j
        {
            int j = s1.top();
            s1.pop();
            sum += i - j;
            int a = i - j;
//这里是合并多个积水处的面积
//如果s2中存的积水处最左侧的位置大于当前"\"对应左侧的位置,则需要合并
            while (s2.size() > 0 && s2.top().first > j)
            {
                a += s2.top().second;
                s2.pop();
            }
//新的积水面积入栈
            s2.push(make_pair(j, a));
        }
    }

最后用 v e c t o r vector vector来存储不定长的积水处面积即可

vector<int> ans;
    while (s2.size() > 0)
    {
        ans.push_back(s2.top().second);
        s2.pop();
    }
    reverse(ans.begin(), ans.end());

由于栈是先进后出的,所以在栈顶的面积是图中相对靠右部的面积。直接输出积水的面积顺序是相反的。所以我们要 r e v e r s e reverse reverse一下

vector自带翻转元素的函数reverse(begin,end)
begin:起始位置的迭代器
end:终止位置的迭代器

最后附上完整代码

#include <bits/stdc++.h>
using namespace std;
int main()
{
    stack<int> s1;
    stack<pair<int, int>> s2;
    char ch;
    int sum = 0;
    for (int i = 0; cin >> ch; i++)
    {
        if (ch == '\\')
            s1.push(i);
        else if (ch == '/' && s1.size() > 0)
        {
            int j = s1.top();
            s1.pop();
            sum += i - j;
            int a = i - j;
            while (s2.size() > 0 && s2.top().first > j)
            {
                a += s2.top().second;
                s2.pop();
            }
            s2.push(make_pair(j, a));
        }
    }
    vector<int> ans;
    while (s2.size() > 0)
    {
        ans.push_back(s2.top().second);
        s2.pop();
    }
    reverse(ans.begin(), ans.end());
    cout << sum << endl;
    cout << ans.size();
    int len = ans.size();
    for (int i = 0; i < len; i++)
    {
        cout << " ";
        cout << ans[i];
    }
    cout << endl;
    return 0;
}

这里是题目链接ALDS1_3_D

  • 3
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值