补题补题,stack的应用

ALDS1_3_D: Areas on the Cross-Section Diagram

题目摘要


计算积水的面积
计算积水的面积。

输入

1行,用”\”和”/”表示斜面,用”_”表示平地。


程序:

//#include <bits/stdc++.h>
//using namespace std;
//
//int main()
//{
//    vector<int> V;
//    for (int i=0; i<5; i++){
//        V.push_back(i);
//    }
//    for (int i=0; i<5; i++){
//        cout<<V[i]<<" ";
//    }
//    cout<<endl;
//    reverse(V.begin(), V.end());
//    for (int i=0; i<5; i++){
//        cout<<V[i]<<" ";
//    }
//    return 0;
//}

#include <bits/stdc++.h>
using namespace std;

//样例:\\///\_/\/\\\\/_/\\///__\\\_\\/_\/_/\

int main(){
    stack<int> index;
    stack<pair<int, int> > area;
    char ch;
    int sum = 0;
    for (int i=0; cin>>ch; i++){
        if (ch == '\\') index.push(i);
        else if (ch == '/'&&index.size()>0){
            int j = index.top();
            index.pop();
            int s = i-j;
            sum += s;
            while(area.size()>0&&area.top().first>j){
                s += area.top().second//修改前面的记录的下标和对应的面积,看p87就能明白这个while循环的意义了!!
                area.pop();
            }
            area.push(make_pair(j, s));
            //cout<<s<<" ";
        }
    }
    //cout<<endl;
    vector<int> ans;
    while(!area.empty()){
        ans.push_back(area.top().second);
        area.pop();
    }
    reverse(ans.begin(), ans.end());//翻转整个vector,注意这个一定要是个可迭代的对象。如果是数组,和swap()效果一样
    cout<<sum<<endl;
    cout<<ans.size()<<" ";
    for(int i=0; i<ans.size(); i++){
        cout<<ans[i]<<" ";
    }
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值