ALDS1_3_D: Areas on the Cross-Section Diagram
来源:互联网 发布:万网购买域名流程 编辑:程序博客网 时间:2024/05/20 02:52
题目描述

计算积水的面积。
输入
1行,用”\”和”/”表示斜面,用”_”表示平地。
输出
第1行输出总面积,第2行输出积水数量和各积水处的横截面积。
实现
代码
#include<iostream>#include<stack>#include<vector>#include<string>#include<algorithm>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);//如果ch是'\',就把ch放入S1中。 else if(ch=='/'&&S1.size()>0)//否则如果ch是'/'并且S1中还存有'\', { int j=S1.top();S1.pop();//把S1中的第一个'\'取出来。 sum+=i-j;//i-j是这两个'\'和'/'形成的梯形的面积。 int a=i-j; while(S2.size()>0&&S2.top().first>j) { a+=S2.top().second; S2.pop(); } //如果j代表的"\"的位置小于当前S2最上面"\"的位置,说明S2最上面"\"不是最左侧的,说明这对j代表的"\"和i代表的'/'已经和之前的积水处一起形成了一个更大的积水处。 S2.push(make_pair(j,a)); } } //现在S2中储存的是一个个积水处。 vector<int> ans; while(S2.size()>0)//把S2复制到ans中去 { ans.push_back(S2.top().second);S2.pop(); } reverse(ans.begin(),ans.end());//把ans颠倒 cout<<sum<<endl; cout<<ans.size(); for(int i=0;i<ans.size();i++) { cout<<" "; cout<<ans[i]; } cout<<endl; //输出}
阅读全文
0 0
- ALDS1_3_D: Areas on the Cross-Section Diagram
- ALDS1_3_D: Areas on the Cross-Section Diagram
- 第四章 ALDS1_3_D:Areas on the Cross-Section Diagram
- Areas on the Cross-Section Diagram
- Section 3.3 Home on the Range
- USACO Section 3.3 Home on the Range
- arm-linux-gcc compile the cross-platform mpich2 on ubuntu
- On the Android ListView listSelector to solve cross-border issues
- The Cross
- usaco Chapter 3 section 3.3 Home on the Range
- USACO Section 3.3 Home on the Range - 优化的BFS..
- USACO-Section 3.3-PROB Home on the Range
- 解决方法!:getImageData on 'CanvasRenderingContext2D': The canvas has been tainted by cross-origin data.
- hdu3669 Cross the Wall
- Cross the Wall UVALive
- Clustering of residential areas based on residential conditions
- About the Use case diagram
- UML basics: The class diagram
- PC/Android/IOS平台下访问StreamingAssets文件夹下资源
- javaScript document对象详解
- 完美解决Unable to locate package、 files failed to download、fail to fatch;not get lock
- linux解压日志文件并查看
- spring源码编译导入eclipse
- ALDS1_3_D: Areas on the Cross-Section Diagram
- sublime text中Emmet的安装方法以及使用的快捷键
- win7升级正版win10方法(精华篇)!
- android/bitmap.h 详解
- LeetCode 492 构造矩形
- Ubuntu下安装日语输入法
- HTML/CSS笔记整理
- Laravel 中的Facades的研究
- Mybatis Generator最完整配置详解