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画过几块板子,一直没有做过整理.每次画图遇到问题时,都 ...
随机推荐
- LYDSY模拟赛day2 Dash Speed
/* 弃坑 */ #include<cstdio> #include<algorithm> using namespace std; ,M=N*; ],nxt[N<< ...
- PHP 获取中国时间,即上海时区时间
/** * 获取中国时间,即上海时区时间 * @param <type> $format * @return <type> */ function getChinaTime($ ...
- chrome地址栏搜索直接跳转百度首页?
https://www.baidu.com/s?ie={inputEncoding}&wd=%s
- javascript基础05
javascript基础05 1.变量的作用域 变量既可以是全局,也可以是局部的. 全局变量:可以在脚本中的任何位置被引用,一旦你在某个脚本里声明了全局变量,你就可以 在这个脚本的任何位置(包括函数内 ...
- linux常用命令-文件搜索命令-find
find [目录] [选项] 文件名或者正则表达式 -name 根据文件名搜索 -iname 搜索文件名的时候忽略大小写 例:find /etc -name init find /etc -i ...
- 记录一下两个比较常用的md5加密算法
第一个,计算字符串的md5值 public static String getMD5(String s){ String newString = null; byte[] inputByteArray ...
- python模块引用梳理
文件组织结构: t ├── __init__.py ├── main.py ├── t1 │ ├── A.py │ └── __init__.py └── t2 ├── B.py └── __ ...
- 关于git的简单实用命令
时代在进步啊,现在已经不是svn的时代了,好多人都在使用git.所以自己也稍微学习了下git的使用. 常见的通过git提交代码步骤: git status :查看文件状态 :该命令显示你工程内修改的所 ...
- delphi 各新版本特性收集
delphi 各新版本特性收集 http://www.cnblogs.com/dreamszx/p/3602589.html
- BubbleSort冒泡排序
#include <stdio.h>void BubbleSort(int *a,int n);int main(void){ int arr[10] = {2,4,6,8,0,1,3,5 ...