第四章 ALDS1_3_D:Areas on the Cross-Section Diagram
来源:互联网 发布:高晓松酒驾事件知乎 编辑:程序博客网 时间:2024/05/20 04:32
问题链接
ALDS1_3_D:Areas on the Cross-Section Diagram
问题内容
求出每个积水处的横截面积
思路
利用栈的思想去做
- 如果是’\’则将该字符的位置i压入栈
- 如果是’/’则从栈顶取出与之对应的’\’的位置
ip - 累加两者距离
i−ip ,结果即为总面积
代码
#include<iostream>#include<cstdio>#include<cstring>#include<vector>#include<stack>#include<algorithm>using namespace std;int main() { stack<int> S1; stack<pair<int, int> >S2; char s[20000 + 10]; scanf("%s", s); int i = 0, sum = 0; while (s[i] != '\0') { // 若是\则压入栈 if (s[i] == '\\') S1.push(i); else if (s[i] == '/' && !S1.empty()) { int j = S1.top(); S1.pop(); // 加上新形成的面积 sum += i - j; int a = i - j; // 同一块的合在一起 while (!S2.empty() && S2.top().first > j) { a += S2.top().second; S2.pop(); } S2.push(make_pair(j, a)); } i++; } vector<int> ans; while (!S2.empty()) { ans.push_back(S2.top().second); S2.pop(); } printf("%d\n", sum); int len = ans.size(); printf("%d", len); for (int i = len - 1; i >= 0; i--) { printf(" %d", ans[i]); } printf("\n"); return 0;}
阅读全文
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
- Zenmap常用参数解释
- xml套json
- Java应用架构的演化之路
- 二叉树的镜像
- LeetCode 279. Perfect Squares
- 第四章 ALDS1_3_D:Areas on the Cross-Section Diagram
- 平均数
- Codeforces 18A 18B 18C
- java使用new和newInstance()创建对象的区别
- 训练计划
- eclipse 配置git过滤文件
- java应用架构
- java应用架构
- 第四讲 身份认证技术