Areas on the Cross-Section Diagram

Areas on the Cross-Section Diagram Aizu - ALDS1_3_D
Areas on the Cross-Section Diagram
地域の治水対策として、洪水の被害状況をシミュレーションで仮想してみよう。
図のように $1 \times 1 (m^2)$ の区画からなる格子上に表された地域の模式断面図が与えられるので、地域にできる各水たまりの面積を報告してください。
与えられた地域に対して限りなく雨が降り、地域から溢れ出た水は左右の海に流れ出ると仮定します。 例えば、図の断面図では、左から面積が 4、2、1、19、9 の水たまりができます。
入力
模式断面図における斜面を '/' と '\'、平地を '_' で表した文字列が1行に与えられます。例えば、図の模式断面図は文字列
\\///\_/\/\\\\/_/\\///__\\\_\\/_\/_/\ で与えられます。
出力
次の形式で水たまりの面積を出力してください。
$A$
$k$ $L_1$ $L_2$ ... $L_k$
1行目に地域にできる水たまりの総面積を表す整数 $A$ を出力してください。
2行目に水たまりの数 $k$、各水たまりの面積 $L_i (i = 1, 2, ..., k)$ を断面図の左から順番に空白区切りで出力してください。
制約
- $1 \leq 文字列の長さ \leq 20,000$
ただし、得点の 50 点分は以下の条件を満たす。
- 水たまりの数は1つ以下であり ($k \leq 1$)、かつ文字列の長さは 100 以下である。
入力例 1
\\//
出力例 1
4
1 4
入力例 2
\\///\_/\/\\\\/_/\\///__\\\_\\/_\/_/\
出力例 2
35
5 4 2 1 19 9
Note
题目分析:
本题是一道对栈的应用比较好的一道题,不仅运用栈的特性解决了水坑的左右匹配问题,还将他们分开储存,也仅仅只是运用一个下标判断和栈的特性。
代码如下:
#include <iostream>
#include <cstdio>
#include <stack>
#include <vector>
#include <algorithm>
using namespace std; int main(){
stack<int> S1;
stack<pair<int, int> > S2;
char ch;
int sum = ;
for(int i = ;cin >> ch; i++){
if(ch == '\\')S1.push(i);
else if(ch == '/' && S1.size() > ){
int j = S1.top();S1.pop();
sum += i - j;
int a = i - j;
while(S2.size() > && S2.top().first > j){
a += S2.top().second;S2.pop();
}
S2.push(make_pair(j, a));
}
}
vector<int> ans;
while(S2.size() > ){ans.push_back(S2.top().second);S2.pop();}
reverse(ans.begin(),ans.end());
cout<< sum << endl;
cout<< ans.size();
for( int i = ; i < ans.size(); i++){
cout<< " ";
cout<< ans[i];
}
cout << endl;
return ;
}
Areas on the Cross-Section Diagram的更多相关文章
- Transistor 晶体管 场效应 双极型 达林顿 CMOS PMOS BJT FET
Transistor Tutorial Summary Transistor Tutorial Summary Bipolar Junction Transistor Tutorial We can ...
- Circles and Pi
Circles and Pi Introduction id: intro-1 For as long as human beings exist, we have looked to the sky ...
- Video processing systems and methods
BACKGROUND The present invention relates to video processing systems. Advances in imaging technology ...
- Computer Generated Angular Fisheye Projections [转]
Computer GeneratedAngular Fisheye Projections Written by Paul Bourke May 2001 There are two main ide ...
- 每日英语:Risk-Averse Culture Infects U.S. Workers, Entrepreneurs
Americans have long taken pride on their willingness to bet it all on a dream. But that risk-taking ...
- linux heads分析(转)
内核默认的运行地址为PHY_OFFSET+0x8000,即物理地址开始后的0x8000字节处,前面是留给参数用的.参数以atag方式存储,默认放在0x100偏移位置. http://blog.chin ...
- BC in fluent
Boundary conditions in Fluent Table of Contents 1. Boundary Conditions (BC) 1.1. Turbulence Paramete ...
- HEC-ResSim原文档
HEC-ResSim Reservoir System Simulation User's Manual Version 3.1 May 201 ...
- STA之RC Corner再论
Q:RC-Corner跟PVT怎么组合? A:通常的组合: Q:通常说的ttcorner指的是啥? A:@孟时光 ttcorner是指管子在tt+RCtyp吧. Typesof corners W ...
- 使用Cadence绘制PCB流程
转载:https://blog.csdn.net/hailin0716/article/details/47169799 之前使用过cadence画过几块板子,一直没有做过整理.每次画图遇到问题时,都 ...
随机推荐
- PHP输出XML文件函数
PHP输出XML文件函数 function xml_out($content, $charset = 'utf-8') { @header("Expires: -1"); @hea ...
- 第2月第25天 BlocksKit
1.blockskit https://github.com/zwaldowski/BlocksKit bk_showAlertViewWithTitle 2.toast +(void)showToa ...
- c++笔记整理
一:导读 假设编写了一个C++程序,如何让他允许起来呢,这取决于计算机环境和所使用的C++编译器. 1.使用文本编辑器编写程序,并将其保存在文档中,====此就是源代码 2.编译源代码,编译过程就意味 ...
- 为什么get比post更快
引言 get和post在面试过程中一般都会问到,一般的区别: 1.post更安全(不会作为url的一部分,不会被缓存.保存在服务器日志.以及浏览器浏览记录中) 2.post发送的数据量更大(get有u ...
- ACM/ICPC 之 卡卡的矩阵旅行-最小费用最大流(可做模板)(POJ3422)
将每个点拆分成原点A与伪点B,A->B有两条单向路(邻接表实现时需要建立一条反向的空边,并保证环路费用和为0),一条残留容量为1,费用为本身的负值(便于计算最短路),另一条残留容量+∞,费用为0 ...
- js中的事件委托或是事件代理详解
起因: 1.这是前端面试的经典题型,要去找工作的小伙伴看看还是有帮助的: 2.其实我一直都没弄明白,写这个一是为了备忘,二是给其他的知其然不知其所以然的小伙伴们以参考: 概述: 那什么叫事件委托呢?它 ...
- 一款全兼容的播放器 videojs
[官网]http://www.videojs.com/ videojs就提供了这样一套解决方案,他是一个兼容HTML5的视频播放工具,早期版本兼容所有浏览器,方法是:提供三个后缀名的视频,并在不支持h ...
- 【转】android中ListView的定位:使用setSelectionFromTop实现ListView的position的保持
如果一个ListView太长,有时我们希望ListView在从其他界面返回的时候能够恢复上次查看的位置,这就涉及到ListView的定位问题: 解决的办法如下: 1 2 3 4 5 6 7 // 保存 ...
- 【原创】js中input type=file的一些问题
1.介绍 在开发中,文件上传必不可少,input[type=file] 是常用的上传标签,但是它长得又丑.浏览的字样不能换,但是他长得到底有多丑呢.我们来看看在不同浏览器里的样子吧. <inpu ...
- 使用vs2008搭建php扩展环境
所需要的工具或者文件: 1.php源码,去官网下载即可.http://windows.php.net/download 2.php-sdk:php 开发工具包 3.deps类库 4.Cygwin:wi ...