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画过几块板子,一直没有做过整理.每次画图遇到问题时,都 ...
随机推荐
- tyvj1202 数数食物链
描述 TsyD学习了生物的生态环境那一张后,老师留了一项作业,就是给一张食物网,求所有食物链的总数.(从最低营养级生物(它不能吃任何其他的生物)开始到最高营养级(它不能被任何其他生物吃) 叫做一条食物 ...
- hadoop集群安装_实战
spark1.6.2+ hadoop2.6.2 词频统计完整案例:http://blog.csdn.net/zythy/article/details/17852579 hadoop学习:http:/ ...
- 守护进程demon.c
1 #include <stdio.h> #include <unistd.h> #include <errno.h> #include <time.h> ...
- [Recommendation System] 推荐系统之协同过滤(CF)算法详解和实现
1 集体智慧和协同过滤 1.1 什么是集体智慧(社会计算)? 集体智慧 (Collective Intelligence) 并不是 Web2.0 时代特有的,只是在 Web2.0 时代,大家在 Web ...
- 基本 sql语句
1.打开数据库 int sqlite3_open( const char *filename, // 数据库的文件路径 sqlite3 **ppDb // 数据库实例 ); 2. ...
- 【荐】JavaScript编码风格
作者:阮一峰 Douglas Crockford是 JavaScript 权威,Json 格式就是他的发明. 去年 11 月他有一个演讲(Youtube),谈到了好的 JavaScript 编程风格是 ...
- iOS开发——高级篇——二维码的生产和读取
一.二维码的生成 从iOS7开始集成了二维码的生成和读取功能此前被广泛使用的zbarsdk目前不支持64位处理器 生成二维码的步骤:导入CoreImage框架通过滤镜CIFilter生成二维码 二维码 ...
- 使用http.sys,让delphi 的多层服务飞起来
核心提示:一直以来,delphi 的网络通讯层都是以indy 为主,虽然indy 的功能非常多,涉及到网络服务的各个方面,但是对于大多数多层服务来说,就是需要一个快速.稳定.高效的传输层.Delphi ...
- spring jpa 实体互相引用返回restful数据循环引用报错的问题
spring jpa 实体互相引用返回restful数据循环引用报错的问题 Java实体里两个对象有关联关系,互相引用,比如,在一对多的关联关系里 Problem对象,引用了标签列表ProblemLa ...
- mongodb更新操作
除了查询条件,还可以使用修改器对文档进行更新. 1. $inc > db.tianyc03.find() { "_id" : ObjectId("50ea6b6f1 ...